home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 395082238

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/2191#issuecomment-395082238 https://api.github.com/repos/pydata/xarray/issues/2191 395082238 MDEyOklzc3VlQ29tbWVudDM5NTA4MjIzOA== 6628425 2018-06-06T14:09:56Z 2018-10-19T19:38:56Z MEMBER

When the time coordinate contains np.datetime64 objects I recommend using resample directly, because the underlying index will be a pandas DatetimeIndex (so you just need some logic to detect if that's the case).

I think the most general workaround for right now would probably look something like the example below. This has the property that it preserves the underlying calendar type of the time index. ```python import pandas as pd import xarray as xr

def resample_ms_freq(ds, dim='time'): """Resample the dataset to 'MS' frequency regardless of the calendar used.

Parameters
----------
ds : Dataset
    Dataset to be resampled
dim : str
    Dimension name associated with the time index

Returns
-------
Dataset
"""
index = ds.indexes[dim]
if isinstance(index, pd.DatetimeIndex):
    return ds.resample(**{dim: 'MS'}).mean(dim)
elif isinstance(index, xr.CFTimeIndex):
    date_type = index.date_type
    month_start = [date_type(date.year, date.month, 1) for date in ds[dim].values]
    ms = xr.DataArray(month_start, coords=ds[dim].coords)
    ds = ds.assign_coords(MS=ms)
    return ds.groupby('MS').mean(dim).rename({'MS': dim})
else:
    raise TypeError(
        'Resampling to month start frequency requires using a time index of either '
        'type pd.DatetimeIndex or xr.CFTimeIndex.')

with xr.set_options(enable_cftimeindex=True): ds = xr.open_mfdataset(files) resampled = resample_ms_freq(ds) ```

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