-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
assign_coords with datetime64[us] changes dtype to datetime64[ns] #4427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks @andrewpauling -- I do think there's a bug here, but this issue happens to be more complicated than it might seem on the surface :). Xarray standardizes around nanosecond precision for Addressing this fully would be a challenge (we've discussed this at times in the past). It was concluded that for dates outside the representable range that This is a long way of saying, without a fair amount of work (i.e. addressing this issue upstream in pandas) xarray is unlikely to relax its approach for the precision of However, the fact that your example silently results in non-sensical times should be considered a bug; instead, following pandas, I would argue we should raise an error if the dates cannot be represented with nanosecond precision. |
Thanks @spencerkclark for the response, that makes sense. I have been able to get what I needed to work using I agree it would be nice if an error was raised when dates can't be represented, would this be difficult to implement? I have been hoping to contribute to some open-source projects so if it's not too complex I'd be happy to tackle it, and if you have any advice on where to start with this problem that would be great. |
That would be great @andrewpauling! I think this is the relevant code in xarray: xarray/xarray/core/variable.py Lines 244 to 250 in 1155f56
I want to say arguably we could use the I agree this casting behavior is a bit surprising. If we wanted to be a little more transparent, we could also warn when attempting to cast non-nanosecond-precision |
What happened:
When using xr.DataArray.assign_coords() to assign a new coordinate to the time dimension that is an array with dtype datetime64[us], after assigning, the dtype is datetime64[ns], resulting in the wrong dates, since the dates I am using are outside the valid range for the [ns] units.
What you expected to happen:
Preserve dtype of array when assigning as a coordinate.
Minimal Complete Verifiable Example:
which gives for the original array:
and for the array after assigning:
Anything else we need to know?:
Environment:
Output of xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.7.8 | packaged by conda-forge | (default, Jul 31 2020, 02:37:09)
[Clang 10.0.1 ]
python-bits: 64
OS: Darwin
OS-release: 18.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.7.3
xarray: 0.16.0
pandas: 1.1.0
numpy: 1.19.1
scipy: 1.4.1
netCDF4: 1.5.3
pydap: installed
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.21.0
distributed: 2.22.0
matplotlib: 3.1.2
cartopy: 0.17.0
seaborn: None
numbagg: None
pint: None
setuptools: 49.3.1.post20200810
pip: 20.2.2
conda: None
pytest: None
IPython: 7.17.0
sphinx: 3.2.0
The text was updated successfully, but these errors were encountered: