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
Should we be more clever about incompatible dtypes? (numpy is not)
a. By raising an error?
b. By casting arr?
c. Add an argument for the behavior on incompatible dtypes?
If not - is the numpy error explicit enough?
Should we just remove the example or should we replace it with something like arr.pad(x=1, constant_values=1.5) and mention that 1.5 is cast to 1
Currently there are 3 different behaviors, depending on constant_values
importxarrayasxrimportnumpyasnparr=xr.DataArray([5, 6, 7], coords=[("x", [0, 1, 2])])
arr.pad(x=1, constant_values=np.nan)
# ValueError: cannot convert float NaN to integerarr.pad(x=1, constant_values=None)
# casts arr to floatarr.pad(x=1, constant_values=1.5)
# casts constant_values to int
Hi, we had a similar discussion in de #3596, xarray makes a distinction between np.nan and xarray.dtypes.NaN. The current behaviour is consistent with that of other xarray functions such as shift. Though, I am personally not a big fan of this distinction.
Hmm, so numpy changed its behaviour? Then this example, should probably also fail in numpy 1.20.
On a side note: I am not a big fan of the example in the doctest, it displays an edge case, which is not unique to pad.
I think the nicest solution would be to make the usage xarray.dtypes.NaN and np.nan equivalent. But this would require changes in all xarray functions that take some kind of fill_value.
replace the example with arr.pad(x=1, constant_values=1.23456789) and mention that the float is cast to int (or would you leave the example away?)
open a new issue to discuss the issue of assigning float to int
I agree, I think that would be a good solutions for now, I think replacing the example is fine. Maybe we could even open a new issue, to discuss how xarray functions handle np.nan.
Is dask.array.pad gonna handle casting the same way? It would be strange if the cast to float happens, depending on the underlying array type. But that discussion should probably happen in the newly opened issue ;-)
Activity
mathause commentedon Feb 4, 2021
@mark-boer @dcherian @max-sixty
a. By raising an error?
b. By casting arr?
c. Add an argument for the behavior on incompatible dtypes?
arr.pad(x=1, constant_values=1.5)and mention that1.5is cast to1Currently there are 3 different behaviors, depending on
constant_valuesxarray/xarray/core/dataarray.py
Lines 3883 to 3892 in 5735e16
mark-boer commentedon Feb 4, 2021
Hi, we had a similar discussion in de #3596, xarray makes a distinction between
np.nanandxarray.dtypes.NaN. The current behaviour is consistent with that of other xarray functions such asshift. Though, I am personally not a big fan of this distinction.Check e.g. this comment: #3596 (comment)
The example I posted in this comment:
Hmm, so numpy changed its behaviour? Then this example, should probably also fail in numpy 1.20.
On a side note: I am not a big fan of the example in the doctest, it displays an edge case, which is not unique to
pad.I think the nicest solution would be to make the usage
xarray.dtypes.NaNandnp.nanequivalent. But this would require changes in all xarray functions that take some kind offill_value.mathause commentedon Feb 4, 2021
Thanks for the other examples. Yes these now also raise an error with numpy 1.20. What still does not raise is the following
(because this gets converted to
da.variable._data[0:1, 0:1] = np.array([np.nan])(approximately).My suggestion is:
arr.pad(x=1, constant_values=1.23456789)and mention that the float is cast to int (or would you leave the example away?)mark-boer commentedon Feb 4, 2021
I agree, I think that would be a good solutions for now, I think replacing the example is fine. Maybe we could even open a new issue, to discuss how xarray functions handle
np.nan.Is
dask.array.padgonna handle casting the same way? It would be strange if the cast to float happens, depending on the underlying array type. But that discussion should probably happen in the newly opened issue ;-)