8000 `numpy.nanvar()` now raises an error if `out` is specified in version 6.1.5 · Issue #17353 · astropy/astropy · GitHub
[go: up one dir, main page]

Skip to content

numpy.nanvar() now raises an error if out is specified in version 6.1.5 #17353

@roytsmart

Description

@roytsmart

Description

Apparently a change in #17239 broke the out argument in np.nanvar, and now a AttributeError is raised.

Expected behavior

For this function to work the same as 6.1.4 and earlier and support the out argument

How to Reproduce

import numpy as np
import astropy.units as u

a = np.random.uniform(0, 1, size=(16, 16)) * u.mm

out = np.zeros(16) * u.mm

np.nanstd(a, axis=1, out=out)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[<ipython-input-9-807e78ea43b5>](https://localhost:8080/#) in <cell line: 1>()
----> 1 np.nanstd(a, out=out)

3 frames
[/usr/local/lib/python3.10/dist-packages/astropy/units/quantity.py](https://localhost:8080/#) in __array_function__(self, function, types, args, kwargs)
   1863                 return self._not_implemented_or_raise(function, types)
   1864 
-> 1865             result = super().__array_function__(function, types, args, kwargs)
   1866             # Fall through to return section
   1867 

[/usr/local/lib/python3.10/dist-packages/numpy/lib/nanfunctions.py](https://localhost:8080/#) in nanstd(a, axis, dtype, out, ddof, keepdims, where)
   1877 
   1878     """
-> 1879     var = nanvar(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
   1880                  keepdims=keepdims, where=where)
   1881     if isinstance(var, np.ndarray):

[/usr/local/lib/python3.10/dist-packages/astropy/units/quantity.py](https://localhost:8080/#) in __array_function__(self, function, types, args, kwargs)
   1859             function_helper = FUNCTION_HELPERS[function]
   1860             try:
-> 1861                 args, kwargs, unit, out = function_helper(*args, **kwargs)
   1862             except NotImplementedError:
   1863                 return self._not_implemented_or_raise(function, types)

[/usr/local/lib/python3.10/dist-packages/astropy/units/quantity_helper/function_helpers.py](https://localhost:8080/#) in nanvar(a, *args, **kwargs)
    258 @function_helper
    259 def nanvar(a, *args, **kwargs):
--> 260     return (a.view(np.ndarray),) + args, kwargs, a.unit**2, None
    261 
    262 

AttributeError: 'numpy.ndarray' object has no attribute 'unit'

Which outputs:

Versions

Linux-6.1.85+-x86_64-with-glibc2.35
Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0]
astropy 6.1.5
Numpy 1.26.4
pyerfa 2.0.1.4
Scipy 1.13.1
Matplotlib 3.8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0