8000 PERF: masked ops for reductions (min/max) by jorisvandenbossche · Pull Request #33261 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

PERF: masked ops for reductions (min/max) #33261

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 7 commits into from
Apr 6, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
different nesting to be mypy friendlier
  • Loading branch information
jorisvandenbossche committed Apr 3, 2020
commit cb992a22982d2dcdff1b33b283030d638338c764
59 changes: 30 additions & 29 deletions pandas/core/array_algos/masked_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,40 +47,41 @@ def sum(
return np.sum(values, where=~mask)


def _minmax(func):
def reduction(values: np.ndarray, mask: np.ndarray, skipna: bool = True):
"""
Reduction for 1D masked array.
def _minmax(func, values: np.ndarray, mask: np.ndarray, skipna: bool = True):
"""
Reduction for 1D masked array.

Parameters
----------
values : np.ndarray
Numpy array with the values (can be of any dtype that support the
operation).
mask : np.ndarray
Boolean numpy array (True values indicate missing values).
skipna : bool, default True
Whether to skip NA.
"""
if not skipna:
if mask.any():
return libmissing.NA
else:
if values.size:
return func(values)
else:
# min/max with empty array raise in numpy, pandas returns NA
return libmissing.NA
Parameters
----------
values : np.ndarray
Numpy array with the values (can be of any dtype that support the
operation).
mask : np.ndarray
Boolean numpy array (True values indicate missing values).
skipna : bool, default True
Whether to skip NA.
"""
if not skipna:
if mask.any():
return libmissing.NA
else:
subset = values[~mask]
if subset.size:
return func(values[~mask])
if values.size:
return func(values)
else:
# min/max with empty array raise in numpy, pandas returns NA
return libmissing.NA
else:
subset = values[~mask]
if subset.size:
return func(values[~mask])
else:
# min/max with empty array raise in numpy, pandas returns NA
return libmissing.NA


return reduction
def min(values: np.ndarray, mask: np.ndarray, skipna: bool = True):
return _minmax(np.min, values=values, mask=mask, skipna=skipna)


min = _minmax(np.min)
max = _minmax(np.max)
def max(values: np.ndarray, mask: np.ndarray, skipna: bool = True):
return _minmax(np.max, values=values, mask=mask, skipna=skipna)
0