【发布时间】:2018-02-10 02:10:21
【问题描述】:
我有一个文件是一年的月度数据(12 分)。数据从 12 月开始,到 11 月结束。我希望创建一个 3 个月的运行平均文件,即 DJF、JFM、...、SON(10 分)
我注意到有一个DataArray.rolling 函数返回一个滚动窗口选项,我认为这对此很有用。但是,我还没有找到任何使用滚动功能的示例。我承认我不熟悉bottleneck、pandas.rolling_mean 或更新的pandas.rolling,所以我的入门级别相当低。
这里有一些代码要测试:
import numpy as np
import pandas as pd
import xarray as xr
lat = np.linspace(-90, 90, num=181); lon = np.linspace(0, 359, num=360)
# Define monthly average time as day in middle of month
time = pd.date_range('15/12/1999', periods=12, freq=pd.DateOffset(months=1))
# Create data as 0:11 at each grid point
a = np.linspace(0,11,num=12)
# expand to 2D
a2d = np.repeat(tmp[:, np.newaxis], len(lat), axis=1)
# expand to 3D
a3d = np.repeat(a2d[:, :, np.newaxis], len(lon), axis=2)
# I'm sure there was a cleaner way to do that...
da = xr.DataArray(a3d, coords=[time, lat, lon], dims=['time','lat','lon'])
# Having a stab at the 3-month rolling mean
da.rolling(dim='time',window=3).mean()
# Error output:
Traceback (most recent call last):
File "<ipython-input-132-9d64cc09c263>", line 1, in <module>
da.rolling(dim='time',window=3).mean()
File "/Users/Ray/anaconda/lib/python3.6/site-packages/xarray/core/common.py", line 478, in rolling
center=center, **windows)
File "/Users/Ray/anaconda/lib/python3.6/site-packages/xarray/core/rolling.py", line 126, in __init__
center=center, **windows)
File "/Users/Ray/anaconda/lib/python3.6/site-packages/xarray/core/rolling.py", line 62, in __init__
raise ValueError('exactly one dim/window should be provided')
ValueError: 应该只提供一个暗淡/窗口
【问题讨论】:
标签: pandas python-xarray