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

Datetime as coordinaets does not convert back to datetime (returns int) #4880

Closed
feefladder opened this issue Feb 7, 2021 · 6 comments
Closed

Comments

@feefladder
Copy link
Contributor

feefladder commented Feb 7, 2021

What happened:
datetime was in np.datetime64 formet. When converted t datetime.datetime format it returned an int
What you expected to happen:
`to get a datetime returned
Minimal Complete Verifiable Example:

# Put your MCVE code here
import xarray as xr
import numpy as np
import datetime
date_frame = xr.DataArray(dims='time',coords={'time':pd.date_range('2000-01-01',periods=365)},data=np.zeros(365))
print('pandas date range (datetime): ',pd.date_range('2000-01-01',periods=365)[0])
print('dataframe datetime converted to datetime (int): ',date_frame.coords['time'].data[0].astype(datetime.datetime))
print("normal numpy datetime64 converted to datetime (datetime): ",np.datetime64(datetime.datetime(2000,1,1)).astype(datetime.datetime))

output:

pandas date range (datetime):  2000-01-01 00:00:00
dataframe datetime converted to datetime (int):  946684800000000000
normal numpy datetime64 converted to datetime (datetime):  2000-01-01 00:00:00

if converted to int, it also gives different lengths of int :
date_frame:
946684800000000000
946684800000000
normal datetime64^
Anything else we need to know?:

it is also mentioned in this SO thread appears to be a problem in the datetime64....

numpy version 1.20.0
pandas version 1.2.1

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.9 | packaged by conda-forge | (default, Dec 9 2020, 21:08:20)
[GCC 9.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.0-59-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: None
libnetcdf: None

xarray: 0.16.2
pandas: 1.2.1
numpy: 1.20.0
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.6.1
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2021.01.1
distributed: 2021.01.1
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 49.6.0.post20210108
pip: 21.0.1
conda: None
pytest: None
IPython: 7.20.0
sphinx: None

@max-sixty
Copy link
Collaborator

Thanks for clear example. As per @keewis 's label, this does look like an upstream issue.

@keewis
Copy link
Collaborator

keewis commented Mar 25, 2021

yes, I believe this to be a numpy issue:

date_frame.coords['time'].data[0]

returns a numpy.datetime64[ns] object, which, casted to datetime.datetime using astype becomes a int. See numpy/numpy#18363 for the upstream issue.

@feefladder
Copy link
Contributor Author

Yes, I created that issue :), should this then be closed?

@keewis
Copy link
Collaborator

keewis commented Mar 26, 2021

Possibly, if this doesn't require any action from us. @spencerkclark, what do you think?

@spencerkclark
Copy link
Member

Yes I think unfortunately there is not much we can do here, since as @keewis noted, data_frame.coords["time"].data is a NumPy array.

This issue in general is a little tricky since xarray and pandas use nanosecond-precision datetime values, but datetime.datetime objects only have microsecond precision. See also discussion in numpy/numpy#8546, numpy/numpy#12550, and #3256.

@max-sixty
Copy link
Collaborator

Closing as dupe of #3256

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