选择基于月份的xarray数据集

2024-05-29 03:07:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有包含以下信息的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年,熊猫不支持该范围内的日期


Tags: 数据gsdatadateobjecttimevariablesjan
1条回答
网友
1楼 · 发布于 2024-05-29 03:07:38

一般来说,为了分析这样的时间序列数据,您希望使用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)

希望这有帮助

相关问题 更多 >

    热门问题