From 651bd12749e56b0b2f992c8cae51dae0ece29c65 Mon Sep 17 00:00:00 2001 From: Pontus Lurcock Date: Wed, 26 Jun 2024 20:16:09 +0200 Subject: [PATCH] Change np.core.defchararray to np.char (#9165) (#9166) * 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 * whats-new normalization --------- Co-authored-by: Justus Magin --- doc/whats-new.rst | 2 ++ xarray/testing/assertions.py | 2 +- xarray/tests/test_assertions.py | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index c3383a5648a..97631b4c324 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -35,6 +35,8 @@ Deprecations Bug fixes ~~~~~~~~~ +- Make :py:func:`testing.assert_allclose` work with numpy 2.0 (:issue:`9165`, :pull:`9166`). + By `Pontus Lurcock `_. Documentation diff --git a/xarray/testing/assertions.py b/xarray/testing/assertions.py index 69885868f83..2a4c17e115a 100644 --- a/xarray/testing/assertions.py +++ b/xarray/testing/assertions.py @@ -36,7 +36,7 @@ def wrapper(*args, **kwargs): def _decode_string_data(data): if data.dtype.kind == "S": - return np.core.defchararray.decode(data, "utf-8", "replace") + return np.char.decode(data, "utf-8", "replace") return data diff --git a/xarray/tests/test_assertions.py b/xarray/tests/test_assertions.py index aa0ea46f7db..20b5e163662 100644 --- a/xarray/tests/test_assertions.py +++ b/xarray/tests/test_assertions.py @@ -52,6 +52,11 @@ def test_allclose_regression() -> None: xr.Dataset({"a": ("x", [0, 2]), "b": ("y", [0, 1])}), id="Dataset", ), + pytest.param( + xr.DataArray(np.array("a", dtype="|S1")), + xr.DataArray(np.array("b", dtype="|S1")), + id="DataArray_with_character_dtype", + ), ), ) def test_assert_allclose(obj1, obj2) -> None: