Skip to content

Passing in DataArray into np.linspace breaks with Numpy 2 #9043

Open
@hoxbro

Description

@hoxbro

What happened?

Trying to be compatible with Numpy 2, I discovered the following behavior change.

I could work around it, so it's not urgent for me to be fixed and can be marked as wontfix.

What did you expect to happen?

No response

Minimal Complete Verifiable Example

import numpy as np
import xarray as xr

arr = np.array([1,2,3,4])
xarr = xr.DataArray(arr, coords=[('x', arr)])
np.linspace(xarr[0], xarr[-1], len(xarr))

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

Traceback (most recent call last):
  File "/home/shh/projects/holoviz/repos/datashader/example.py", line 6, in <module>
    np.linspace(xarr[0], xarr[-1], len(xarr))
  File "/home/shh/miniconda3/envs/datashader-dev/lib/python3.12/site-packages/numpy/_core/function_base.py", line 189, in linspace
    y = conv.wrap(y.astype(dtype, copy=False))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shh/miniconda3/envs/datashader-dev/lib/python3.12/site-packages/xarray/core/dataarray.py", line 4685, in __array_wrap__
    new_var = self.variable.__array_wrap__(obj, context)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shh/miniconda3/envs/datashader-dev/lib/python3.12/site-packages/xarray/core/variable.py", line 2294, in __array_wrap__
    return Variable(self.dims, obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shh/miniconda3/envs/datashader-dev/lib/python3.12/site-packages/xarray/core/variable.py", line 397, in __init__
    super().__init__(
  File "/home/shh/miniconda3/envs/datashader-dev/lib/python3.12/site-packages/xarray/namedarray/core.py", line 264, in __init__
    self._dims = self._parse_dimensions(dims)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shh/miniconda3/envs/datashader-dev/lib/python3.12/site-packages/xarray/namedarray/core.py", line 490, in _parse_dimensions
    raise ValueError(
ValueError: dimensions () must have the same length as the number of data dimensions, ndim=1

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS
------------------
commit: None
python: 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:38:13) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 6.8.0-76060800daily20240311-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.3
libnetcdf: 4.9.2

xarray: 2024.5.0
pandas: 2.2.2
numpy: 2.0.0rc2
scipy: 1.13.0
netCDF4: 1.6.5
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: 1.6.3
nc_time_axis: None
iris: None
bottleneck: None
dask: 2024.5.1
distributed: None
matplotlib: 3.8.4
cartopy: None
seaborn: None
numbagg: None
fsspec: 2024.5.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 69.5.1
pip: 24.0
conda: 24.1.2
pytest: 7.4.4
mypy: None
IPython: 8.24.0
sphinx: None

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