python - xarray : compute daily anomalies from monthly resampled average (not the climatology)

标签 python python-xarray

xarray 的 documentation解释了如何计算月度气候学的异常。在这里,我试图做一些稍微不同的事情:我想根据每日时间序列计算本月平均值的每日异常值(而不是来自月度气候学)。

我设法使用 groupby 和手动创建的月戳(下面的代码)来做到这一点。有没有更好、更简单的方法来获得相同的结果?

import xarray as xr
import numpy as np
import pandas as pd

# Create a data array
t = pd.date_range('2001', '2003', freq='D')
da = xr.DataArray(np.arange(len(t)), coords={'time':t}, dims='time')

# Monthly time stamp for groupby
da.coords['stamp'] = ('time', [str(y) + '-' + str(m) for (y, m) in 
                               zip(da['time.year'].values, 
                                   da['time.month'].values)])

# Anomaly
da_ano = da.groupby('stamp') - da.groupby('stamp').mean()

da_ano.plot();

plot output

最佳答案

你可以显式地 resample月时间序列意味着变成日时间序列。示例:

monthly = da.resample(time='1MS').mean()
upsampled_monthly = monthly.resample(time='1D').ffill()
anomalies = da - upsampled_monthly

关于python - xarray : compute daily anomalies from monthly resampled average (not the climatology),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49600180/

相关文章:

python - 我如何解释 Python coverage.py 分支覆盖结果?

python-3.x - xarray 中的乘法

Python xarray.concat 然后 xarray.to_netcdf 生成巨大的新文件大小

python - 在 python 中使用 xarray 重命名 netcdf 全局属性

python - 删除 Xarray 子图中的共享颜色条

python - Spacy 中的自定义句子分割

python - 如何复制virtualenv

python - 如何反转 Matplotlib 发散图中的顺序?

python - 从文件中读取行但存储为列表(python)

python - 我们如何从指定坐标的邻域值中提取最大值?