-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: fix DataFrame.apply returning wrong result when dealing with dtype (#28773) #30304
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
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d515b5b
BUG: fix DataFrame.apply returning wrong result when dealing with dty…
d045aed
Merge remote-tracking branch 'upstream/master' into apply_bug
85a2af7
Reformatting
Reksbril 7b6e793
Resolve conflict with iterator variables
Reksbril dcaf64b
Change approach to #28773 fix
f47b420
Merge branch 'master' into apply_bug
Reksbril a0b89e9
Update apply.py
Reksbril 9f24abb
Filter out mixed-dtypes from regular standard_apply procedure
6eb6d08
Merge branch 'master' into apply_bug
Reksbril b45b44d
Remove whitespace from test_apply.py
Reksbril 3755138
Refactor
fdb2f43
Black pandas refactor
78f0154
Merge remote-tracking branch 'upstream/master' into apply_bug
1f81b77
apply fix
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,10 @@ | |
is_list_like, | ||
is_sequence, | ||
) | ||
from pandas.core.dtypes.generic import ABCSeries | ||
from pandas.core.dtypes.generic import ( | ||
ABCSeries, | ||
ABCMultiIndex, | ||
) | ||
|
||
from pandas.core.construction import create_series_with_explicit_dtype | ||
|
||
|
@@ -271,16 +274,22 @@ def apply_standard(self): | |
|
||
# we cannot reduce using non-numpy dtypes, | ||
# as demonstrated in gh-12244 | ||
if ( | ||
can_reduce = ( | ||
self.result_type in ["reduce", None] | ||
and not self.dtypes.apply(is_extension_array_dtype).any() | ||
# Disallow complex_internals since libreduction shortcut | ||
# cannot handle MultiIndex | ||
and not isinstance(self.agg_axis, ABCMultiIndex) | ||
# Disallow dtypes where setting _index_data will break | ||
# ExtensionArray values, see GH#31182 | ||
and not self.dtypes.apply(lambda x: x.kind in ["m", "M"]).any() | ||
# Disallow complex_internals since libreduction shortcut raises a TypeError | ||
and not self.agg_axis._has_complex_internals | ||
): | ||
) | ||
|
||
column_by_column = (self.axis != 0 and self.axis != "index") or self.obj._is_homogeneous_type | ||
|
||
if can_reduce and column_by_column: | ||
values = self.values | ||
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. @Reksbril the underlying problem here is that this call to |
||
index = self.obj._get_axis(self.axis) | ||
labels = self.agg_axis | ||
|
@@ -309,9 +318,17 @@ def apply_standard(self): | |
else: | ||
return self.obj._constructor_sliced(result, index=labels) | ||
|
||
|
||
# compute the result using the series generator | ||
results, res_index = self.apply_series_generator() | ||
|
||
if can_reduce and not column_by_column: | ||
results = list(results.values()) | ||
results = np.array(results) | ||
return self.obj._constructor_sliced( | ||
results, index=res_index | ||
) | ||
|
||
# wrap results | ||
return self.wrap_results(results, res_index) | ||
|
||
|
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.