home / github / issue_comments

Menu
  • GraphQL API
  • Search all tables

issue_comments: 390973986

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/1270#issuecomment-390973986 https://api.github.com/repos/pydata/xarray/issues/1270 390973986 MDEyOklzc3VlQ29tbWVudDM5MDk3Mzk4Ng== 6628425 2018-05-22T12:36:35Z 2018-05-22T12:36:35Z MEMBER

+1 to this issue. I'm struggling big time with an 1800-year climate model dataset that I need to resample in order to make different annual means (June-May).

@lvankampenhout I agree that it would be nice if xarray had better support for PeriodIndexes.

Do you happen to be using a PeriodIndex because of pandas Timestamp-limitations? Despite the fact that generalized resample has not been implemented yet, I recommend you try using the new CFTimeIndex. As it turns out, for some one-off cases (like this one) resample is not too difficult to mimic using groupby. See the following example for your case. I'm assuming you're looking for resampling with the 'AS-JUN' anchored offset? ```python from itertools import product from cftime import DatetimeProlepticGregorian as datetime import numpy as np import xarray as xr

xr.set_options(enable_cftimeindex=True)

Set up some example data indexed by cftime.DatetimeProlepticGregorian objects

dates = [datetime(year, month, 1) for year, month in product(range(2, 5), range(1, 13))] da = xr.DataArray(np.arange(len(dates)), coords=[dates], dims=['time'])

Mimic resampling with the AS-JUN anchored offset

years = da.time.dt.year - (da.time.dt.month < 6) da['AS-JUN'] = xr.DataArray([datetime(year, 6, 1) for year in years], coords=da.time.coords) resampled = da.groupby('AS-JUN').mean('time').rename({'AS-JUN': 'time'}) This gives the following for `resampled`: <xarray.DataArray (time: 4)> array([ 2. , 10.5, 22.5, 32. ]) Coordinates: * time (time) object 0001-06-01 00:00:00 0002-06-01 00:00:00 ... This is analogous to using `resample(time='AS-JUN')` with a DataArray indexed by a DatetimeIndex:python import pandas as pd dates = pd.date_range('2002-01-01', freq='M', periods=36) da = xr.DataArray(np.arange(len(dates)), coords=[dates], dims='time') resampled = da.resample(time='AS-JUN').mean('time') which gives: <xarray.DataArray (time: 4)> array([ 2. , 10.5, 22.5, 32. ]) Coordinates: * time (time) datetime64[ns] 2001-06-01 2002-06-01 2003-06-01 2004-06-01 ```

{
    "total_count": 1,
    "+1": 1,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  207862981
Powered by Datasette · Queries took 3.37ms · About: xarray-datasette