Skip to content

FIX: resample to single group with custom function #4494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
2 changes: 1 addition & 1 deletion pandas/src/reduce.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down