【问题标题】:subset an xarray dataset or data array in the time dimension在时间维度中子集 xarray 数据集或数据数组
【发布时间】:2018-08-23 07:22:48
【问题描述】:

我正在尝试计算 xarray 数据集中时间维度子集的每月气候学。时间使用 datetime64 定义。

如果我想使用整个时间序列,这很好用:

monthly_avr=ds_clm.groupby('time.month').mean(dim='time')

但我真的只想要比 2001 年更大的年份。这些都不起作用:

monthly_avr2=ds_clm.where(ds_clm.time>'2001-01-01').groupby('time.month').mean('time')
monthly_avr3=ds_clm.isel(time=slice('2001-01-01', '2018-01-01')).groupby('time.month').mean('time')

这是我的数据集的样子:

<xarray.Dataset>
Dimensions:       (hist_interval: 2, lat: 192, lon: 288, time: 1980)
Coordinates:
  * lon           (lon) float32 0.0 1.25 2.5 3.75 5.0 6.25 7.5 8.75 10.0 ...
  * lat           (lat) float32 -90.0 -89.057594 -88.11518 -87.172775 ...
  * time          (time) datetime64[ns] 1850-01-31 1850-02-28 1850-03-31 ...
Dimensions without coordinates: hist_interval
Data variables:
EFLX_LH_TOT   (time, lat, lon) float32 0.26219246 0.26219246 0.26219246 ...

有谁知道使用 datetime64 及时子集的正确语法吗?

【问题讨论】:

    标签: python-xarray


    【解决方案1】:

    通常使用sel() 方法在xarray 中按坐标值索引和选择数据。在您的情况下,类似以下示例的内容应该可以工作。

    monthly_avr3 = ds_clm.sel(
        time=slice('2001-01-01', '2018-01-01')).groupby('time.month').mean('time')
    

    使用where() 方法有时也很有用,但对于您的用例,您还需要添加drop=True 选项:

    monthly_avr2 = ds_clm.where(
        ds_clm['time.year'] > 2000, drop=True).groupby('time.month').mean('time')
    

    【讨论】:

    • 太棒了!谢谢乔!仅供参考,第二个选项(使用where)不起作用,但第一个选项起作用了。
    • @aswann - 谢谢你让我诚实。我已经更新了where 示例。
    猜你喜欢
    • 2016-09-07
    • 1970-01-01
    • 2021-10-15
    • 2020-04-17
    • 2020-11-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多