Skip to content

Fixed Issue Preventing Agg on RollingGroupBy Objects #21323

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 12 commits into from
Sep 26, 2018
8 changes: 7 additions & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,14 @@ def _gotitem(self, key, ndim, subset=None):
# with the same groupby
kwargs = dict([(attr, getattr(self, attr))
for attr in self._attributes])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor, but can you put the comments before the try (and consolidate them into 1)

if self._groupby.ndim == 1 and self._groupby.obj.name == key:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate checks like this :>

does
groupby = self._groupby[key] not just work here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s what failed in the original code. Could alternately try and catch the error

groupby = self._groupby
else:
groupby = self._groupby[key]

self = self.__class__(subset,
groupby=self._groupby[key],
groupby=groupby,
parent=self,
**kwargs)
self._reset_cache()
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,39 @@ def test_iter_raises(self, klass):
with pytest.raises(NotImplementedError):
iter(obj.rolling(2))

def test_agg(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be a bit more descriptive here on the test name / expl

# GH 15072
df = pd.DataFrame([
['A', 10, 20],
['A', 20, 30],
['A', 30, 40],
['B', 10, 30],
['B', 30, 40],
['B', 40, 80],
['B', 80, 90]], columns=['stock', 'low', 'high'])

index = pd.MultiIndex.from_tuples([
('A', 0), ('A', 1), ('A', 2),
('B', 3), ('B', 4), ('B', 5), ('B', 6)], names=['stock', None])
columns = pd.MultiIndex.from_tuples([
('low', 'mean'), ('low', 'max'), ('high', 'mean'),
('high', 'min')])
expected = pd.DataFrame([
[np.nan, np.nan, np.nan, np.nan],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maye put in TestApi. this should test rolling & expanding (atm we don't have a nice modular structure to do this :<)

[15., 20., 25., 20.],
[25., 30., 35., 30.],
[np.nan, np.nan, np.nan, np.nan],
[20., 30., 35., 30.],
[35., 40., 60., 40.],
[60., 80., 85., 80]], index=index, columns=columns)

result = df.groupby('stock').rolling(2).agg({
'low': ['mean', 'max'],
'high': ['mean', 'min']
})

tm.assert_frame_equal(result, expected)


class TestExpanding(Base):

Expand Down