From 1348e934bea72f40262c2b388996e01b6abd5119 Mon Sep 17 00:00:00 2001 From: Oleh Khoma-ext Date: Wed, 16 Mar 2022 14:59:51 +0100 Subject: [PATCH 1/3] #6367 Fix for time units checking could produce "unhashable type" error --- xarray/coding/times.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xarray/coding/times.py b/xarray/coding/times.py index 0eb8707f0cc..42a815300e5 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -695,7 +695,8 @@ def encode(self, variable, name=None): def decode(self, variable, name=None): dims, data, attrs, encoding = unpack_for_decoding(variable) - if "units" in attrs and attrs["units"] in TIME_UNITS: + units = attrs.get("units") + if isinstance(units, str) and units in TIME_UNITS: units = pop_to(attrs, encoding, "units") transform = partial(decode_cf_timedelta, units=units) dtype = np.dtype("timedelta64[ns]") From 9cd88ffc1e32217e772c5769d93c1a089e1cd3a6 Mon Sep 17 00:00:00 2001 From: Oleh Khoma-ext Date: Fri, 18 Mar 2022 13:58:00 +0100 Subject: [PATCH 2/3] #6367 Added test --- xarray/tests/test_conventions.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/xarray/tests/test_conventions.py b/xarray/tests/test_conventions.py index ab290955e6c..83e560e7208 100644 --- a/xarray/tests/test_conventions.py +++ b/xarray/tests/test_conventions.py @@ -416,3 +416,10 @@ def test_encoding_kwarg(self) -> None: def test_encoding_kwarg_fixed_width_string(self) -> None: # CFEncodedInMemoryStore doesn't support explicit string encodings. pass + + +class TestDecodeCFVariableWithArrayUnits: + def test_decode_cf_variable_with_array_units(self) -> None: + v = Variable(["t"], [1, 2, 3], {"units": np.array(["foobar"], dtype=object)}) + v_decoded = conventions.decode_cf_variable("test2", v) + assert_identical(v, v_decoded) From 76d5d780716f94bdee49319d8e0e0f993dc8700c Mon Sep 17 00:00:00 2001 From: Oleh Khoma-ext Date: Fri, 18 Mar 2022 14:11:40 +0100 Subject: [PATCH 3/3] #6367 Added what's new record --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b22c6e4d858..79e280ea477 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -37,6 +37,8 @@ Bug fixes - Set ``skipna=None`` for all ``quantile`` methods (e.g. :py:meth:`Dataset.quantile`) and ensure it skips missing values for float dtypes (consistent with other methods). This should not change the behavior (:pull:`6303`). By `Mathias Hauser `_. +- Fixed "unhashable type" error trying to read NetCDF file with variable having its 'units' + attribute not ``str`` (e.g. ``numpy.ndarray``) (:issue:`6368`). By `Oleh Khoma `_. Documentation ~~~~~~~~~~~~~