-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Fix segfault in GroupBy.count and DataFrame.count #32842
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
13be18e
Enable bounds checking.
tv3141 36513c3
Add failing tests.
tv3141 d6df0f0
Count only masked (non-na) values.
tv3141 049b897
Mask NaNs in index and dataframe values.
tv3141 1e755b3
Clean up tests.
tv3141 de75248
Re-disable bounds checking.
tv3141 3bea5af
Move test into right module.
tv3141 432cd68
Fix masking logic to avoid SegFaults with DataFrame.count().
tv3141 7e379b1
black
tv3141 a660d0f
Add info to whatsnew.
tv3141 52a6f99
Fix issue with default int32 on Windows.
tv3141 6c75c89
Merge remote-tracking branch 'upstream/master' into fix_count_segfault
tv3141 375a4d1
Merge remote-tracking branch 'upstream/master' into fix_count_segfault
tv3141 60289c2
Re-add notna optimisation.
tv3141 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7893,34 +7893,36 @@ def _count_level(self, level, axis=0, numeric_only=False): | |
f"Can only count levels on hierarchical {self._get_axis_name(axis)}." | ||
) | ||
|
||
# Mask NaNs: Mask rows or columns where the index level is NaN, and all | ||
# values in the DataFrame that are NaN | ||
if frame._is_mixed_type: | ||
# Since we have mixed types, calling notna(frame.values) might | ||
# upcast everything to object | ||
mask = notna(frame).values | ||
values_mask = notna(frame).values | ||
else: | ||
# But use the speedup when we have homogeneous dtypes | ||
mask = notna(frame.values) | ||
values_mask = notna(frame.values) | ||
|
||
index_mask = notna(count_axis.get_level_values(level=level)) | ||
if axis == 1: | ||
# We're transposing the mask rather than frame to avoid potential | ||
# upcasts to object, which induces a ~20x slowdown | ||
mask = mask.T | ||
mask = index_mask & values_mask | ||
else: | ||
mask = index_mask.reshape(-1, 1) & values_mask | ||
|
||
if isinstance(level, str): | ||
level = count_axis._get_level_number(level) | ||
|
||
level_name = count_axis._names[level] | ||
level_index = count_axis.levels[level]._shallow_copy(name=level_name) | ||
level_codes = ensure_int64(count_axis.codes[level]) | ||
counts = lib.count_level_2d(mask, level_codes, len(level_index), axis=0) | ||
|
||
result = DataFrame(counts, index=level_index, columns=agg_axis) | ||
counts = lib.count_level_2d(mask, level_codes, len(level_index), axis=axis) | ||
|
||
if axis == 1: | ||
# Undo our earlier transpose | ||
return result.T | ||
result = DataFrame(counts, index=agg_axis, columns=level_index) | ||
else: | ||
return result | ||
result = DataFrame(counts, index=level_index, columns=agg_axis) | ||
|
||
return result | ||
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 amended the masking logic in this function to mask all rows/columns that |
||
|
||
def _reduce( | ||
self, op, name, axis=0, skipna=True, numeric_only=None, filter_type=None, **kwds | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.