Skip to content

Commit 651bd12

Browse files
pont-uskeewis
andauthored
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 <keewis@users.noreply.github.com> * whats-new normalization --------- Co-authored-by: Justus Magin <keewis@users.noreply.github.com>
1 parent 19d0fbf commit 651bd12

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Deprecations
3535

3636
Bug fixes
3737
~~~~~~~~~
38+
- Make :py:func:`testing.assert_allclose` work with numpy 2.0 (:issue:`9165`, :pull:`9166`).
39+
By `Pontus Lurcock <https://github.com/pont-us>`_.
3840

3941

4042
Documentation

xarray/testing/assertions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def wrapper(*args, **kwargs):
3636

3737
def _decode_string_data(data):
3838
if data.dtype.kind == "S":
39-
return np.core.defchararray.decode(data, "utf-8", "replace")
39+
return np.char.decode(data, "utf-8", "replace")
4040
return data
4141

4242

xarray/tests/test_assertions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ def test_allclose_regression() -> None:
5252
xr.Dataset({"a": ("x", [0, 2]), "b": ("y", [0, 1])}),
5353
id="Dataset",
5454
),
55+
pytest.param(
56+
xr.DataArray(np.array("a", dtype="|S1")),
57+
xr.DataArray(np.array("b", dtype="|S1")),
58+
id="DataArray_with_character_dtype",
59+
),
5560
),
5661
)
5762
def test_assert_allclose(obj1, obj2) -> None:

0 commit comments

Comments
 (0)