From 832ebbe08fa0efeccc582a3964ad14cf3664a287 Mon Sep 17 00:00:00 2001 From: Kieran O'Mahony Date: Wed, 7 Aug 2013 12:38:32 +1000 Subject: [PATCH] FIX: resample to single group with custom function --- doc/source/release.rst | 1 + pandas/src/reduce.pyx | 2 +- pandas/tseries/tests/test_resample.py | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 97150cbeb53a2..a0e04156bbb64 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -380,6 +380,7 @@ Bug Fixes (:issue:`4170`, :issue:`4440`) - Fixed Panel slicing issued in ``xs`` that was returning an incorrect dimmed object (:issue:`4016`) + - Fix resampling bug where custom reduce function not used if only one group (:issue:`3849`, :issue:`4494`) - Fixed Panel assignment with a transposed frame (:issue:`3830`) - Raise on set indexing with a Panel and a Panel as a value which needs alignment (:issue:`3777`) - frozenset objects now raise in the ``Series`` constructor (:issue:`4482`, diff --git a/pandas/src/reduce.pyx b/pandas/src/reduce.pyx index d59c28a30796a..cd010151f1ef7 100644 --- a/pandas/src/reduce.pyx +++ b/pandas/src/reduce.pyx @@ -206,7 +206,7 @@ cdef class SeriesBinGrouper: counts = np.zeros(self.ngroups, dtype=np.int64) - if self.ngroups > 1: + if self.ngroups > 0: counts[0] = self.bins[0] for i in range(1, self.ngroups): if i == self.ngroups - 1: diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py index 6ad69a466ba03..620310e32ffcc 100644 --- a/pandas/tseries/tests/test_resample.py +++ b/pandas/tseries/tests/test_resample.py @@ -438,6 +438,26 @@ def test_resample_anchored_ticks(self): expected = ts.resample(freq, closed='left', label='left') assert_series_equal(result, expected) + def test_resample_single_group(self): + mysum = lambda x: x.sum() + + rng = date_range('2000-1-1', '2000-2-10', freq='D') + ts = Series(np.random.randn(len(rng)), index=rng) + assert_series_equal(ts.resample('M', how='sum'), + ts.resample('M', how=mysum)) + + rng = date_range('2000-1-1', '2000-1-10', freq='D') + ts = Series(np.random.randn(len(rng)), index=rng) + assert_series_equal(ts.resample('M', how='sum'), + ts.resample('M', how=mysum)) + + # GH 3849 + s = Series([30.1, 31.6], index=[Timestamp('20070915 15:30:00'), + Timestamp('20070915 15:40:00')]) + expected = Series([0.75], index=[Timestamp('20070915')]) + result = s.resample('D', how=lambda x: np.std(x)) + assert_series_equal(result, expected) + def test_resample_base(self): rng = date_range('1/1/2000 00:00:00', '1/1/2000 02:00', freq='s') ts = Series(np.random.randn(len(rng)), index=rng)