Skip to content

Commit

Permalink
array_function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed Dec 27, 2024
1 parent d1eb38d commit 3189633
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pint_pandas/pint_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ def __setstate__(self, dct):
self.__dict__.update(dct)
self._Q = self.dtype.ureg.Quantity

def __array_function__(self, func, types, args, kwargs):
args = convert_np_inputs(args)
result = func(*args, **kwargs)
return self._convert_np_result(result)

def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
out = kwargs.get("out", ())
for x in inputs + out:
Expand Down
13 changes: 13 additions & 0 deletions pint_pandas/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,16 @@ def test_issue246(self):

# now an operation where each cell is independent from each other
df.apply(lambda x: x * 2, axis=1)


class TestIssue255(BaseExtensionTests):
def test_issue255(self):
a = np.r_[1, 2, np.nan, 4, 10]
pa = PintArray.from_1darray_quantity(a * ureg.m)

result = np.clip(pa, 3 * ureg.m, 5 * ureg.m)

e = np.clip(a, 3, 5)
expected = PintArray.from_1darray_quantity(e * ureg.m)

tm.assert_equal(result, expected)

0 comments on commit 3189633

Please sign in to comment.