Skip to content

BUG: Bug in .groupby(..).resample(..) when the same object is called multiple times #13175

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Bug Fixes
- Bug when passing a not-default-indexed ``Series`` as ``xerr`` or ``yerr`` in ``.plot()`` (:issue:`11858`)



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


- 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`)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2835,6 +2835,20 @@ def test_getitem(self):
result = self.frame.B.groupby(self.frame.A).rolling(2).mean()
assert_series_equal(result, expected)

def test_getitem_multiple(self):

# GH 13174
g = self.frame.groupby('A')
r = g.rolling(2)
g_mutated = self.frame.groupby('A', mutated=True)
expected = g_mutated.B.apply(lambda x: x.rolling(2).count())

result = r.B.count()
assert_series_equal(result, expected)

result = r.B.count()
assert_series_equal(result, expected)

def test_rolling(self):
g = self.frame.groupby('A')
r = g.rolling(window=4)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tseries/resample.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import timedelta
import numpy as np
import warnings
import copy

import pandas as pd
from pandas.core.base import AbstractMethodError, GroupByMixin
Expand Down Expand Up @@ -592,7 +593,7 @@ def __init__(self, obj, *args, **kwargs):
self._groupby = groupby
self._groupby.mutated = True
self._groupby.grouper.mutated = True
self.groupby = parent.groupby
self.groupby = copy.copy(parent.groupby)

def _apply(self, f, **kwargs):
"""
Expand Down
19 changes: 19 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2519,6 +2519,25 @@ def test_getitem(self):
result = g.resample('2s').mean().B
assert_series_equal(result, expected)

def test_getitem_multiple(self):

# GH 13174
# multiple calls after selection causing an issue with aliasing
data = [{'id': 1, 'buyer': 'A'}, {'id': 2, 'buyer': 'B'}]
df = pd.DataFrame(data, index=pd.date_range('2016-01-01', periods=2))
r = df.groupby('id').resample('1D')
result = r['buyer'].count()
expected = pd.Series([1, 1],
index=pd.MultiIndex.from_tuples(
[(1, pd.Timestamp('2016-01-01')),
(2, pd.Timestamp('2016-01-02'))],
names=['id', None]),
name='buyer')
assert_series_equal(result, expected)

result = r['buyer'].count()
assert_series_equal(result, expected)

def test_methods(self):
g = self.frame.groupby('A')
r = g.resample('2s')
Expand Down