微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xarray - 在时区感知时间维度上每小时到每天重新采样

如何解决Xarray - 在时区感知时间维度上每小时到每天重新采样

我有一个名为 da_ffdi_sel_utc 的 xarray DataArray,维度为 timelatitudelongitude

 xarray.DataArray 'FFDI' (time: 48   latitude: 2   longitude: 2)

 longitude (longitude) float32         145.34322 145.38107
 latitude  (latitude)  float32         -37.39728 -37.361225
 time      (time)      datetime64[ns]  1972-01-20T00:00:00,1972-01-20T01:00:00,1972-01-20T02:00:00 ... 1972-01-21T23:00:00

由于 time 坐标采用 UTC,而我需要按 24 小时重新采样 FFDI 值并在澳大利亚东部标准时间 (UTC+10) 中获取 max();因此重新采样的 time 坐标需要为 1972-01-20、1972-01-21 和 1972-01-22。

我尝试过的是:

# Create a timezone aware datatime
ds_time_austtime = pd.date_range('1972-01-20T11:00:00.000000000','1972-01-22T11:00:00.000000000',freq='1H',closed='left',name="time",tz='Australia/Melbourne')  #timezone aware
print(ds_time_austtime)

    DatetimeIndex(['1972-01-20 11:00:00+11:00','1972-01-20 12:00:00+11:00','1972-01-20 13:00:00+11:00','1972-01-20 14:00:00+11:00','1972-01-20 15:00:00+11:00','1972-01-20 16:00:00+11:00','1972-01-20 17:00:00+11:00','1972-01-20 18:00:00+11:00','1972-01-20 19:00:00+11:00','1972-01-20 20:00:00+11:00','1972-01-20 21:00:00+11:00','1972-01-20 22:00:00+11:00','1972-01-20 23:00:00+11:00','1972-01-21 00:00:00+11:00','1972-01-21 01:00:00+11:00','1972-01-21 02:00:00+11:00','1972-01-21 03:00:00+11:00','1972-01-21 04:00:00+11:00','1972-01-21 05:00:00+11:00','1972-01-21 06:00:00+11:00','1972-01-21 07:00:00+11:00','1972-01-21 08:00:00+11:00','1972-01-21 09:00:00+11:00','1972-01-21 10:00:00+11:00','1972-01-21 11:00:00+11:00','1972-01-21 12:00:00+11:00','1972-01-21 13:00:00+11:00','1972-01-21 14:00:00+11:00','1972-01-21 15:00:00+11:00','1972-01-21 16:00:00+11:00','1972-01-21 17:00:00+11:00','1972-01-21 18:00:00+11:00','1972-01-21 19:00:00+11:00','1972-01-21 20:00:00+11:00','1972-01-21 21:00:00+11:00','1972-01-21 22:00:00+11:00','1972-01-21 23:00:00+11:00','1972-01-22 00:00:00+11:00','1972-01-22 01:00:00+11:00','1972-01-22 02:00:00+11:00','1972-01-22 03:00:00+11:00','1972-01-22 04:00:00+11:00','1972-01-22 05:00:00+11:00','1972-01-22 06:00:00+11:00','1972-01-22 07:00:00+11:00','1972-01-22 08:00:00+11:00','1972-01-22 09:00:00+11:00','1972-01-22 10:00:00+11:00'],dtype='datetime64[ns,Australia/Melbourne]',name='time',freq='H')


# Create a new Dataset with the same Lat and Lon and FFDI as da_ffdi_sel_utc but time is from the new data as above
ds_ffdi_sel_aest = xr.Dataset()
ds_ffdi_sel_aest.coords['longitude'] = ('longitude',da_ffdi_sel_utc['longitude'])
ds_ffdi_sel_aest.coords['latitude'] = ('latitude',da_ffdi_sel_utc['latitude'])
ds_ffdi_sel_aest.coords['time'] = ('time',ds_time_austtime)
ds_ffdi_sel_aest['FFDI'] = (('time','latitude','longitude'),da_ffdi_sel_utc.values)

print(ds_ffdi_sel_aest['time'])
array([64713600000000000,64717200000000000,64720800000000000,64724400000000000,64728000000000000,64731600000000000,64735200000000000,64738800000000000,64742400000000000,64746000000000000,64749600000000000,64753200000000000,64756800000000000,64760400000000000,64764000000000000,64767600000000000,64771200000000000,64774800000000000,64778400000000000,64782000000000000,64785600000000000,64789200000000000,64792800000000000,64796400000000000,64800000000000000,64803600000000000,64807200000000000,64810800000000000,64814400000000000,64818000000000000,64821600000000000,64825200000000000,64828800000000000,64832400000000000,64836000000000000,64839600000000000,64843200000000000,64846800000000000,64850400000000000,64854000000000000,64857600000000000,64861200000000000,64864800000000000,64868400000000000,64872000000000000,64875600000000000,64879200000000000,64882800000000000],dtype=object)

# Do resample by time and get max daily FFDI from hourly FFdis
da_ffdi_sel_aest_daily_max = ds_ffdi_sel_aest['FFDI'].resample(time='1D').max('time')

然后我遇到了以下错误

~\Anaconda3\envs\geo_scipy_3\lib\site-packages\pandas\core\groupby\groupby.py in __init__(self,obj,keys,axis,level,grouper,exclusions,selection,as_index,sort,group_keys,squeeze,observed,mutated,dropna)
    531                 observed=observed,532                 mutated=self.mutated,--> 533                 dropna=self.dropna,534             )
    535 

~\Anaconda3\envs\geo_scipy_3\lib\site-packages\pandas\core\groupby\grouper.py in get_grouper(obj,key,validate,dropna)
    683     # a passed-in Grouper,directly convert
    684     if isinstance(key,Grouper):
--> 685         binner,obj = key._get_grouper(obj,validate=False)
    686         if key.key is None:
    687             return grouper,[],obj

~\Anaconda3\envs\geo_scipy_3\lib\site-packages\pandas\core\resample.py in _get_grouper(self,validate)
   1440     def _get_grouper(self,validate: bool = True):
   1441         # create the resampler and return our binner
-> 1442         r = self._get_resampler(obj)
   1443         r._set_binner()
   1444         return r.binner,r.grouper,r.obj

~\Anaconda3\envs\geo_scipy_3\lib\site-packages\pandas\core\resample.py in _get_resampler(self,kind)
   1433 
   1434         raise TypeError(
-> 1435             "Only valid with DatetimeIndex,"
   1436             "timedeltaIndex or Periodindex,"
   1437             f"but got an instance of '{type(ax).__name__}'"

TypeError: Only valid with DatetimeIndex,timedeltaIndex or Periodindex,but got an instance of 'Index'

我不确定是否支持在 xarray 中使用时区感知时间坐标进行重新采样。否则,这样做的最佳做法是什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。