def test_resample(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) array = DataArray(np.arange(10), [('time', times)]) actual = array.resample('6H', dim='time') self.assertDataArrayIdentical(array, actual) actual = array.resample('24H', dim='time') expected = DataArray(array.to_series().resample('24H')) self.assertDataArrayIdentical(expected, actual) actual = array.resample('24H', dim='time', how=np.mean) self.assertDataArrayIdentical(expected, actual) with self.assertRaisesRegexp(ValueError, 'index must be monotonic'): array[[2, 0, 1]].resample('1D', dim='time')
def test_resample_skipna(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) array = DataArray(np.ones(10), [('time', times)]) array[1] = np.nan actual = array.resample('1D', dim='time', skipna=False) expected = DataArray([np.nan, 1, 1], [('time', times[::4])]) self.assertDataArrayIdentical(expected, actual)
def test_resample_upsampling(self): times = pd.date_range('2000-01-01', freq='1D', periods=5) array = DataArray(np.arange(5), [('time', times)]) expected_time = pd.date_range('2000-01-01', freq='12H', periods=9) expected = array.reindex(time=expected_time) for how in ['mean', 'median', 'sum', 'first', 'last', np.mean]: actual = array.resample('12H', 'time', how=how) self.assertDataArrayIdentical(expected, actual)
def test_resample_first(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) array = DataArray(np.arange(10), [('time', times)]) actual = array.resample('1D', dim='time', how='first') expected = DataArray([0, 4, 8], [('time', times[::4])]) self.assertDataArrayIdentical(expected, actual) # verify that labels don't use the first value actual = array.resample('24H', dim='time', how='first') expected = DataArray(array.to_series().resample('24H', how='first')) self.assertDataArrayIdentical(expected, actual) # missing values array = array.astype(float) array[:2] = np.nan actual = array.resample('1D', dim='time', how='first') expected = DataArray([2, 4, 8], [('time', times[::4])]) self.assertDataArrayIdentical(expected, actual) actual = array.resample('1D', dim='time', how='first', skipna=False) expected = DataArray([np.nan, 4, 8], [('time', times[::4])]) self.assertDataArrayIdentical(expected, actual)