Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing.assert_allclose on string dtype gives error with NumPy 2.0.0 #9165

Closed
5 tasks done
pont-us opened this issue Jun 24, 2024 · 4 comments · Fixed by #9166
Closed
5 tasks done

testing.assert_allclose on string dtype gives error with NumPy 2.0.0 #9165

pont-us opened this issue Jun 24, 2024 · 4 comments · Fixed by #9166
Labels

Comments

@pont-us
Copy link
Contributor

pont-us commented Jun 24, 2024

What happened?

This error came up in an nc2zarr unit test in a nightly CI run, just after the release of NumPy 2.0.0. This is the test in question, and here is the call to xarray.testing.allclose which triggers the error:

AttributeError: Module 'numpy._core' has no attribute 'defchararray'

What did you expect to happen?

We expected that xarray.testing.allclose would perform as stated rather than producing an error.

Minimal Complete Verifiable Example

import numpy as np
import xarray as xr

xr.testing.assert_allclose(
    da := xr.DataArray(np.array("a", dtype="|S1")),
    da.copy()
)

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/pont/allclose_error.py", line 6, in <module>
    xr.testing.assert_allclose(
  File "/home/pont/loc/repos/xarray/xarray/testing/assertions.py", line 32, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/pont/loc/repos/xarray/xarray/testing/assertions.py", line 282, in assert_allclose
    ) and compat_variable(a.variable, b.variable)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pont/loc/repos/xarray/xarray/testing/assertions.py", line 274, in compat_variable
    return a.dims == b.dims and (a._data is b._data or equiv(a.data, b.data))
                                                       ^^^^^^^^^^^^^^^^^^^^^
  File "/home/pont/loc/repos/xarray/xarray/testing/assertions.py", line 45, in _data_allclose_or_equiv
    arr1 = _decode_string_data(arr1)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pont/loc/repos/xarray/xarray/testing/assertions.py", line 39, in _decode_string_data
    return np.core.defchararray.decode(data, "utf-8", "replace")
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/pont/mambaforge/envs/xrrepo/lib/python3.12/site-packages/numpy/core/__init__.py", line 30, in __getattr__
    attr = getattr(_core, attr_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pont/mambaforge/envs/xrrepo/lib/python3.12/site-packages/numpy/_core/__init__.py", line 167, in __getattr__
    raise AttributeError(f"Module {__name__!r} has no attribute {name!r}")
AttributeError: Module 'numpy._core' has no attribute 'defchararray'

Anything else we need to know?

Error appears to be due to a failed import of defchararray from a path that's no longer valid in NumPy 2. numpy._core.defchararray says "The preferred alias for defchararray is numpy.char.". Replacing np.core.defchararray with np.char.chararray in xarray.testing.assertions seems to fix it.

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.4 | packaged by conda-forge | (main, Jun 17 2024, 10:23:07) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 5.15.0-107-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: ('en_GB', 'UTF-8')
libhdf5: 1.14.2
libnetcdf: 4.9.3-development

xarray: 0.1.dev5453+g872c1c5
pandas: 2.2.2
numpy: 2.0.0
scipy: 1.13.1
netCDF4: 1.7.1
pydap: None
h5netcdf: 1.3.0
h5py: 3.11.0
zarr: 2.18.2
cftime: 1.6.4
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.4.0
dask: 2024.6.2
distributed: 2024.6.2
matplotlib: 3.9.0
cartopy: None
seaborn: 0.13.2
numbagg: 0.8.1
fsspec: 2024.6.0
cupy: None
pint: None
sparse: None
flox: 0.9.8
numpy_groupies: 0.11.1
setuptools: 70.1.0
pip: 24.0
conda: None
pytest: 8.2.2
mypy: 1.10.0
IPython: 8.25.0
sphinx: None

@pont-us pont-us added bug needs triage Issue that has not been reviewed by xarray team member labels Jun 24, 2024
@keewis
Copy link
Collaborator

keewis commented Jun 24, 2024

Thanks for the report, @pont-us.

Only the most recent version of xarray is compatible with numpy>=2 (I remember fixing that kind of issue in the past). Your environment shows that you installed from a git repo, can you make sure you're at main?

@keewis
Copy link
Collaborator

keewis commented Jun 24, 2024

actually, I can reproduce this locally... maybe our tests are not as extensive as I had hoped they are?

@keewis
Copy link
Collaborator

keewis commented Jun 24, 2024

good news is, this can be easily fixed by replacing np.core.defchararray.decode with np.char.decode. We'll probably need to extend our tests, though.

@keewis keewis removed the needs triage Issue that has not been reviewed by xarray team member label Jun 24, 2024
@pont-us
Copy link
Contributor Author

pont-us commented Jun 24, 2024

Your environment shows that you installed from a git repo, can you make sure you're at main?

Yes, I'm on main at commit 872c1c5.

pont-us added a commit to pont-us/xarray that referenced this issue Jun 24, 2024
Replace a reference to np.core.defchararray with np.char.chararray
in xarray.testing.assertions, since the former no longer works on
NumPy 2.0.0 and the latter is the "preferred alias" according to
NumPy docs. See Issue pydata#9165.
pont-us added a commit to pont-us/xarray that referenced this issue Jun 25, 2024
pont-us added a commit to pont-us/xarray that referenced this issue Jun 25, 2024
max-sixty pushed a commit that referenced this issue Jun 26, 2024
* Change np.core.defchararray to np.char.chararray (#9165)

Replace a reference to np.core.defchararray with np.char.chararray
in xarray.testing.assertions, since the former no longer works on
NumPy 2.0.0 and the latter is the "preferred alias" according to
NumPy docs. See Issue #9165.

* Add test for assert_allclose on dtype S (#9165)

* Use np.char.decode, not np.char.chararray.decode

... in assertions._decode_string_data. See #9166.

* List #9165 fix in whats-new.rst

* cross-like the fixed function

* Improve a parameter ID in tests.test_assertions

Co-authored-by: Justus Magin <keewis@users.noreply.github.com>

* whats-new normalization

---------

Co-authored-by: Justus Magin <keewis@users.noreply.github.com>
dcherian added a commit to dcherian/xarray that referenced this issue Jun 28, 2024
* main:
  promote floating-point numeric datetimes to 64-bit before decoding (pydata#9182)
  also pin `numpy` in the all-but-dask CI (pydata#9184)
  temporarily remove `pydap` from CI (pydata#9183)
  temporarily pin `numpy<2` (pydata#9181)
  Change np.core.defchararray to np.char (pydata#9165) (pydata#9166)
  Fix example code formatting for CachingFileManager (pydata#9178)
  Slightly improve DataTree repr (pydata#9064)
  switch to unit `"D"` (pydata#9170)
  Docs: Add page with figure for navigating help resources (pydata#9147)
  Add test for pydata#9155 (pydata#9161)
  Remove mypy exclusions for a couple more libraries (pydata#9160)
  Include numbagg in type checks (pydata#9159)
  Improve zarr chunks docs (pydata#9140)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants