Skip to content

WIP: sketch of resample support for CFTimeIndex #2458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,8 @@ def resample(self, freq=None, dim=None, how=None, skipna=None,
"""
# TODO support non-string indexer after removing the old API.

from ..coding.cftime_offsets import cftime_range
from ..coding.cftimeindex import CFTimeIndex
from .dataarray import DataArray
from .resample import RESAMPLE_DIM

Expand All @@ -679,12 +681,24 @@ def resample(self, freq=None, dim=None, how=None, skipna=None,

if isinstance(dim, basestring):
dim_name = dim
dim = self[dim]
dim_array = self[dim]
else:
raise TypeError("Dimension name should be a string; "
"was passed %r" % dim)
group = DataArray(dim, [(dim.dims, dim)], name=RESAMPLE_DIM)
grouper = pd.Grouper(freq=freq, closed=closed, label=label, base=base)
group = DataArray(dim_array, [(dim_array.dims, dim_array)],
name=RESAMPLE_DIM)

if isinstance(self.indexes[dim], CFTimeIndex):
# TODO: handle closed, label and base arguments, and the case where
# frequency is specified without an integer count.
times = self.indexes[dim]
resampled_times = cftime_range(
start=times[0], end=times[-1], freq=freq)
grouper = (pd.Series(resampled_times, index=resampled_times)
.reindex(times, method='pad'))
else:
grouper = pd.Grouper(
freq=freq, closed=closed, label=label, base=base)
resampler = self._resample_cls(self, group=group, dim=dim_name,
grouper=grouper,
resample_dim=RESAMPLE_DIM)
Expand Down