简体   繁体   中英

use time without date as one dimension in xarray

I have an xarray.DataArray that I successfully set one dim as time of one day.

tmp.dims
('ITEM', 'DATE', 'TIME', 'CODE')
tmp.TIME
<xarray.DataArray 'TIME' (TIME: 15)>
array([datetime.time(14, 15), datetime.time(14, 16), datetime.time(14, 17),
       datetime.time(14, 18), datetime.time(14, 19), datetime.time(14, 20),
       datetime.time(14, 21), datetime.time(14, 22), datetime.time(14, 23),
       datetime.time(14, 24), datetime.time(14, 25), datetime.time(14, 26),
       datetime.time(14, 27), datetime.time(14, 28), datetime.time(14, 29)],
      dtype=object)
tmp.TIME.values[0]
datetime.time(14, 15)

but I cannot save this xarray do to the this error:

tmp.to_netcdf('/sdata/user/tsu/tmp/srpd.nc')
        *** ValueError: unable to infer dtype on variable 'TIME'; xarray cannot serialize arbitrary Python objects

Is it wrong to use datetime.time in this way?

Insteads of using lists of python datetime objects, use timedelta arrays from numpy or pandas. See the pandas docs on working with time series data .

For data indexed by hour of day, but not a specific date, I'd recommend using pd.to_timedelta , as in this example:

In [9]: da = xr.DataArray(
   ...:     np.ones(16).reshape(4, 4),
   ...:     dims=['x', 'hour'],
   ...:     coords=[range(4), pd.to_timedelta(range(4), unit='h')],
   ...: )
   ...:

In [10]: da
Out[10]:
<xarray.DataArray (x: 4, hour: 4)>
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
Coordinates:
  * x        (x) int64 0 1 2 3
  * hour     (hour) timedelta64[ns] 00:00:00 01:00:00 02:00:00 03:00:00

In [11]: da.hour
Out[11]:
<xarray.DataArray 'hour' (hour: 4)>
array([             0,  3600000000000,  7200000000000, 10800000000000],
      dtype='timedelta64[ns]')
Coordinates:
  * hour     (hour) timedelta64[ns] 00:00:00 01:00:00 02:00:00 03:00:00

Note that this can be written to netCDF without issue:

In [12]: da.to_dataset(name='myarr').to_netcdf('sample.nc')

In [13]: xr.open_dataset('sample.nc')
Out[13]:
<xarray.Dataset>
Dimensions:  (x: 4, hour: 4)
Coordinates:
  * x        (x) int64 0 1 2 3
  * hour     (hour) timedelta64[ns] 00:00:00 01:00:00 02:00:00 03:00:00
Data variables:
    myarr    (x, hour) float64 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM