Skip to content

Error saving xr.Dataset with timezone aware time index to netcdf format. #3320

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

Open
fjanoos opened this issue Sep 18, 2019 · 3 comments
Open
Labels

Comments

@fjanoos
Copy link

fjanoos commented Sep 18, 2019

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:
image

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

image

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

@keewis keewis added the bug label Mar 29, 2020
@leifdenby
Copy link
Contributor

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 #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

@andersy005 andersy005 changed the title Error saving xr.Dataset to netcdf format. Error saving xr.Dataset with timezone aware time index to netcdf format. Jan 17, 2022
@kasra-keshavarz
Copy link

kasra-keshavarz commented Apr 17, 2025

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.

@dcherian
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants