Error saving xr.Dataset with timezone aware time index to netcdf format.
When I try to save a xr.Dataset that was created from a pandas dataframe with tz-aware time index ( see #3291) - xarray converts the time index into a int64 nanosecs
For example, this is what the converted dataset looks like:
<xarray.Dataset>
Dimensions: (symbol: 3196, time: 4977)
Coordinates:
* time (time) object 946933200000000000 ... 1566334800000000000
* symbol (symbol) int64 0 1 2 3 4 5 6 ... 3189 3190 3191 3192 3193 3194 3195
Data variables:
var_0 (time, symbol) float32 nan 4301510000.0 nan nan ... nan nan nan nan
var_1 (time, symbol) object nan False nan nan nan ... nan nan nan nan nan
var_2 (time, symbol) float32 nan 475.0 nan nan nan ... nan nan nan nan
var_3 (time, symbol) float32 nan 475.0 nan nan nan ... nan nan nan nan
var_5 (time, symbol) float32 nan 475.9 nan nan nan ... nan nan nan nan
var_6 (time, symbol) float32 nan 475.9 nan nan nan ... nan nan nan nan
var_7 (time, symbol) float32 nan 429.5 nan nan nan ... nan nan nan nan
var_8 (time, symbol) float32 nan 429.5 nan nan nan ... nan nan nan nan
var_10 (time, symbol) float32 nan -0.06736842 nan nan ... nan nan nan nan
var_11 (time, symbol) float32 nan 0.05085102 nan nan ... nan nan nan nan
var_12 (time, symbol) float32 nan 0.029103609 nan nan ... nan nan nan nan
var_13 (time, symbol) float32 nan 0.048769474 nan nan ... nan nan nan nan
var_14 (time, symbol) float32 nan 442.9 nan nan nan ... nan nan nan nan
var_15 (time, symbol) float32 nan 442.9 nan nan nan ... nan nan nan nan
var_16 (time, symbol) float32 nan nan nan nan nan ... nan nan nan nan nan
var_17 (time, symbol) float32 nan nan nan nan nan ... nan nan nan nan nan
var_18 (time, symbol) float32 nan nan nan nan nan ... nan nan nan nan nan
var_19 (time, symbol) float32 nan nan nan nan nan ... nan nan nan nan nan
var_20 (time, symbol) float32 nan nan nan nan nan ... nan nan nan nan nan
var_21 (time, symbol) float32 nan 9501900.0 nan nan ... nan nan nan nan
var_22 (time, symbol) float32 nan 9501900.0 nan nan ... nan nan nan nan
Now when I try to save this dataset using
pds.to_netcdf( ... )
I get the following error:

Dropping into pdb when this error is hit - it looks like the problem is with the time index.

After converting the time index into a regular int index by:
pds = pds.assign_coords(time=np.arange( len( pds.time )) )
pds.to_netcdf( ... )
this works OK.
And this also works !!
pds = pds.assign_coords(time=pd.to_datetime( pds.time ) )
pds.to_netcdf( ... )
Note pd.to_datetime(pds.time) drops the timezone from the index - so the issue is very much about saving tz-aware time indices.
Any ideas on what I can do about this ?
Thanks! -firdaus
I just had this issue too @keewis, I think you're right. Once I remove timezone information from my datetime object it works just fine. Looking at https://github.com/pydata/xarray/issues/2512 it looks like the change which is required might be in xarray/core/variable.py (https://github.com/pydata/xarray/blob/master/xarray/core/variable.py#L165). I'll have a look and see if I can make it work
Just a ping on this, was wondering if this ever got resolved. I still cannot assign tz-aware datetime64ns-dtype objects as the time coordinate variable.
We'll have to build it. Looks like CF now supports the recording of time zone offsets: https://cfconventions.org/Data/cf-conventions/cf-conventions-1.12/cf-conventions.html#time-coordinate-units