Skip to content
Merged
Changes from 3 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
7 changes: 6 additions & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
final,
)
import warnings
import weakref

import numpy as np

Expand Down Expand Up @@ -947,9 +948,13 @@ 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):
ref = weakref.ref(obj_gpr_column._mgr.blocks[0])
return ref in gpr._mgr.blocks[0].refs.referenced_blocks
Copy link
Member

Choose a reason for hiding this comment

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

So if you create multiple times a weakref to the same object, this is always the same (i.e. identical) weakref object?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, that's how it is documented (https://docs.python.org/3/library/weakref.html#weakref.ref). Did some basic checks with out blocks and it works as I would have expected

return False
try:
return gpr is obj[gpr.name]
except (KeyError, IndexError, InvalidIndexError):
Expand Down