You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@nbiederbeck - thanks for raising the issue! Unfortunately, this is one of the very few numpy functions which we still cannot override (numpy allows only to override functions that take an array as an argument, so np.full_likeis possible, though there is some work on trying to change this). I think @pllim's suggestion is probably the cleanest that we can do at the moment.
@pllim - you are right that we should add this to the known issues. One similarly cannot do np.zeros, np.ones, and np.empty.
@astrojuanlu - thanks for raising the new issue. I marked the present one 'upstream fix required' since there are developments to make it work for numpy >=1.20 (though it would need an additional like= argument), but discussion is still ongoing - numpy/numpy#17075
I just saw this due to the xref in numpy/numpy#17075, it seems like np.full_like would be of use (there's no need for like=), and that's available since NumPy 1.16 (or maybe earlier). I have zero experience with astropy so I can't really comment on specifics for it and perhaps can be implemented/used here. np.full_like is very useful in other libraries such as CuPy, for example:
In [1]: importnumpyasnp, cupyascpIn [2]: a=cp.ones(3)
In [3]: b=np.full_like(a, 7)
In [4]: bOut[4]: array([7., 7., 7.])
In [5]: type(b)
Out[5]: cupy.core.core.ndarrayIn [6]: b=np.full_like(a, 7, shape=(2, 2)) # You can override properties such as `shape`In [7]: bOut[7]:
array([[7., 7.],
[7., 7.]])
In [8]: type(b)
Out[8]: cupy.core.core.ndarray
I see now that the above has already been proposed in #10836 (comment). Note that like= may not achieve the desired result in that case either, because we'll only pass the array type downstream (in this case, u.Quantity), but properties will still need to be specified in np.full's constructor.
@pentschev - indeed, np.full_like is already supported. I think that like= would work, as all we'd need was indeed the class. But I had forgotten that, as you note, full_like is quite similar already to full(..., like=), since one can override the shape and other properties. So,
It would have been nice if the fill_value defaulted to the input array if that is a scalar...
Given that full_like is sort of an extension to fill, I guess this was a design decision, as fill_value is a required argument in full. But I see in the example above how it looks weird, unfortunately I think we don't have a better solution at the moment.
Update: like= now is supported in numpy 1.20, so we should gain an override method. For np.full, that could look at the fill argument and infer the unit from that.
Activity
pllim commentedon Oct 12, 2020
You could also do:
But for the original request, perhaps @mhvk has an idea. If it is indeed a "won't fix," we should probably add it to https://docs.astropy.org/en/latest/known_issues.html .
mhvk commentedon Oct 12, 2020
@nbiederbeck - thanks for raising the issue! Unfortunately, this is one of the very few numpy functions which we still cannot override (numpy allows only to override functions that take an array as an argument, so
np.full_likeis possible, though there is some work on trying to change this). I think @pllim's suggestion is probably the cleanest that we can do at the moment.@pllim - you are right that we should add this to the known issues. One similarly cannot do
np.zeros,np.ones, andnp.empty.nbiederbeck commentedon Oct 13, 2020
Ok, thanks for your quick answers!
astrojuanlu commentedon Oct 13, 2020
If you are okay then, until a proper fix is in place, this would be simple enough for new contributors.
pllim commentedon Oct 13, 2020
@astrojuanlu , to avoid confusing new contributor, I wonder if we should open a new issue just to address the doc part of things.
astrojuanlu commentedon Oct 13, 2020
#10845
mhvk commentedon Oct 13, 2020
@astrojuanlu - thanks for raising the new issue. I marked the present one 'upstream fix required' since there are developments to make it work for numpy >=1.20 (though it would need an additional
like=argument), but discussion is still ongoing - numpy/numpy#17075pentschev commentedon Oct 28, 2020
I just saw this due to the xref in numpy/numpy#17075, it seems like
np.full_likewould be of use (there's no need forlike=), and that's available since NumPy 1.16 (or maybe earlier). I have zero experience with astropy so I can't really comment on specifics for it and perhaps can be implemented/used here.np.full_likeis very useful in other libraries such as CuPy, for example:pentschev commentedon Oct 28, 2020
I see now that the above has already been proposed in #10836 (comment). Note that
like=may not achieve the desired result in that case either, because we'll only pass the array type downstream (in this case,u.Quantity), but properties will still need to be specified innp.full's constructor.mhvk commentedon Oct 28, 2020
@pentschev - indeed,
np.full_likeis already supported. I think thatlike=would work, as all we'd need was indeed the class. But I had forgotten that, as you note,full_likeis quite similar already tofull(..., like=), since one can override theshapeand other properties. So,works. It would have been nice if the
fill_valuedefaulted to the input array if that is a scalar...pentschev commentedon Oct 28, 2020
Given that
full_likeis sort of an extension tofill, I guess this was a design decision, asfill_valueis a required argument infull. But I see in the example above how it looks weird, unfortunately I think we don't have a better solution at the moment.mhvk commentedon Oct 28, 2020
@pentschev - yes, no problem! I don't quite think this is worth (slightly) changing the numpy API for.
mhvk commentedon Apr 12, 2021
Update:
like=now is supported in numpy 1.20, so we should gain an override method. Fornp.full, that could look at the fill argument and infer the unit from that.pllim commentedon Jan 25, 2023
Update with astropy 5.2.1 , it no longer throw exception but it also loses unit.
mhvk commentedon Aug 1, 2025
Going to close this as
np.full(shape, q, like=q)is now supported -- though the suggestion above to usenp.full_likeis definitely the best route.