-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 3 commits
df865a1
05b4340
36afc13
3635ce9
d6e77d6
c8563a9
b907a4e
a275aec
faf6846
a84df00
1fe6559
c234b36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
|
||
if self._groupby.ndim == 1 and self._groupby.obj.name == key: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hate checks like this :> does There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -520,6 +520,39 @@ def test_iter_raises(self, klass): | |
with pytest.raises(NotImplementedError): | ||
iter(obj.rolling(2)) | ||
|
||
def test_agg(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
||
|
There was a problem hiding this comment.
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)