Decoding time and timezones with datetime64[ns]

1,504 views
Skip to first unread message

Ravi Shekhar

unread,
Jul 24, 2015, 2:03:48 PM7/24/15
to xray
Hi,

I'm having some problems with the way xray (and numpy) handle timezones. For example, with this dataset: ftp://ftp.cdc.noaa.gov/Datasets/noaa.ersst/sst.mnmean.v4.nc

    import xray
    fh = xray.open_dataset('sst.mnmean.v4.nc')
    print(fh.time)
--
<xray.DataArray 'time' (time: 1937)>
array(['1854-01-15T00:00:00.000000000Z', '1854-02-15T00:00:00.000000000Z',
       '1854-03-15T00:00:00.000000000Z', ...,
       '2015-03-14T20:00:00.000000000-0400',
       '2015-04-14T20:00:00.000000000-0400',
       '2015-05-14T20:00:00.000000000-0400'], dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 1854-01-15 1854-02-15 1854-03-15 ...
Attributes:
    long_name: Center time of the day
    standard_name: time
    axis: T
    delta_t: 0000-01-00


My computer happens to be set to UTC-4. I'm not sure how to go about telling xray to ignore the local timezone when loading data. This is problematic for two reasons.

1) For files that have dates such as 2014-01-01 00:00:00 encoded in "hours since 1970-01-01", subtracting 4 hours puts the date in the previous month of the previous year. This makes calculating climatologies pointlessly complex.
2) For very long timeseries, dates that straddle the 1970-01-01 boundary have the timezone correction applied after that date, but not before. Also pointlessly complex.

I realize that some of this behavior is in numpy.datetime64, but it makes time handling in xray difficult. Maybe xray could have a switch to return plain netCDF4 datetime objects that are timezone naive? Or alternatively a timezone could be manually specified for datetime64 when loading in xray?

One caveat:
I don't want to have to change my local timezone as seen by the python interpreter in the tzinfo or datetime modules to fix this issue. There are other places in my code where timezones are important.

Cheers,
Ravi

--
ravi.shekhar__at__yale__.edu
PhD Candidate
Department of Geology and Geophysics
Yale University

Stephan Hoyer

unread,
Jul 24, 2015, 2:16:29 PM7/24/15
to Ravi Shekhar, xray
Hi Ravi,

So the good news here is that xray/pandas is actually always treats datetime64 arrays as timezone naive/UTC. All climatology calculations (resample, groupby, etc.) are all done in UTC.

Unfortunately, NumPy converts to datetimes to your local timezone when you print an array, and xray relies on numpy when we print a DataArray. This is a repeated source of confusion that I would like to definitely fix:

If you're interested, help putting together a PR would definitely be a appreciated.

Cheers,
Stephan


--
You received this message because you are subscribed to the Google Groups "xray" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xray-dev+u...@googlegroups.com.
To post to this group, send email to xray...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xray-dev/36de684c-9053-4254-997d-5ed423010e4d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages