Skip to content

BUG: avoid incorrectly inheriting result #27445

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

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
17 changes: 11 additions & 6 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1):
new_blocks = []
new_items = []
deleted_items = []
no_result = object()
Copy link
Contributor

Choose a reason for hiding this comment

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

why don't you just set result = None outside the try instead as its more clear

Copy link
Member Author

Choose a reason for hiding this comment

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

because I don't know for sure that None won't be the correct result produced on line 154

for block in data.blocks:

# avoid inheriting result from previous step in loop
result = no_result

locs = block.mgr_locs.as_array
try:
result, _ = self.grouper.aggregate(
Expand All @@ -171,15 +175,16 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1):
except TypeError:
# we may have an exception in trying to aggregate
# continue and exclude the block
pass
deleted_items.append(locs)
Copy link
Contributor

Choose a reason for hiding this comment

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

do we actually use the deleted_items?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, down on 195

continue

finally:
if result is not no_result:
dtype = block.values.dtype

dtype = block.values.dtype

# see if we can cast the block back to the original dtype
result = block._try_coerce_and_cast_result(result, dtype=dtype)
newb = block.make_block(result)
# see if we can cast the block back to the original dtype
result = block._try_coerce_and_cast_result(result, dtype=dtype)
newb = block.make_block(result)

new_items.append(locs)
new_blocks.append(newb)
Expand Down