Skip to content

Commit

Permalink
BUG: check for monotonicity in resampling, close #1580
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Jul 11, 2012
1 parent 005b264 commit 9a8ad65
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
============
Expand Down
7 changes: 7 additions & 0 deletions pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9a8ad65

Please sign in to comment.