Skip to content
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

WIP/DEPR: DataFrame reductions match Series behavior #36133

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
45 changes: 36 additions & 9 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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. "
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"ExtensionDtype or ObjectDtype will be performed column-wise. "
"datetime/timedelta, extension or object dtypes will be performed column-wise, and this operation will raise an error. "

"To keep the old behavior, explicitly cast columns to bool/numeric "
"dtypes."
)
if axis == 0:
if self.dtypes.apply(check_func).any():
Copy link
Member

Choose a reason for hiding this comment

The 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 any/all ? Otherwise I would also explicitly check this first.

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:
Expand Down