Skip to content

Error calling numpy nan functions on views #2149

@joshua-gould

Description

@joshua-gould

Please make sure these conditions are met

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of anndata.
  • (optional) I have confirmed this bug exists on the main branch of anndata.

Report

Code:

import anndata as ad
import numpy as np
import pandas as pd

d = ad.AnnData(X=np.arange(16).reshape((4, 4)),
               obs=pd.DataFrame(data=dict(b=[1, 2, 3, 4])))
d2 = d[[3, 2, 1]]
X = d2.X
indices = np.array([[0, 1], [1, 2]])
X2 = X[indices]
np.nanmedian(X2, axis=1)

Traceback:

Traceback (most recent call last):
  File "python3.12/site-packages/IPython/core/interactiveshell.py", line 3699, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-61-c14b7afa76ff>", line 1, in <module>
    np.nanmedian(X2, axis=1)
  File "python3.12/site-packages/numpy/lib/nanfunctions.py", line 1217, in nanmedian
    return function_base._ureduce(a, func=_nanmedian, keepdims=keepdims,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/numpy/lib/function_base.py", line 3823, in _ureduce
    r = func(a, **kwargs)
        ^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/numpy/lib/nanfunctions.py", line 1094, in _nanmedian
    return _nanmedian_small(a, axis, out, overwrite_input)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/numpy/lib/nanfunctions.py", line 1109, in _nanmedian_small
    m = np.ma.median(a, axis=axis, overwrite_input=overwrite_input)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/numpy/ma/extras.py", line 734, in median
    return _ureduce(a, func=_median, keepdims=keepdims, axis=axis, out=out,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/numpy/lib/function_base.py", line 3823, in _ureduce
    r = func(a, **kwargs)
        ^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/numpy/ma/extras.py", line 753, in _median
    asorted = sort(a, axis=axis, fill_value=fill_value)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/numpy/ma/core.py", line 7050, in sort
    a.sort(axis=axis, kind=kind, order=order,
  File "python3.12/site-packages/numpy/ma/core.py", line 5758, in sort
    self[...] = np.take_along_axis(self, sidx, axis=axis)
    ~~~~^^^^^
  File "python3.12/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/numpy/ma/core.py", line 3397, in __setitem__
    _data[indx] = dval
    ~~~~~^^^^^^
  File "anndata/src/anndata/_core/views.py", line 86, in __setitem__
    container[idx] = value

Versions

| Package | Version                  |
| ------- | ------------------------ |
| anndata | 0.1.0.dev1582+g390ad2944 |

| Dependency        | Version     |
| ----------------- | ----------- |
| python-dateutil   | 2.9.0.post0 |
| donfig            | 0.8.1.post1 |
| h5py              | 3.14.0      |
| typing_extensions | 4.15.0      |
| crc32c            | 2.7.1       |
| natsort           | 8.4.0       |
| PyYAML            | 6.0.3       |
| legacy-api-wrap   | 1.4.1       |
| setuptools        | 80.9.0      |
| pytz              | 2025.2      |
| packaging         | 25.0        |
| pandas            | 2.3.3       |
| numcodecs         | 0.16.3      |
| session-info2     | 0.2.3       |
| scipy             | 1.16.2      |
| zarr              | 3.1.3       |
| six               | 1.17.0      |
| numpy             | 2.3.3       |

| Component | Info                                                                              |
| --------- | --------------------------------------------------------------------------------- |
| Python    | 3.12.11 | packaged by conda-forge | (main, Jun  4 2025, 14:38:53) [Clang 18.1.8 ] |
| OS        | macOS-15.7-arm64-arm-64bit                                                        |
| Updated   | 2025-10-10 18:01     

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