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
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHANGES
Unreleased
-----------

- Fix ``np.ptp`` dropping the units of a Quantity with NumPy 2, which no
longer dispatches it to ``Quantity.ptp``.
- Correct the pond force unit to 0.00980665 N (one gram-force); it was
previously 1000 times too large (`#44 <https://github.com/python-quantities/python-quantities/issues/44>`_).

Expand Down
11 changes: 9 additions & 2 deletions quantities/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ def __array_wrap__(self, obj, context=None, return_scalar=False):
else:
return self.__array_prepare__(obj, context)

def __array_function__(self, func, types, args, kwargs):
# NumPy 2 removed ndarray.ptp, so np.ptp no longer dispatches to
# Quantity.ptp and would drop the units.
if func is np.ptp and isinstance(args[0], Quantity):
return Quantity.ptp(*args, **kwargs)
return super().__array_function__(func, types, args, kwargs)

@with_doc(np.ndarray.__add__)
@scale_other_units
Expand Down Expand Up @@ -628,8 +634,9 @@ def nanargmax(self,axis=None, out=None):
return np.nanargmax(self.magnitude)

@with_doc(np.ptp)
def ptp(self, axis=None, out=None):
ret = np.ptp(self.magnitude, axis, None if out is None else out.magnitude)
def ptp(self, axis=None, out=None, keepdims=False):
ret = np.ptp(self.magnitude, axis, None if out is None else out.magnitude,
keepdims=keepdims)
dim = self.dimensionality
if out is None:
return Quantity(ret, dim)
Expand Down
8 changes: 8 additions & 0 deletions quantities/tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ def test_ptp(self):
self.methodWithOut('ptp', [2, 2] * pq.m, axis=0)
self.methodWithOut('ptp', [1, 1] * pq.m, axis=1)

def test_np_ptp(self):
self.assertQuantityEqual(np.ptp(self.q), 3 * pq.m)
self.assertQuantityEqual(np.ptp(self.q, axis=0), [2, 2] * pq.m)
self.assertQuantityEqual(np.ptp(self.q, 1), [1, 1] * pq.m)
self.assertQuantityEqual(
np.ptp(self.q, axis=1, keepdims=True), [[1], [1]] * pq.m
)

def test_clip(self):
self.methodWithOut(
'clip',
Expand Down
Loading