ENH: Add where= parameter support to np.ptp #29661
Open
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.
PR Description
This PR extends
numpy.ptp
(peak-to-peak: max - min) to support thewhere=
keyword argument, consistent with other NumPy reduction functions such assum
,min
, andmax
.Changes Made
_methods._ptp
to forwardwhere
intoumr_maximum
andumr_minimum
.fromnumeric.py
to acceptwhere=np._NoValue
.where
parameter.fromnumeric.pyi
to includewhere
.test_numeric.py
(test_ptp_where
) to validate functionality.Behavior
np.ptp
now honors masks passed viawhere
, enabling selective element inclusion in peak-to-peak computation.For example:
Edge cases (e.g., all-False masks) follow the identity rules of
maximum
(-inf
) andminimum
(+inf
), resulting in-inf
for floating-point arrays.Testing
axis
,out
,keepdims
).Documentation
where
parameter description to theptp
docstring.versionadded
tag should be included to mark this feature.This aligns
ptp
with other NumPy reductions and improves API consistency.Fix #29675