Skip to content

PERF: nanops #43311

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 2 commits into from
Aug 31, 2021
Merged
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
33 changes: 33 additions & 0 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,35 @@ def _na_for_min_count(values: np.ndarray, axis: int | None) -> Scalar | np.ndarr
return np.full(result_shape, fill_value, dtype=values.dtype)


def maybe_operate_rowwise(func):
"""
NumPy operations on C-contiguous ndarrays with axis=1 can be
very slow. Operate row-by-row and concatenate the results.
"""

@functools.wraps(func)
def newfunc(values: np.ndarray, *, axis: int | None = None, **kwargs):
if (
axis == 1
and values.ndim == 2
and values.flags["C_CONTIGUOUS"]
and values.dtype != object
):
arrs = list(values)
if kwargs.get("mask") is not None:
mask = kwargs.pop("mask")
results = [
func(arrs[i], mask=mask[i], **kwargs) for i in range(len(arrs))
]
else:
results = [func(x, **kwargs) for x in arrs]
return np.array(results)

return func(values, axis=axis, **kwargs)

return newfunc


def nanany(
values: np.ndarray,
*,
Expand Down Expand Up @@ -543,6 +572,7 @@ def nanall(

@disallow("M8")
@_datetimelike_compat
@maybe_operate_rowwise
def nansum(
values: np.ndarray,
*,
Expand Down Expand Up @@ -1111,6 +1141,7 @@ def nanargmin(


@disallow("M8", "m8")
@maybe_operate_rowwise
def nanskew(
values: np.ndarray,
*,
Expand Down Expand Up @@ -1198,6 +1229,7 @@ def nanskew(


@disallow("M8", "m8")
@maybe_operate_rowwise
def nankurt(
values: np.ndarray,
*,
Expand Down Expand Up @@ -1294,6 +1326,7 @@ def nankurt(


@disallow("M8", "m8")
@maybe_operate_rowwise
def nanprod(
values: np.ndarray,
*,
Expand Down