Skip to content

BUG/REF: Refactor BaseWindowIndex so groupby.apply has a consistent index output #39765

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 19 commits into from
Feb 25, 2021

Conversation

mroeschke
Copy link
Member

@mroeschke mroeschke added Bug Refactor Internal refactoring of code Window rolling, ewma, expanding labels Feb 12, 2021
@mroeschke mroeschke added this to the 1.3 milestone Feb 12, 2021
self._groupby = groupby
self._groupby.mutated = True
self._groupby.grouper.mutated = True
_attributes = [
Copy link
Contributor

Choose a reason for hiding this comment

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

rather than doing this. can you create a private classmethod, instantiate and mutate the returned object? e.g.

@classmethod _create_from_grouping(cls,, grouping_indices, keys, codes......):
    obj = cls(.......)
    obj._grouping_indices = grouping_indcies
    .....
    return obj

Copy link
Member Author

Choose a reason for hiding this comment

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

This attribute (_attributes) is currently necessary to the window classes for _gotitem where a shallow copy is performed, so I would still need to keep this reference

https://github.com/pandas-dev/pandas/pull/39765/files#diff-376f031d24a9c167d8ea06982971b613702f7dd122e168c3a8b333009a811ed4R254

_grouping_levels=None,
**kwargs,
):
self._grouping_indices = _grouping_indices or {}
Copy link
Contributor

Choose a reason for hiding this comment

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

see above but maybe worthile having a dataclass holding all of these things (so 1 object) e.g.

class Groupings:
    indices: .....

Copy link
Contributor

Choose a reason for hiding this comment

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

can you do this instead

Copy link
Contributor

Choose a reason for hiding this comment

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

you might also just be able to pass the Grouper object itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah good point. I'll just pass the grouper object

_grouping_levels=None,
**kwargs,
):
self._grouping_indices = _grouping_indices or {}
Copy link
Contributor

Choose a reason for hiding this comment

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

can you do this instead

_grouping_levels=None,
**kwargs,
):
self._grouping_indices = _grouping_indices or {}
Copy link
Contributor

Choose a reason for hiding this comment

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

you might also just be able to pass the Grouper object itself.

@@ -101,6 +107,9 @@ def test_rolling_quantile(self, interpolation):
g = self.frame.groupby("A")
r = g.rolling(window=4)
result = r.quantile(0.4, interpolation=interpolation)
# GH 39732
g.mutated = True
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you setting all of these? what is the purpose?

Copy link
Member Author

Choose a reason for hiding this comment

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

To get the resulting index of g.apply(lambda x: x.rolling(...) to match self.frame..groupby(...).rolling(...), the mutated attributes on g need to be set to True

It directly why #39732 was a bug

Copy link
Member Author

Choose a reason for hiding this comment

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

I will change the expected indexes instead of setting this attribute

@jreback
Copy link
Contributor

jreback commented Feb 25, 2021

lgtm. can merge on green.

@mroeschke mroeschke merged commit 2724350 into pandas-dev:master Feb 25, 2021
@mroeschke mroeschke deleted the cln/rollinggroupby branch February 25, 2021 06:05
@mroeschke
Copy link
Member Author

CI failures were related to the timedelta issue that has already been fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Refactor Internal refactoring of code Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Resulting index from groupby.apply is different depending on whether a RollingGroupby object is created
2 participants