Error saving xr.Dataset with timezone aware time index to netcdf format.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the failure with the timezone-aware time index described in the issue and the pds.to_netcdf call. Compare it with the working integer and pd.to_datetime conversions, then trace the time-coordinate serialization path. Done means a dataset with a timezone-aware time index can be saved without the reported error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, pandas, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100