使用 xarray DataArray.resample() 方法重采样 DataArray



我正在尝试使用 xarray 中的 DataArray.resample(( 将 DataArray 从每小时输出重新采样到每天输出。

首先,我尝试了以下方法:

pm25.resample(dim='Time', freq="1D", how='mean')

这给了我:

TypeError: resample() no longer supports the `how` or `dim` arguments. 
Instead call methods on resample objects, e.g., data.resample(time='1D').mean()

因此,我尝试了以下方法:

pm25_daily = pm25.resample(Time='1D').mean()

但是,我收到以下错误:

Traceback (most recent call last):
File "/Users/jacob/opt/anaconda3/envs/WRF-1/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3319, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-140-4ba4bc53a1c2>", line 1, in <module>
pm25.resample(Time='1D').mean(dim='Time')
File "/Users/jacob/opt/anaconda3/envs/WRF-1/lib/python3.7/site-packages/xarray/core/common.py", line 1039, in resample
if isinstance(self.indexes[dim_name], CFTimeIndex):
File "/Users/jacob/opt/anaconda3/envs/WRF-1/lib/python3.7/site-packages/xarray/core/indexes.py", line 36, in __getitem__
return self._indexes[key]
KeyError: 'Time'

我还尝试了以下方法

pm25_daily = pm25.resample(Time='1D').mean(dim='Time)

我得到了同样的错误。下面是我的数据数组的外观以及维度和坐标。

<xarray.DataArray 'PM2_5_DRY' (Time: 283, bottom_top: 50, south_north: 115, west_east: 115)>
array([[[[1.163326, ..., 1.10779 ],
...,
[1.145038, ..., 1.167824]],
...,
[[0.090804, ..., 0.090798],
...,
[0.090985, ..., 0.091001]]],
...,
[[0.024746, ..., 0.02613 ],
...,
[0.025582, ..., 0.026747]]]], dtype=float32)
Coordinates:
XLAT     (Time, south_north, west_east) float32 ...
XLONG    (Time, south_north, west_east) float32 ...
XTIME    (Time) datetime64[ns] ...
Dimensions without coordinates: Time, bottom_top, south_north, west_east
Attributes:
FieldType:    104
MemoryOrder:  XYZ
description:  pm2.5 aerosol dry mass
units:        ug m^-3
stagger:

数据数组的维度中包含"时间"。我哪里出错了?

谢谢!

根据我的 DataArray,"Time"维度既不是维度坐标,也不是日期时间对象。

我将 XTIME 坐标作为我的维度坐标与原始的"时间"维度交换。

pm25 = pm25.swap_dims({'Time': 'XTIME'})

然后其他一切都正常。

最新更新