Skip to content

PERF: any/all with axis=1 #44857

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 6 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 5 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5998,14 +5998,15 @@ def dropna(
raise KeyError(np.array(subset)[check].tolist())
agg_obj = self.take(indices, axis=agg_axis)

count = agg_obj.count(axis=agg_axis)

if thresh is not None:
count = agg_obj.count(axis=agg_axis)
mask = count >= thresh
elif how == "any":
mask = count == len(agg_obj._get_axis(agg_axis))
# faster equivalent to 'agg_obj.count(agg_axis) == self.shape[agg_axis]'
mask = notna(agg_obj).all(axis=agg_axis, bool_only=False)
elif how == "all":
mask = count > 0
# faster equivalent to 'agg_obj.count(agg_axis) > 0'
mask = notna(agg_obj).any(axis=agg_axis, bool_only=False)
else:
if how is not None:
raise ValueError(f"invalid how option: {how}")
Expand Down
33 changes: 33 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10349,6 +10349,39 @@ def _logical_func(
)
return res._logical_func(name, func, skipna=skipna, **kwargs)

if (
self.ndim > 1
and axis == 1
and len(self._mgr.arrays) > 1
and bool_only is not None
):
# Try to avoid a potentially-expensive transpose
Copy link
Contributor

Choose a reason for hiding this comment

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

can you throw here on down into a function and call it. (i now it has to be on self) but maybe _reduce_maybe_without_transpose or _reduce_fast or similar

Copy link
Member Author

Choose a reason for hiding this comment

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

sure. will need to do this to eventually extend this to sum and prod too.

Copy link
Member Author

Choose a reason for hiding this comment

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

(xref #44793)

arrays = self._mgr.arrays
if all(x.ndim == 2 for x in arrays):
# TODO(EA2D): special case not needed
obj = self
if bool_only:
obj = self._get_bool_data()

if name == "all":
result = np.ones(len(obj), dtype=bool)
ufunc = np.logical_and
else:
result = np.zeros(len(obj), dtype=bool)
# error: Incompatible types in assignment
# (expression has type "_UFunc_Nin2_Nout1[Literal['logical_or'],
# Literal[20], Literal[False]]", variable has type
# "_UFunc_Nin2_Nout1[Literal['logical_and'], Literal[20],
# Literal[True]]")
ufunc = np.logical_or # type: ignore[assignment]

for arr in obj._mgr.arrays:
middle = func(arr, axis=0, skipna=skipna)
result = ufunc(result, middle)

res_ser = obj._constructor_sliced(result, index=obj.index)
return res_ser

return self._reduce(
func,
name=name,
Expand Down