您可以按月中的日期对 XArray 数据数组进行分组吗?

Can you Group XArray Data Array by Day of Month?

如果 xarray 数据集中有一个变量,其时间维度在某个多年时间跨度内具有每日值

2017-01-01 ... 2018-12-31,然后可以按月或一年中的某天对数据进行分组,使用

.groupby("time.month") or .groupby("time.dayofyear")

有没有一种方法可以有效地按月中的日期对数据进行分组,例如,如果我想计算每个月 21 日的平均值?

我不确定,但也许你可以这样做:

import datetime

x = datetime.datetime.now()

day = x.strftime("%d")
month = x.strftime("%m")
year = x.strftime("%Y")

.groupby(month) or .groupby(year)

请参阅 DateTimeAccessor helper object. For more info, you can also check out the xarray docs on Working with Time Series Data: Datetime Components, which in turn refers to the pandas docs on date/time components 上的 xarray 文档。

您正在寻找 day。不幸的是,pandas 和 xarray 都简单地将 .dt.day 描述为指的是“日期时间的日子”,这并不是特别有用。但是如果你看一下 python 的原生 datetime.Date.day 定义,你会看到更具体的:

date.day
Between 1 and the number of days in the given month of the given year.

所以,简单地说

da.groupby("time.day")

应该成功!