8000 BUG: ensure nanvar(angle) drops to quantity (instead of raising) by mhvk · Pull Request #17239 · astropy/astropy · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
22 changes: 22 additions & 0 deletions astropy/coordinates/tests/test_angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,28 @@ def test_angle_methods():
assert a_min == 0.0 * u.degree


def test_angle_nan_functions():
# Most numpy functions tested as part of the Quantity tests.
# But check that we drop to Quantity when appropriate; see
# https://github.com/astropy/astropy/pull/17221#discussion_r1813060768
a = Angle([0.0, 2.0, np.nan], "deg")
a_mean = np.nanmean(a)
assert type(a_mean) is Angle
assert a_mean == 1.0 * u.degree
a_std = np.nanstd(a)
assert type(a_std) is Angle
assert a_std == 1.0 * u.degree
a_var = np.nanvar(a)
assert type(a_var) is u.Quantity
assert a_var == 1.0 * u.degree**2
a_max = np.nanmax(a)
assert type(a_max) is Angle
assert a_max == 2.0 * u.degree
a_min = np.nanmin(a)
assert type(a_min) is Angle
assert a_min == 0.0 * u.degree


def test_angle_convert():
"""
Test unit conversion of Angle objects
Expand Down
15 changes: 13 additions & 2 deletions astropy/units/quantity_helper/function_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,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.nansum, np.nancumsum, np.nanstd, np.nanvar,
np.nanprod, np.nancumprod,
np.nansum, np.nancumsum, np.nanprod, np.nancumprod,
np.einsum_path, np.linspace,
np.sort, np.partition, np.meshgrid,
np.common_type, np.result_type, np.can_cast, np.min_scalar_type,
Expand Down Expand Up @@ -219,6 +218,7 @@ def __call__(self, f=None, helps=None, module=np):
np.fft.fft2, np.fft.ifft2, np.fft.rfft2, np.fft.irfft2,
np.fft.fftn, np.fft.ifftn, np.fft.rfftn, np.fft.irfftn,
np.fft.hfft, np.fft.ihfft,
np.nanstd, # See comment on nanvar helper.
np.linalg.eigvals, np.linalg.eigvalsh,
} | ({np.asfarray} if NUMPY_LT_2_0 else set()) # noqa: NPY201
) # fmt: skip
Expand Down Expand Up @@ -252,6 +252,17 @@ def like_helper(a, *args, **kwargs):
return (a.view(np.ndarray),) + args, kwargs, unit, None


# nanvar is safe for Quantity and was previously in SUBCLASS_FUNCTIONS, but it
# is not safe for Angle, since the resulting unit is inconsistent with being
# an Angle. By using FUNCTION_HELPERS, the unit gets passed through
# _result_as_quantity, which will correctly drop to Quantity.
# A side effect would be that np.nanstd then also produces Quantity; this
# is avoided by it being helped by invariant_a_helpers above.
@function_helper
def nanvar(a, *args, **kwargs):
return (a.view(np.ndarray),) + args, kwargs, a.unit**2, None


@function_helper
def sinc(x):
from astropy.units.si import radian
Expand Down
2 changes: 2 additions & 0 6C0D deletions docs/changes/coordinates/17239.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``np.nanvar(angle)`` now produces a ``Quantity`` with the correct
unit, rather than raising an exception.
0