From 9a8ad655d1105c6ea381ba02017439819bdfd005 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Wed, 11 Jul 2012 19:37:08 -0400 Subject: [PATCH] BUG: check for monotonicity in resampling, close #1580 --- RELEASE.rst | 1 + pandas/tseries/resample.py | 7 +++++++ pandas/tseries/tests/test_resample.py | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/RELEASE.rst b/RELEASE.rst index a58faa0086275..9b9e7e5b8d9b9 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -62,6 +62,7 @@ pandas 0.8.1 - Override Index.tolist for compatibility with MultiIndex (#1576) - Fix hierarchical summing bug with MultiIndex of length 1 (#1568) - Work around numpy.concatenate use/bug in Series.set_value (#1561) + - Ensure Series/DataFrame are sorted before resampling (#1580) pandas 0.8.0 ============ diff --git a/pandas/tseries/resample.py b/pandas/tseries/resample.py index 9ecaeb5d87bae..c5ba73edad225 100644 --- a/pandas/tseries/resample.py +++ b/pandas/tseries/resample.py @@ -53,6 +53,13 @@ def __init__(self, freq='Min', closed='right', label='right', how='mean', def resample(self, obj): axis = obj._get_axis(self.axis) + + if not axis.is_monotonic: + try: + obj = obj.sort_index(axis=self.axis) + except TypeError: + obj = obj.sort_index() + if isinstance(axis, DatetimeIndex): return self._resample_timestamps(obj) elif isinstance(axis, PeriodIndex): diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py index 216eb9232ecd9..14a05aaf4b31b 100644 --- a/pandas/tseries/tests/test_resample.py +++ b/pandas/tseries/tests/test_resample.py @@ -510,6 +510,16 @@ def test_upsample_apply_functions(self): result = ts.resample('20min', how=['mean', 'sum']) self.assert_(isinstance(result, DataFrame)) + def test_resample_not_monotonic(self): + rng = pd.date_range('2012-06-12', periods=200, freq='h') + ts = Series(np.random.randn(len(rng)), index=rng) + + ts = ts.take(np.random.permutation(len(ts))) + + result = ts.resample('D', how='sum') + exp = ts.sort_index().resample('D', how='sum') + assert_series_equal(result, exp) + def _simple_ts(start, end, freq='D'): rng = date_range(start, end, freq=freq) return Series(np.random.randn(len(rng)), index=rng)