Skip to content

Conversation

KAVYANSHTYAGI
Copy link

@KAVYANSHTYAGI KAVYANSHTYAGI commented Sep 3, 2025

PR Description

This PR extends numpy.ptp (peak-to-peak: max - min) to support the where= keyword argument, consistent with other NumPy reduction functions such as sum, min, and max.

Changes Made

  • Updated _methods._ptp to forward where into umr_maximum and umr_minimum.
  • Extended public API in fromnumeric.py to accept where=np._NoValue.
  • Updated docstrings to document the new where parameter.
  • Updated type hints in fromnumeric.pyi to include where.
  • Added unit tests in test_numeric.py (test_ptp_where) to validate functionality.

Behavior

  • np.ptp now honors masks passed via where, enabling selective element inclusion in peak-to-peak computation.

  • For example:

    >>> import numpy as np
    >>> a = np.array([1, 5, 7, 0])
    >>> where = [True, False, True, False]
    >>> np.ptp(a, where=where)
    6
  • Edge cases (e.g., all-False masks) follow the identity rules of maximum (-inf) and minimum (+inf), resulting in -inf for floating-point arrays.

Testing

  • Added direct test for masked computation.
  • Verified broadcasting behavior and compatibility with existing parameters (axis, out, keepdims).

Documentation

  • Added where parameter description to the ptp docstring.
  • versionadded tag should be included to mark this feature.

This aligns ptp with other NumPy reductions and improves API consistency.


Fix #29675

@mattip
Copy link
Member

mattip commented Sep 5, 2025

Before diving in to fix the failures, we should discuss the necessity for this change. Is there an open issue that describes the real world use case for adding the where argument to ptp?

Copy link
Author

@KAVYANSHTYAGI KAVYANSHTYAGI left a comment

Choose a reason for hiding this comment

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

@mattip, I have opened an issue in which the whole scenario is elaborated. Thank you

return (a, out)


@array_function_dispatch(_ptp_dispatcher)
def ptp(a, axis=None, out=None, keepdims=np._NoValue):
def ptp(a, axis=None, out=None, keepdims=np._NoValue, where=np._NoValue):
Copy link
Member

Choose a reason for hiding this comment

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

This should be kwarg only.

FWIW, I don't care much to do this, but I agree with Matti in that I am curious where this type of issues come from. It feels like a lot more recently and context matters.

For example, if an automatic tool says: oh this might match better if it has a where=, then we'll never see the end of it.
OTOH, if you have an actual use, that is a reason to just do it (when there is little downside otherwise).

(The matter would be different if a maintainer spearheaded this so that we have dedicated review bandwidth.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Add where= parameter support to np.ptp for consistency with other reductions
3 participants