xarray
xarray copied to clipboard
min and max methods changing dtype to float64
I'm working with datasets that have coordinates with lots of decimal places. As I don't need this I reassign cords and transform the dtype to float32. For calculating the extent of the dataset a standard procedure is to use the min() and max() methods but using them over the float32 coordinates yields the float64 values which leads to misaligned coordinates.
Minimum reproducible example:
import xarray as xr
import numpy as np
da = xr.DataArray(
data=np.array([1.2, 2.2, 3.3], dtype=np.float32),
coords={"x": [1.345, 2.345, 3.345]},
dims=("x",),
)
print(da[0].dtype) # dtype('float32')
print(da.min().dtype) # dtype('float64')
print(type(np.min(da.values))) # dtype('float32')
Is there a reason for this?
I'm not sure about this, but I think the reason is that we're using bottleneck
to speed up the computation, which returns a python float
object for scalars. To be able to wrap it, xarray
will convert it to a 0d array, which by default has a dtype of float64
.
np.{min,max}
returns np.float32
instances, so the dtype information is not lost with that.
To work around that, you can disable bottleneck:
xr.set_options(use_bottleneck=False)