diff --git a/xarray/core/common.py b/xarray/core/common.py index 41e4fec2982..7c811182816 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -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 @@ -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)