Skip to content

Commit

Permalink
Backport PR astropy#15228: Bugfix for short version of np.nanmedian
Browse files Browse the repository at this point in the history
  • Loading branch information
mhvk authored and meeseeksmachine committed Aug 27, 2023
1 parent afeeaa6 commit 357c997
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion astropy/units/quantity_helper/function_helpers.py
Expand Up @@ -83,7 +83,7 @@
np.isposinf, np.isneginf, np.isreal, np.iscomplex,
np.average, np.mean, np.std, np.var, np.trace,
np.nanmax, np.nanmin, np.nanargmin, np.nanargmax, np.nanmean,
np.nanmedian, np.nansum, np.nancumsum, np.nanstd, np.nanvar,
np.nansum, np.nancumsum, np.nanstd, np.nanvar,
np.nanprod, np.nancumprod,
np.einsum_path, np.trapz, np.linspace,
np.sort, np.partition, np.meshgrid,
Expand Down Expand Up @@ -574,6 +574,11 @@ def percentile(a, q, *args, **kwargs):
return quantile(a, q, *args, _q_unit=percent, **kwargs)


@function_helper
def nanmedian(a, axis=None, out=None, **kwargs):
return _iterable_helper(a, axis=axis, out=out, **kwargs)


@function_helper
def count_nonzero(a, *args, **kwargs):
return (a.value,) + args, kwargs, None, None
Expand Down
11 changes: 9 additions & 2 deletions astropy/units/tests/test_quantity_non_ufuncs.py
Expand Up @@ -1006,8 +1006,15 @@ def test_nanargmax(self):
def test_nanmean(self):
self.check(np.nanmean)

def test_nanmedian(self):
self.check(np.nanmedian)
@pytest.mark.parametrize("axis", [None, 0, 1, -1])
def test_nanmedian(self, axis):
self.check(np.nanmedian, axis=axis)

def test_nanmedian_out(self):
out = np.empty_like(self.q)
o = np.nanmedian(self.q, out=out)
assert o is out
assert np.all(o == np.nanmedian(self.q))

def test_nansum(self):
self.check(np.nansum)
Expand Down
3 changes: 3 additions & 0 deletions docs/changes/units/15228.bugfix.rst
@@ -0,0 +1,3 @@
Fixed ``astropy.units.Quantity``'s implementation of ``numpy.nanmedian()``,
where for Numpy >= 1.25 an exception was raised for some array shapes and axis
combinations.

0 comments on commit 357c997

Please sign in to comment.