Issue
I am using xarray and I want to downsample this weekly data to monthly data (similar to xarray's downsampling example in the docs: http://xarray.pydata.org/en/v0.10.9/generated/xarray.Dataset.resample.html
I tried using xarray's example by downsampling to yearly instead of monthly data:
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
What is the issue with xarrays resampling and is it possible to downsample monthly to yearly (or weekly to monthly) data using xarray's resample?
Solution
Quick fix!
monthly_means = data.resample(time="M").mean() # where M is for months
Answered By - wabash
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.