【问题标题】:Select xarray dataset based on month根据月份选择 xarray 数据集
【发布时间】:2020-07-02 14:17:36
【问题描述】:

我有包含以下信息的 xarray 数据集:

Coordinates:
lat: float64 (192)
lon: float64 (288)
time: object (1200) (monthly data)

Data Variables:
tas: (time, lat, lon)

现在我想要特定月份的 tas 值,例如,我想要包含一月份所有记录的新数据集。

输出数据集如下所示:

Coordinates:
lat: float64 (192)
lon: float64 (288)
time: object (100) (monthly data of January)

Data Variables:
tas: (time, lat, lon)

我尝试过类似这样的方法,我以前用过:

jan = pd.date_range(start='1979-01-01', periods=41, freq='AS-JAN').date.tolist()
gs_jan = gs.sel(time = jan)

但这不适用于我的情况,因为我的日期是 0001-0100 年,而 pandas 不支持该范围内的日期!

【问题讨论】:

标签: python pandas numpy datetime python-xarray


【解决方案1】:

一般要分析这样的时间序列数据,你要遵循 group-split-apply 方法,使用 xarray 的 da.groupby() 方法 (http://xarray.pydata.org/en/stable/groupby.html)。

在你的情况下,我建议尝试:

# Use .groupby('time.month') to organize the data into months
# then use .groups to extract the indices for each month
month_idxs=gs.groupby('time.month').groups

# Extract the time indices corresponding to all the Januarys 
jan_idxs=month_idxs[1]

# Extract the january months by selecting 
# the relevant indices
gs_jan=gs.isel(time=jan_idxs)

希望这会有所帮助!

【讨论】:

  • 完美运行!太感谢了。 bdw 这里的month_idxs(第一行代码!)有什么用?
  • 糟糕!下一行是jan_idxs=month_idxs[1],但我忘了。哈哈。将编辑 :) 乐于助人!
猜你喜欢
  • 2017-03-09
  • 2018-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 2018-03-02
  • 2023-03-30
相关资源
最近更新 更多