簡體   English   中英

如何使用 xarray 重采樣將月度數據下采樣為年度數據?

[英]How to downsample monthly data to yearly data using xarray resample?

我正在使用 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 的重采樣將每月數據下采樣到每年(或每周到每月)數據?

快速解決!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM