From b5d2e8423f45c56173ec3f50fd935dff4590d926 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Thu, 10 Aug 2023 14:36:46 +0200 Subject: [PATCH 1/3] convert string and bytes items to standard python types --- xarray/core/formatting.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 1f2bf720a10..18fffcae8ef 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -156,6 +156,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}" From dc41da4faa9a7467b0d4297092bda5a799a929b4 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Thu, 10 Aug 2023 17:44:34 +0200 Subject: [PATCH 2/3] [test-upstream] From 44ddeda08ba6762a43259379e7e11efe3e9d6e4a Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Wed, 20 Sep 2023 11:49:38 +0200 Subject: [PATCH 3/3] modify the expected error message --- xarray/tests/test_dataset.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 1513c6dba94..708fcde411b 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -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"