Skip to content

ENH: Improve ref-tracking for group keys #51442

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 7 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 11 additions & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,9 +947,19 @@ def is_in_obj(gpr) -> bool:
# For the CoW case, we need an equality check as the identity check
Copy link
Member

Choose a reason for hiding this comment

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

Can you update this comment?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

# no longer works (each Series from column access is a new object)
try:
return gpr.equals(obj[gpr.name])
obj_gpr_column = obj[gpr.name]
except (AttributeError, KeyError, IndexError, InvalidIndexError):
Copy link
Member

Choose a reason for hiding this comment

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

You can remove the AttributeError now, I think (.name is already checked above)

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct, thx

return False
if isinstance(gpr, Series) and isinstance(obj_gpr_column, Series):
# Item "SingleArrayManager" of "Union[SingleArrayManager,
# SingleBlockManager]" has no attribute "references_same_values"
# Argument 1 to "references_same_values" of "BaseBlockManager" has
# incompatible type "Union[SingleArrayManager, SingleBlockManager]";
# expected "BaseBlockManager
return gpr._mgr.references_same_values( # type: ignore[union-attr]
obj_gpr_column._mgr, 0 # type: ignore[arg-type]
)
return False
try:
return gpr is obj[gpr.name]
except (KeyError, IndexError, InvalidIndexError):
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
cast,
)
import warnings
import weakref

import numpy as np

Expand Down Expand Up @@ -247,6 +248,14 @@ def _has_no_reference_block(self, blkno: int) -> bool:
"""
return not self.blocks[blkno].refs.has_reference()

def references_same_values(self, mgr: BaseBlockManager, blkno: int) -> bool:
"""
Checks if two blocks from two different block managers reference the
same underlying values.
"""
ref = weakref.ref(self.blocks[blkno])
return ref in mgr.blocks[blkno].refs.referenced_blocks

def get_dtypes(self):
dtypes = np.array([blk.dtype for blk in self.blocks])
return dtypes.take(self.blknos)
Expand Down