-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
to_netcdf() fails because of datetime encoding #2512
Comments
I think this is a bug. The error message in particular makes things confusing. The problem stems from this line, which results in an array of dtype object (see the explanation below): dts = np.array([dt + timedelta(days=x) for x in range(10)]) As a temporary workaround, if you keep dts = [dt + timedelta(days=x) for x in range(10)] By default NumPy will cast a list of
If you specify an array of dtype object as a coordinate, xarray currently has some logic that requires that it be cast to a generic
The code where this happens is here: Lines 69 to 73 in b2a377f
In practice, in the event that an object type array contains datetime.datetime objects, this should probably convert them to np.datetime64 instead and return a pandas.DatetimeIndex . This would be more consistent with how xarray currently handles passing an object array of datetime.datetime objects as the data argument to a DataArray; there it converts things automatically:
Xarray's logic to encode dates (i.e. what is used when saving datetime data to files) requires that dates are either of type The error message actually results from the dates being converted to |
I agree, this is definitely a bug. We do have logic that is supposed to automatically convert datetime object arrays into datetime64, but for some reason it isn't being triggered here: xarray/xarray/core/variable.py Lines 182 to 184 in 2a46913
|
Simple example:
Problem description
When I run the above, I get the following error when executing the last line:
ValueError: unsupported dtype for netCDF4 variable: datetime64[ns]
The documentation indicates that datetime and datetime64 objects are both supported by xarray and should write to netcdf just fine when supplied "units" for encoding (this code fails with or without the encoding lines). Any Idea when is going wrong here?
Output of
xr.show_versions()
xarray: 0.10.9
pandas: 0.20.3
numpy: 1.13.1
scipy: 0.19.1
netCDF4: 1.4.2
h5netcdf: 0.5.0
h5py: 2.8.0
Nio: None
zarr: None
cftime: 1.0.1
PseudonetCDF: None
rasterio: None
iris: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.16.0
distributed: 1.20.1
matplotlib: 2.1.0
cartopy: None
seaborn: 0.8.0
setuptools: 27.2.0
pip: 9.0.1
conda: 4.5.11
pytest: 3.1.3
IPython: 6.1.0
sphinx: 1.6.2
The text was updated successfully, but these errors were encountered: