Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions numpy/_core/_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *,

return ret

def _ptp(a, axis=None, out=None, keepdims=False):
def _ptp(a, axis=None, out=None, keepdims=False, where=True):
return um.subtract(
umr_maximum(a, axis, None, out, keepdims),
umr_minimum(a, axis, None, None, keepdims),
umr_maximum(a, axis, None, out, keepdims, _NoValue, where),
umr_minimum(a, axis, None, None, keepdims, _NoValue, where),
out
)

Expand Down
12 changes: 9 additions & 3 deletions numpy/_core/fromnumeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2953,12 +2953,12 @@ def cumsum(a, axis=None, dtype=None, out=None):
return _wrapfunc(a, 'cumsum', axis=axis, dtype=dtype, out=out)


def _ptp_dispatcher(a, axis=None, out=None, keepdims=None):
def _ptp_dispatcher(a, axis=None, out=None, keepdims=None, where=None):
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.)

"""
Range of values (maximum - minimum) along an axis.
Expand Down Expand Up @@ -2998,6 +2998,10 @@ def ptp(a, axis=None, out=None, keepdims=np._NoValue):
sub-class' method does not implement `keepdims` any
exceptions will be raised.
where : array_like of bool, optional
Elements to include in the peak-to-peak. See
`~numpy.ufunc.reduce` for details.
Returns
-------
ptp : ndarray or scalar
Expand Down Expand Up @@ -3038,7 +3042,9 @@ def ptp(a, axis=None, out=None, keepdims=np._NoValue):
"""
kwargs = {}
if keepdims is not np._NoValue:
kwargs['keepdims'] = keepdims
kwargs["keepdims"] = keepdims
if where is not np._NoValue:
kwargs["where"] = where
return _methods._ptp(a, axis=axis, out=out, **kwargs)


Expand Down
4 changes: 4 additions & 0 deletions numpy/_core/fromnumeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1004,20 +1004,23 @@ def ptp(
axis: None = None,
out: None = None,
keepdims: Literal[False] = ...,
where: _ArrayLikeBool_co = ...,
) -> _ScalarT: ...
@overload
def ptp(
a: ArrayLike,
axis: _ShapeLike | None = None,
out: None = None,
keepdims: bool = ...,
where: _ArrayLikeBool_co = ...,
) -> Any: ...
@overload
def ptp(
a: ArrayLike,
axis: _ShapeLike | None,
out: _ArrayT,
keepdims: bool = ...,
where: _ArrayLikeBool_co = ...,
) -> _ArrayT: ...
@overload
def ptp(
Expand All @@ -1026,6 +1029,7 @@ def ptp(
*,
out: _ArrayT,
keepdims: bool = ...,
where: _ArrayLikeBool_co = ...,
) -> _ArrayT: ...

@overload
Expand Down
5 changes: 5 additions & 0 deletions numpy/_core/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ def test_ptp(self):
a = [3, 4, 5, 10, -3, -5, 6.0]
assert_equal(np.ptp(a, axis=0), 15.0)

def test_ptp_where(self):
a = np.array([1, 5, 7, 0])
where = [True, False, True, False]
assert_equal(np.ptp(a, where=where), 6)

def test_prod(self):
arr = [[1, 2, 3, 4],
[5, 6, 7, 9],
Expand Down
Loading