如何使用 xarray resample 将月度数据下采样为年度数据?

问题描述 投票:0回答:2

我正在使用 xarray,我想将每周数据下采样为每月数据(类似于文档中 xarray 的下采样示例:http://xarray.pydata.org/en/v0.10.9/ generated/xarray.Dataset.resample .html

我尝试使用 xarray 的示例,将数据下采样为每年而不是每月数据:

import pandas as pd
import xarray as xr 

da = xr.DataArray(np.linspace(0, 11, num=12),
                   coords=[pd.date_range('15/12/1999',
                          periods=12, freq=pd.DateOffset(months=1))],
                   dims='time')

print(da.time)

<xarray.DataArray 'time' (time: 12)>
array(['1999-12-15T00:00:00.000000000', '2000-01-15T00:00:00.000000000',
       '2000-02-15T00:00:00.000000000', '2000-03-15T00:00:00.000000000',
       '2000-04-15T00:00:00.000000000', '2000-05-15T00:00:00.000000000',
       '2000-06-15T00:00:00.000000000', '2000-07-15T00:00:00.000000000',
       '2000-08-15T00:00:00.000000000', '2000-09-15T00:00:00.000000000',
       '2000-10-15T00:00:00.000000000', '2000-11-15T00:00:00.000000000'],
      dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 1999-12-15 2000-01-15 ... 2000-11-15

da.resample(time="year").mean() #downsampling to yearly instead of monthly 

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
pandas/_libs/tslibs/offsets.pyx in pandas._libs.tslibs.offsets._get_offset()

KeyError: 'YEAR'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
pandas/_libs/tslibs/offsets.pyx in pandas._libs.tslibs.offsets.to_offset()

pandas/_libs/tslibs/offsets.pyx in pandas._libs.tslibs.offsets._get_offset()

ValueError: Invalid frequency: YEAR

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-102-6f67596d7f09> in <module>
----> 1 da.resample(time="year").mean()

~/miniconda3/envs/py3_std_maps/lib/python3.8/site-packages/xarray/core/common.py in resample(self, indexer, skipna, closed, label, base, keep_attrs, loffset, restore_coord_dims, **indexer_kwargs)
   1140                 grouper = CFTimeGrouper(freq, closed, label, base, loffset)
   1141             else:
-> 1142                 grouper = pd.Grouper(
   1143                     freq=freq, closed=closed, label=label, base=base, loffset=loffset
   1144                 )

~/miniconda3/envs/py3_std_maps/lib/python3.8/site-packages/pandas/core/resample.py in __init__(self, freq, closed, label, how, axis, fill_method, limit, loffset, kind, convention, base, origin, offset, **kwargs)
   1337             raise ValueError(f"Unsupported value {convention} for `convention`")
   1338 
-> 1339         freq = to_offset(freq)
   1340 
   1341         end_types = {"M", "A", "Q", "BM", "BA", "BQ", "W"}

pandas/_libs/tslibs/offsets.pyx in pandas._libs.tslibs.offsets.to_offset()

pandas/_libs/tslibs/offsets.pyx in pandas._libs.tslibs.offsets.to_offset()

ValueError: Invalid frequency: year

xarrays 重采样有什么问题?是否可以使用 xarray 的重采样将每月数据下采样到每年(或每周到每月)数据?

python numpy jupyter python-xarray
2个回答
4
投票

快速修复!

monthly_means = data.resample(time="M").mean() # where M is for months

0
投票

当您想从月度平均值变为年度平均值时,您需要考虑每月不同的天数。

您可以使用天数作为权重来完成此操作,如此处所示。

© www.soinside.com 2019 - 2024. All rights reserved.