Skip to content

Commit 9a8ad65

Browse files
committed
BUG: check for monotonicity in resampling, close #1580
1 parent 005b264 commit 9a8ad65

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pandas 0.8.1
6262
- Override Index.tolist for compatibility with MultiIndex (#1576)
6363
- Fix hierarchical summing bug with MultiIndex of length 1 (#1568)
6464
- Work around numpy.concatenate use/bug in Series.set_value (#1561)
65+
- Ensure Series/DataFrame are sorted before resampling (#1580)
6566

6667
pandas 0.8.0
6768
============

pandas/tseries/resample.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def __init__(self, freq='Min', closed='right', label='right', how='mean',
5353

5454
def resample(self, obj):
5555
axis = obj._get_axis(self.axis)
56+
57+
if not axis.is_monotonic:
58+
try:
59+
obj = obj.sort_index(axis=self.axis)
60+
except TypeError:
61+
obj = obj.sort_index()
62+
5663
if isinstance(axis, DatetimeIndex):
5764
return self._resample_timestamps(obj)
5865
elif isinstance(axis, PeriodIndex):

pandas/tseries/tests/test_resample.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,16 @@ def test_upsample_apply_functions(self):
510510
result = ts.resample('20min', how=['mean', 'sum'])
511511
self.assert_(isinstance(result, DataFrame))
512512

513+
def test_resample_not_monotonic(self):
514+
rng = pd.date_range('2012-06-12', periods=200, freq='h')
515+
ts = Series(np.random.randn(len(rng)), index=rng)
516+
517+
ts = ts.take(np.random.permutation(len(ts)))
518+
519+
result = ts.resample('D', how='sum')
520+
exp = ts.sort_index().resample('D', how='sum')
521+
assert_series_equal(result, exp)
522+
513523
def _simple_ts(start, end, freq='D'):
514524
rng = date_range(start, end, freq=freq)
515525
return Series(np.random.randn(len(rng)), index=rng)

0 commit comments

Comments
 (0)