Skip to content

Commit feee089

Browse files
committed
BUG: Bug in .groupby(..).resample(..) when the same object is called multiple times
closes pandas-dev#13174 Author: Jeff Reback <[email protected]> Closes pandas-dev#13175 from jreback/resample and squashes the following commits: 56b405e [Jeff Reback] BUG: Bug in .groupby(..).resample(..) when the same object is called multiple times
1 parent 01dd111 commit feee089

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

doc/source/whatsnew/v0.18.2.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Bug Fixes
118118
- Bug when passing a not-default-indexed ``Series`` as ``xerr`` or ``yerr`` in ``.plot()`` (:issue:`11858`)
119119

120120

121-
121+
- Bug in ``.groupby(..).resample(..)`` when the same object is called multiple times (:issue:`13174`)
122122

123123

124124
- Regression in ``Series.quantile`` with nans (also shows up in ``.median()`` and ``.describe()``); furthermore now names the ``Series`` with the quantile (:issue:`13098`, :issue:`13146`)

pandas/tests/test_window.py

+14
Original file line numberDiff line numberDiff line change
@@ -2835,6 +2835,20 @@ def test_getitem(self):
28352835
result = self.frame.B.groupby(self.frame.A).rolling(2).mean()
28362836
assert_series_equal(result, expected)
28372837

2838+
def test_getitem_multiple(self):
2839+
2840+
# GH 13174
2841+
g = self.frame.groupby('A')
2842+
r = g.rolling(2)
2843+
g_mutated = self.frame.groupby('A', mutated=True)
2844+
expected = g_mutated.B.apply(lambda x: x.rolling(2).count())
2845+
2846+
result = r.B.count()
2847+
assert_series_equal(result, expected)
2848+
2849+
result = r.B.count()
2850+
assert_series_equal(result, expected)
2851+
28382852
def test_rolling(self):
28392853
g = self.frame.groupby('A')
28402854
r = g.rolling(window=4)

pandas/tseries/resample.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import timedelta
22
import numpy as np
33
import warnings
4+
import copy
45

56
import pandas as pd
67
from pandas.core.base import AbstractMethodError, GroupByMixin
@@ -592,7 +593,7 @@ def __init__(self, obj, *args, **kwargs):
592593
self._groupby = groupby
593594
self._groupby.mutated = True
594595
self._groupby.grouper.mutated = True
595-
self.groupby = parent.groupby
596+
self.groupby = copy.copy(parent.groupby)
596597

597598
def _apply(self, f, **kwargs):
598599
"""

pandas/tseries/tests/test_resample.py

+19
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,25 @@ def test_getitem(self):
25192519
result = g.resample('2s').mean().B
25202520
assert_series_equal(result, expected)
25212521

2522+
def test_getitem_multiple(self):
2523+
2524+
# GH 13174
2525+
# multiple calls after selection causing an issue with aliasing
2526+
data = [{'id': 1, 'buyer': 'A'}, {'id': 2, 'buyer': 'B'}]
2527+
df = pd.DataFrame(data, index=pd.date_range('2016-01-01', periods=2))
2528+
r = df.groupby('id').resample('1D')
2529+
result = r['buyer'].count()
2530+
expected = pd.Series([1, 1],
2531+
index=pd.MultiIndex.from_tuples(
2532+
[(1, pd.Timestamp('2016-01-01')),
2533+
(2, pd.Timestamp('2016-01-02'))],
2534+
names=['id', None]),
2535+
name='buyer')
2536+
assert_series_equal(result, expected)
2537+
2538+
result = r['buyer'].count()
2539+
assert_series_equal(result, expected)
2540+
25222541
def test_methods(self):
25232542
g = self.frame.groupby('A')
25242543
r = g.resample('2s')

0 commit comments

Comments
 (0)