简体   繁体   English

使用没有日期的时间作为 xarray 中的一维

[英]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.我有一个 xarray.DataArray,我成功地将一个暗淡设置为一天的时间。

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:但我无法将此 xarray 保存到此错误中:

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?这样使用 datetime.time 有错吗?

Insteads of using lists of python datetime objects, use timedelta arrays from numpy or pandas.而不是使用 python datetime时间对象的列表,而是使用来自timedelta或 Z3A43B4F883925D94022CFAFFA2 的 timedelta arrays See the pandas docs on working with time series data .请参阅有关使用时间序列数据的 pandas 文档。

For data indexed by hour of day, but not a specific date, I'd recommend using pd.to_timedelta , as in this example:对于按一天中的小时而不是特定日期索引的数据,我建议使用pd.to_timedelta ,如下例所示:

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:请注意,这可以毫无问题地写入 netCDF:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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