Open jerabaul29 opened 9 months ago
That seems to be some tricky issue with timezones.
The issue already manifests in the DataArray (Variable) creation. The given list of timezone aware datetimes is converted into pandas._libs.tslibs.timestamps.Timestamp
(wrapped as numpy 'O'). This happens in https://github.com/pydata/xarray/blob/c9ba2be2690564594a89eb93fb5d5c4ae7a9253c/xarray/core/variable.py#L213
In the further course there is no way of conversion to some numpy datetime64[ns] or similar to correctly serialize.
Same happens if you wrap your data as numpy array. This only works when stripping the tzinfo from the array (either by not adding tzinfo in the first place or casting to a proper type):
times = np.array(times).astype("<M8[ns]")
I'm not versed in that special part of DataArray/Variable creation with timezone aware datetimes and how to properly solve that issue. Hoping that others have more insight here, @spencerkclark?
What happened?
I tried to save an xarray dataset with datetimes as data for its time dimension to a nc file with
to_netcdf
and got the errorValueError: unable to infer dtype on variable 'time'; xarray cannot serialize arbitrary Python objects
.What did you expect to happen?
I expected xarray to automatically detect these were datetimes, and convert them to whatever format xarray likes to work with internally to dump it into a CF compatible file, following what is described at https://github.com/pydata/xarray/issues/2512 .
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
The example is available as a notebook viewable at:
https://github.com/jerabaul29/public_bug_reports/blob/main/xarray/2024_01_24/xarray_and_datetimes.ipynb
Environment