You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python xarray DataArray按日历年季度而非季节重采样求均值问题

解决方法

你得到气象季节分组的核心原因是使用了xarray内置的time.season属性,该属性固定对应DJF(12-2月)、MAM(3-5月)、JJA(6-8月)、SON(9-11月)的气象季节划分,和你需要的日历年季度规则不一致,按以下方案调整即可:

场景1:需要得到逐年的日历年季度均值(即输出1991Q1、1991Q2...2020Q4共120个时间点的结果)

重采样时明确指定季度起始月份为1月,避免环境默认值差异,代码调整为:

# QS-JAN代表季度起始于1月,正好对应Q1=1-3月、Q2=4-6月的划分规则
result = ((wind_abs * month_length).resample(time='QS-JAN').sum() / 
          month_length.resample(time='QS-JAN').sum())

此时输出的result的time坐标即为每个日历年季度的起始日期,已经是你需要的逐季度均值结果。

场景2:需要得到1991-2020年所有同季度的均值(即输出Q1、Q2、Q3、Q4共4个分组的结果,对应你原来quarter_means的输出格式)

不要使用time.season进行分组,改用time.dt.quarter提取日历年季度序号作为分组依据:

# 先筛选目标时间范围
result_slice = result.sel(time=slice('1991-01','2020-12'))
# 按日历年季度分组求平均,dt.quarter返回1/2/3/4对应四个自然季度
quarter_means = result_slice.groupby(result_slice.time.dt.quarter).mean(dim='time')
# 可选:修改坐标为更直观的Q1~Q4标识
quarter_means = quarter_means.rename({'quarter': 'calendar_quarter'})
quarter_means['calendar_quarter'] = ['Q1', 'Q2', 'Q3', 'Q4']

内容的提问来源于stack exchange,提问作者user2100039

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 23:54:03