Skip to content
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

Xarray ignores the underlying unit of "datetime64" types. #6591

Closed
alvarosg opened this issue May 11, 2022 · 3 comments
Closed

Xarray ignores the underlying unit of "datetime64" types. #6591

alvarosg opened this issue May 11, 2022 · 3 comments

Comments

@alvarosg
Copy link

What happened?

xarray always converts "datetime64" types to nanoseconds.

xarray also silently does nothing when calling .astype('datetime64[ms]') on an datetime64 type.

What did you expect to happen?

I would expect the same behavior as in numpy (respect units, as shown in the reproducer). Or at least raise an error, when calling astype with a different unit.

Minimal Complete Verifiable Example

import numpy as np
import xarray as xa

# As expected.
time_array = np.array(["2000-01-01"]).astype('datetime64[ms]')
print(time_array.dtype)  # datetime64[ms]
print(time_array.astype('datetime64[s]').dtype)  # datetime64[s]

# Always end up with "ns".
time_data_array = xa.DataArray(data=time_array, dims=("time"))
print(time_data_array.dtype)  # datetime64[ns]
print(time_data_array.astype('datetime64[s]').dtype)  # datetime64[ns]


### MVCE confirmation

- [X] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- [X] Complete example — the example is self-contained, including all data and the text of any traceback.
- [X] Verifiable example — the example copy & pastes into an IPython prompt or [Binder notebook](https://mybinder.org/v2/gh/pydata/xarray/main?urlpath=lab/tree/doc/examples/blank_template.ipynb), returning the result.
- [X] New issue — a search of GitHub Issues suggests this is not a duplicate.

### Relevant log output

_No response_

### Anything else we need to know?

_No response_

### Environment

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.13 (default, Apr 24 2022, 01:04:09) 
[GCC 7.5.0]
python-bits: 64
OS: Linux
OS-release: 5.4.188+
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.0
libnetcdf: 4.7.4

xarray: 0.18.2
pandas: 1.3.5
numpy: 1.21.6
scipy: 1.4.1
netCDF4: 1.5.8
pydap: None
h5netcdf: None
h5py: 3.1.0
Nio: None
zarr: None
cftime: 1.6.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.4
dask: 2.12.0
distributed: 1.25.3
matplotlib: 3.2.2
cartopy: None
seaborn: 0.11.2
numbagg: None
pint: None
setuptools: 57.4.0
pip: 21.1.3
conda: None
pytest: 3.6.4
IPython: 5.5.0
sphinx: 1.8.6

</details>
@alvarosg alvarosg added bug needs triage Issue that has not been reviewed by xarray team member labels May 11, 2022
@dcherian dcherian added topic-documentation topic-error reporting and removed bug needs triage Issue that has not been reviewed by xarray team member labels May 11, 2022
@dcherian
Copy link
Contributor

I agree. We should document this and raise an error.

@SimonHeybrock
Copy link

Note duplicate (or related): #5750

@kmuehlbauer
Copy link
Contributor

Fixed on main with #9618.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants