每年在Xarray中按自定义期间组

发布于 2025-01-19 07:55:26 字数 295 浏览 0 评论 0原文

我正在尝试将xarray.dataset对象分组为一个自定义的5个月,从10月至1月1日,年度频率。这很复杂,因为这个时期越过新年。

我一直在尝试使用该方法

wb_start = temperature.sel(time=temperature.time.dt.month.isin([10,11,12,1]))
wb_start1 = wb_start.groupby('time.year')

,但是这可以预见,这是同一年的一月,而不是+1年。任何帮助将不胜感激!

I'm trying to group an xarray.Dataset object into a custom 5-month period spanning from October-January with an annual frequency. This is complicated because the period crosses New Year.

I've been trying to use the approach

wb_start = temperature.sel(time=temperature.time.dt.month.isin([10,11,12,1]))
wb_start1 = wb_start.groupby('time.year')

But this predictably makes the January month of the same year, instead of +1 year. Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2025-01-26 07:55:26

我以一种有效的方式解决了这一点,尽管将一年的时间添加到一月份之后的几个月中。我的方法基本上将一年的月份增加了10,11,12个,而一年来的数据就适当,然后在重新索引时间数据上进行了一个groupby(年)实例。

wb_start = temperature.sel(time=temperature.time.dt.month.isin([10,11,12,1]))

# convert cftime to datetime
datetimeindex = wb_start.indexes['time'].to_datetimeindex() 
wb_start['time'] = pd.to_datetime(datetimeindex)

# Add custom group by year functionality
custom_year = wb_start['time'].dt.year

# convert time type to pd.Timestamp
time1 = [pd.Timestamp(i) for i in custom_year['time'].values] 

# Add year to Timestamp objects when month is before Jan. (relativedelta does not work from np.datetime64)
time2 = [i + relativedelta(years=1) if i.month>=10 else i for i in time1] 
wb_start['time'] = time2 

#Groupby using the new time index
wb_start1 = wb_start.groupby('time.year')

I fixed this in a somewhat clunk albeit effective way by adding a year to the months after January. My method essentially moves the months 10,11,12 up one year while leaving the January data in place, and then does a groupby(year) instance on the reindexed time data.

wb_start = temperature.sel(time=temperature.time.dt.month.isin([10,11,12,1]))

# convert cftime to datetime
datetimeindex = wb_start.indexes['time'].to_datetimeindex() 
wb_start['time'] = pd.to_datetime(datetimeindex)

# Add custom group by year functionality
custom_year = wb_start['time'].dt.year

# convert time type to pd.Timestamp
time1 = [pd.Timestamp(i) for i in custom_year['time'].values] 

# Add year to Timestamp objects when month is before Jan. (relativedelta does not work from np.datetime64)
time2 = [i + relativedelta(years=1) if i.month>=10 else i for i in time1] 
wb_start['time'] = time2 

#Groupby using the new time index
wb_start1 = wb_start.groupby('time.year')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文