diff --git a/xarray/core/common.py b/xarray/core/common.py index d272115f492..b7e3f129723 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -722,6 +722,7 @@ def resample(self, indexer=None, skipna=None, closed=None, label=None, # TODO support non-string indexer after removing the old API. from .dataarray import DataArray + from .variable import IndexVariable from .resample import RESAMPLE_DIM from ..coding.cftimeindex import CFTimeIndex @@ -745,10 +746,7 @@ def resample(self, indexer=None, skipna=None, closed=None, label=None, ) dim, freq = indexer.popitem() - dim_name = dim - dim_coord = self[dim] - - if isinstance(self.indexes[dim_name], CFTimeIndex): + if isinstance(self.indexes[dim], CFTimeIndex): raise NotImplementedError( 'Resample is currently not supported along a dimension ' 'indexed by a CFTimeIndex. For certain kinds of downsampling ' @@ -761,12 +759,12 @@ def resample(self, indexer=None, skipna=None, closed=None, label=None, 'errors.' ) - group = DataArray(dim_coord, coords=dim_coord.coords, - dims=dim_coord.dims, name=RESAMPLE_DIM) + group = IndexVariable( + data=self.indexes[dim], dims=[dim], name=RESAMPLE_DIM) # TODO: to_offset() call required for pandas==0.19.2 grouper = pd.Grouper(freq=freq, closed=closed, label=label, base=base, loffset=pd.tseries.frequencies.to_offset(loffset)) - resampler = self._resample_cls(self, group=group, dim=dim_name, + resampler = self._resample_cls(self, group=group, dim=dim, grouper=grouper, resample_dim=RESAMPLE_DIM) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 8bd7225efc3..ad70b2dec57 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -1789,12 +1789,17 @@ class IndexVariable(Variable): unless another name is given. """ - def __init__(self, dims, data, attrs=None, encoding=None, fastpath=False): + def __init__(self, dims, data, attrs=None, encoding=None, fastpath=False, name=None): super(IndexVariable, self).__init__(dims, data, attrs, encoding, fastpath) if self.ndim != 1: raise ValueError('%s objects must be 1-dimensional' % type(self).__name__) + + if name is None: + self._name = self.dims[0] + else: + self._name = name # Unlike in Variable, always eagerly load values into memory if not isinstance(self._data, PandasIndexAdapter): @@ -1956,7 +1961,7 @@ def get_level_variable(self, level): @property def name(self): - return self.dims[0] + return self._name @name.setter def name(self, value): diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index aa02e802fc5..727dd5d8fd4 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -2399,6 +2399,14 @@ def test_resample_keep_attrs(self): attrs=array.attrs) assert_identical(result, expected) + def test_resample_period_index(self): + times = pd.period_range('2000-01-01', freq='6H', periods=10) + array = DataArray(np.arange(10), [('time', times)]) + + actual = array.resample(time='24H').mean() + expected = DataArray(array.to_series().resample('24H').mean()) + assert_identical(expected, actual) + def test_resample_skipna(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) array = DataArray(np.ones(10), [('time', times)])