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

AttributeError: np.unicode_ was removed in the NumPy 2.0 release. Use np.str_ instead #8133

Closed
4 tasks done
weiji14 opened this issue Sep 1, 2023 · 1 comment · Fixed by #8117
Closed
4 tasks done
Labels

Comments

@weiji14
Copy link
Contributor

weiji14 commented Sep 1, 2023

What happened?

We've been getting this AttributeError on PyGMT's dev CI which tests nightly packages of numpy/xarray/etc. See https://github.com/GenericMappingTools/pygmt/actions/runs/6043991082/job/16401918043#step:16:40

What did you expect to happen?

Running import xarray as xr should work with numpy 2.0.

Minimal Complete Verifiable Example

mamba create --name xr_with_np2 python=3.11
mamba activate xr_with_np2
python -m pip install --pre --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple numpy pandas xarray

python -c "import xarray as xr"

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.

Relevant log output

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/mambaforge/envs/xr_with_np2/lib/python3.11/site-packages/xarray/__init__.py", line 1, in <module>
    from xarray import testing, tutorial
  File "/home/user/mambaforge/envs/xr_with_np2/lib/python3.11/site-packages/xarray/testing.py", line 10, in <module>
    from xarray.core import duck_array_ops, formatting, utils
  File "/home/user/mambaforge/envs/xr_with_np2/lib/python3.11/site-packages/xarray/core/duck_array_ops.py", line 36, in <module>
    from xarray.core import dask_array_ops, dtypes, nputils
  File "/home/user/mambaforge/envs/xr_with_np2/lib/python3.11/site-packages/xarray/core/dask_array_ops.py", line 3, in <module>
    from xarray.core import dtypes, nputils
  File "/home/user/mambaforge/envs/xr_with_np2/lib/python3.11/site-packages/xarray/core/dtypes.py", line 43, in <module>
    (np.bytes_, np.unicode_),  # numpy promotes to unicode
                ^^^^^^^^^^^
  File "/home/user/mambaforge/envs/xr_with_np2/lib/python3.11/site-packages/numpy/__init__.py", line 372, in __getattr__
    raise AttributeError(
AttributeError: `np.unicode_` was removed in the NumPy 2.0 release. Use `np.str_` instead.

Anything else we need to know?

See https://numpy.org/devdocs/release/2.0.0-notes.html#numpy-2-0-python-api-removals. Relevant code at xarray is here

# Pairs of types that, if both found, should be promoted to object dtype
# instead of following NumPy's own type-promotion rules. These type promotion
# rules match pandas instead. For reference, see the NumPy type hierarchy:
# https://numpy.org/doc/stable/reference/arrays.scalars.html
PROMOTE_TO_OBJECT: tuple[tuple[type[np.generic], type[np.generic]], ...] = (
(np.number, np.character), # numpy promotes to character
(np.bool_, np.character), # numpy promotes to character
(np.bytes_, np.unicode_), # numpy promotes to unicode
)

Environment

# packages in environment at /home/weiji/mambaforge/envs/xr_with_np2:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
bzip2                     1.0.8                h7f98852_4    conda-forge
ca-certificates           2023.7.22            hbcca054_0    conda-forge
ld_impl_linux-64          2.40                 h41732ed_0    conda-forge
libexpat                  2.5.0                hcb278e6_1    conda-forge
libffi                    3.4.2                h7f98852_5    conda-forge
libgcc-ng                 13.1.0               he5830b7_0    conda-forge
libgomp                   13.1.0               he5830b7_0    conda-forge
libnsl                    2.0.0                h7f98852_0    conda-forge
libsqlite                 3.43.0               h2797004_0    conda-forge
libuuid                   2.38.1               h0b41bf4_0    conda-forge
libzlib                   1.2.13               hd590300_5    conda-forge
ncurses                   6.4                  hcb278e6_0    conda-forge
numpy                     2.0.0.dev0               pypi_0    pypi
openssl                   3.1.2                hd590300_0    conda-forge
packaging                 23.1                     pypi_0    pypi
pandas                    2.2.0.dev0+141.g413d92d471          pypi_0    pypi
pip                       23.2.1             pyhd8ed1ab_0    conda-forge
python                    3.11.5          hab00c5b_0_cpython    conda-forge
python-dateutil           2.8.2                    pypi_0    pypi
pytz                      2023.3                   pypi_0    pypi
readline                  8.2                  h8228510_1    conda-forge
setuptools                68.1.2             pyhd8ed1ab_0    conda-forge
six                       1.16.0                   pypi_0    pypi
tk                        8.6.12               h27826a3_0    conda-forge
tzdata                    2023.3                   pypi_0    pypi
wheel                     0.41.2             pyhd8ed1ab_0    conda-forge
xarray                    2023.8.1.dev7+g1043a9e1          pypi_0    pypi
xz                        5.2.6                h166bdaf_0    conda-forge
@weiji14 weiji14 added bug needs triage Issue that has not been reviewed by xarray team member labels Sep 1, 2023
@kmuehlbauer
Copy link
Contributor

@weiji14 Thanks for the report. This is worked on in #8117.

@kmuehlbauer kmuehlbauer removed the needs triage Issue that has not been reviewed by xarray team member label Sep 1, 2023
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