-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
WIP/DEPR: DataFrame reductions match Series behavior #36133
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8681,6 +8681,17 @@ def blk_func(values): | |
|
||
assert numeric_only is None | ||
|
||
def reduce_columnwise(self, func): | ||
from pandas.core.apply import frame_apply | ||
|
||
opa = frame_apply( | ||
self, func=func, result_type="expand", ignore_failures=True | ||
) | ||
result = opa.get_result() | ||
if result.ndim == self.ndim and len(result): | ||
result = result.iloc[0].rename(None) | ||
return result | ||
|
||
if not self._is_homogeneous_type or self._mgr.any_extension_types: | ||
# try to avoid self.values call | ||
|
||
|
@@ -8698,15 +8709,7 @@ def blk_func(values): | |
# numeric_only and yet we have tried a | ||
# column-by-column reduction, where we have mixed type. | ||
# So let's just do what we can | ||
from pandas.core.apply import frame_apply | ||
|
||
opa = frame_apply( | ||
self, func=func, result_type="expand", ignore_failures=True | ||
) | ||
result = opa.get_result() | ||
if result.ndim == self.ndim: | ||
result = result.iloc[0].rename(None) | ||
return result | ||
return reduce_columnwise(self, func) | ||
|
||
data = self | ||
values = data.values | ||
|
@@ -8738,6 +8741,30 @@ def blk_func(values): | |
|
||
if constructor is not None: | ||
result = self._constructor_sliced(result, index=labels) | ||
|
||
check_func = ( | ||
lambda x: is_extension_array_dtype(x) | ||
or is_object_dtype(x) | ||
or needs_i8_conversion(x) | ||
) | ||
msg = ( | ||
"In a future version, DataFrame reduction operations with " | ||
"ExtensionDtype or ObjectDtype will be performed column-wise. " | ||
"To keep the old behavior, explicitly cast columns to bool/numeric " | ||
"dtypes." | ||
) | ||
if axis == 0: | ||
if self.dtypes.apply(check_func).any(): | ||
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. Do we know of other ops where this is a problem, or only |
||
v2 = reduce_columnwise(self, func) | ||
if type(v2) != type(result) or not v2.equals(result): | ||
warnings.warn(msg, FutureWarning) | ||
else: | ||
if self.dtypes.apply(check_func).any(): | ||
v1 = reduce_columnwise(self.T, func) | ||
v2 = reduce_columnwise(_get_data(True).T, func) | ||
if not (v1.equals(v2) and result.equals(v2)): | ||
warnings.warn(msg, FutureWarning) | ||
|
||
return result | ||
|
||
def nunique(self, axis=0, dropna=True) -> Series: | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.