Skip to content
Advertisement

Where does xarrays time.hour start and end?

I am not sure where xarray starts and ends the hour. For example: When I get a value for 1 o’clock, are those values form 00:00-01:00 or from 00:30-01:30 or from 01:00-02:00?

In my specific case I have datas form several year token every minute and I need to know what exact timeslice the mean is when its plotted at 1 o’clock for example

ds_mean = ds_all.groupby("time.hour").mean()

Advertisement

Answer

In datetime indexes, xarray uses pandas DatetimeIndex objects under the hood (for the most part; xarray also has a CFTimeIndex option which implements a wider variety of calendars, but works in the same way when it comes to this question). Note that python does not have a concept of continuous time – all time is discrete in increments of (at most) nanosecond precision. So there’s no averaging window that applies here.

You can test how threshold values are treated pretty easily by creating a time series out of the few nanoseconds around Jan 1, 2020:

In [4]: time_range = (
   ...:     pd.date_range('2020-01-01', freq='ns', periods=3)
   ...:     - pd.Timedelta(1, 'ns')
   ...: )
   ...:
   ...: time_range
Out[4]:
DatetimeIndex(['2019-12-31 23:59:59.999999999',
                         '2020-01-01 00:00:00',
               '2020-01-01 00:00:00.000000001'],
              dtype='datetime64[ns]', freq='N')


In [5]: time_range.hour
Out[5]: Int64Index([23, 0, 0], dtype='int64')

So you can see that down to the nanosecond level, the hour, day, year, etc all change to the next one at the zero value (e.g. 12:59:59.9999 has the hour 12, and 1:00:00.0 has the hour 1).

Generally, xarray, pandas, and numpy all borrow from the python datetime.datetime terminology and conventions.

Advertisement