Skip to content

Commit

Permalink
Change np.core.defchararray to np.char (#9165) (#9166)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
pont-us and keewis authored Jun 26, 2024
1 parent 19d0fbf commit 651bd12
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/pont-us>`_.


Documentation
Expand Down
2 changes: 1 addition & 1 deletion xarray/testing/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 651bd12

Please sign in to comment.