Skip to content

Commit 2279c92

Browse files
committed
BUG: SeriesBinGrouper bugs with how='median' or generic numpy function in some cases close #1688, #1648
1 parent 9f8ee68 commit 2279c92

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ pandas 0.8.2
6161
- Fix regression in handling of Series in Series constructor (#1671)
6262
- Fix another NumPy datetime64 concatenate bug in DataFrame.append (#1681)
6363
- Fix min_periods handling in new rolling_max/min at array start (#1695)
64+
- Fix errors with how='median' and generic NumPy resampling in some cases
65+
caused by SeriesBinGrouper (#1648, #1688)
6466

6567
pandas 0.8.1
6668
============

pandas/core/groupby.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,10 @@ def _python_agg_general(self, func, *args, **kwargs):
395395
if len(output) == 0:
396396
return self._python_apply_general(func, *args, **kwargs)
397397

398-
mask = counts.ravel() > 0
399-
for name, result in output.iteritems():
400-
output[name] = result[mask]
398+
if self.grouper._filter_empty_groups:
399+
mask = counts.ravel() > 0
400+
for name, result in output.iteritems():
401+
output[name] = result[mask]
401402

402403
return self._wrap_aggregated_output(output)
403404

pandas/src/reduce.pyx

+6-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ cdef class SeriesBinGrouper:
129129

130130
self.dummy = self._check_dummy(dummy)
131131
self.passed_dummy = dummy is not None
132-
self.ngroups = len(bins) + 1
132+
133+
# kludge for #1688
134+
if len(bins) > 0 and bins[-1] == len(series):
135+
self.ngroups = len(bins)
136+
else:
137+
self.ngroups = len(bins) + 1
133138

134139
def _check_dummy(self, dummy=None):
135140
if dummy is None:

pandas/tseries/tests/test_resample.py

+12
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,18 @@ def test_resample_not_monotonic(self):
527527
exp = ts.sort_index().resample('D', how='sum')
528528
assert_series_equal(result, exp)
529529

530+
def test_resample_median_bug_1688(self):
531+
df = DataFrame([1, 2], index=[datetime(2012,1,1,0,0,0),
532+
datetime(2012,1,1,0,5,0)])
533+
534+
result = df.resample("T", how=lambda x: x.mean())
535+
exp = df.asfreq('T')
536+
tm.assert_frame_equal(result, exp)
537+
538+
result = df.resample("T", how="median")
539+
exp = df.asfreq('T')
540+
tm.assert_frame_equal(result, exp)
541+
530542
def _simple_ts(start, end, freq='D'):
531543
rng = date_range(start, end, freq=freq)
532544
return Series(np.random.randn(len(rng)), index=rng)

0 commit comments

Comments
 (0)