Skip to content

Commit 86f81b4

Browse files
committed
Merge pull request #4494 from Komnomnomnom/resample-sing-grp-bug
FIX: resample to single group with custom function
2 parents 211ea71 + 832ebbe commit 86f81b4

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ Bug Fixes
380380
(:issue:`4170`, :issue:`4440`)
381381
- Fixed Panel slicing issued in ``xs`` that was returning an incorrect dimmed object
382382
(:issue:`4016`)
383+
- Fix resampling bug where custom reduce function not used if only one group (:issue:`3849`, :issue:`4494`)
383384
- Fixed Panel assignment with a transposed frame (:issue:`3830`)
384385
- Raise on set indexing with a Panel and a Panel as a value which needs alignment (:issue:`3777`)
385386
- frozenset objects now raise in the ``Series`` constructor (:issue:`4482`,

pandas/src/reduce.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ cdef class SeriesBinGrouper:
206206

207207
counts = np.zeros(self.ngroups, dtype=np.int64)
208208

209-
if self.ngroups > 1:
209+
if self.ngroups > 0:
210210
counts[0] = self.bins[0]
211211
for i in range(1, self.ngroups):
212212
if i == self.ngroups - 1:

pandas/tseries/tests/test_resample.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,26 @@ def test_resample_anchored_ticks(self):
438438
expected = ts.resample(freq, closed='left', label='left')
439439
assert_series_equal(result, expected)
440440

441+
def test_resample_single_group(self):
442+
mysum = lambda x: x.sum()
443+
444+
rng = date_range('2000-1-1', '2000-2-10', freq='D')
445+
ts = Series(np.random.randn(len(rng)), index=rng)
446+
assert_series_equal(ts.resample('M', how='sum'),
447+
ts.resample('M', how=mysum))
448+
449+
rng = date_range('2000-1-1', '2000-1-10', freq='D')
450+
ts = Series(np.random.randn(len(rng)), index=rng)
451+
assert_series_equal(ts.resample('M', how='sum'),
452+
ts.resample('M', how=mysum))
453+
454+
# GH 3849
455+
s = Series([30.1, 31.6], index=[Timestamp('20070915 15:30:00'),
456+
Timestamp('20070915 15:40:00')])
457+
expected = Series([0.75], index=[Timestamp('20070915')])
458+
result = s.resample('D', how=lambda x: np.std(x))
459+
assert_series_equal(result, expected)
460+
441461
def test_resample_base(self):
442462
rng = date_range('1/1/2000 00:00:00', '1/1/2000 02:00', freq='s')
443463
ts = Series(np.random.randn(len(rng)), index=rng)

0 commit comments

Comments
 (0)