Skip to content

Commit

Permalink
adapt to NEP 51 (pydata#8064)
Browse files Browse the repository at this point in the history
* convert string and bytes items to standard python types

* [test-upstream]

* modify the expected error message
  • Loading branch information
keewis authored Sep 25, 2023
1 parent 565b23b commit bac90ab
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 2 additions & 0 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def format_item(x, timedelta_format=None, quote_strings=True):
if isinstance(x, (np.timedelta64, timedelta)):
return format_timedelta(x, timedelta_format=timedelta_format)
elif isinstance(x, (str, bytes)):
if hasattr(x, "dtype"):
x = x.item()
return repr(x) if quote_strings else x
elif hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating):
return f"{x.item():.4}"
Expand Down
3 changes: 2 additions & 1 deletion xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4116,7 +4116,8 @@ def test_setitem(self) -> None:
data4[{"dim2": [2, 3]}] = data3["var1"][{"dim2": [3, 4]}].values
data5 = data4.astype(str)
data5["var4"] = data4["var1"]
err_msg = "could not convert string to float: 'a'"
# convert to `np.str_('a')` once `numpy<2.0` has been dropped
err_msg = "could not convert string to float: .*'a'.*"
with pytest.raises(ValueError, match=err_msg):
data5[{"dim2": 1}] = "a"

Expand Down

0 comments on commit bac90ab

Please sign in to comment.