python - 从 xarray 数据集中的闰年中删除闰日

标签 python python-xarray

我在 xarray 数据集中加载了 Netcdf 文件,我想制作没有闰日的每日气候学,即不包含 2 月 29 日。我正在尝试 syntaxDataset.drop 方法对我来说不太直观。这是数据集

print(ds)
>><xarray.Dataset>
Dimensions:        (lat: 1, lev: 1, lon: 720, time: 27133)
Coordinates:
* lon            (lon) float32 -180.0 -179.5 -179.0 ... 178.5 179.0 179.5
* lev            (lev) float32 1.0
* time           (time) datetime64[ns] 2000-01-02T18:00:00 ... 2018-07-30
Dimensions without coordinates: lat
Data variables:
Var1              (time, lev, lon) float32 ...
Var2              (time, lat, lon) float64 ...
Var3              (time, lat, lon) float64 ...

我试过了

ds_N_R.drop(['Var1', 'Var2', 'Var3'], time='2000-02-29')
>>TypeError: drop() got an unexpected keyword argument 'time'
##another approach
ds_N_R.sel(time='2000-02-29').drop(['Var1', 'Var2', 'Var3'])
## gives not the result I intended
<xarray.Dataset>
Dimensions:  (lev: 1, lon: 720, time: 4)
Coordinates:
* lon      (lon) float32 -180.0 -179.5 -179.0 -178.5 ... 178.5 179.0 179.5
* lev      (lev) float32 1.0
* time     (time) datetime64[ns] 2000-02-29 ... 2000-02-29T18:00:00
Data variables:
*empty*

我该如何继续?如果知道是否有一种直接方法可以仅考虑一年中的 365 天来计算每日气候,那就太好了,但我也想知道如何在需要时从特定时间步长中删除数据。

最佳答案

此处使用 drop() 的正确方法是: ds_N_R.drop([np.datetime64('2000-02-29')], dim='time')

但我认为这实际上可以通过索引操作更干净地完成,例如, ds_N_R.sel(time=~((ds_N_R.time.dt.month == 2) & (ds_N_R.time.dt.day == 29)))

关于python - 从 xarray 数据集中的闰年中删除闰日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53376113/

相关文章:

python - 如何将数组中的零转换为Nan?

python - 用于 xarray 的 JAX pytree

python获取最大值xarray的月份

Python 3.5.1 (AMD64) ctypes.ArgumentError : argument 1: <class 'OverflowError' >: int too long to convert

python - Django - 加载静态数据

python - 月份乘以天数怎么算

python - 使用纬度经度坐标从卫星图像获取最近的像素值

python - 在 python 中使用 IMDBpy API 获取剧集的发布日期

python - Pandas - 从每组的最大日期中减去最小日期

python - 查找模式时出现无法散列的类型错误