Skip to content

Commit 073512e

Browse files
authored
#6367 Fix for time units checking could produce "unhashable type" error (#6368)
1 parent 3ead17e commit 073512e

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Bug fixes
4848
- Many bugs fixed by the explicit indexes refactor, mainly related to multi-index (virtual)
4949
coordinates. See the corresponding pull-request on GitHub for more details. (:pull:`5692`).
5050
By `Benoît Bovy <https://github.com/benbovy>`_.
51+
- Fixed "unhashable type" error trying to read NetCDF file with variable having its 'units'
52+
attribute not ``str`` (e.g. ``numpy.ndarray``) (:issue:`6368`). By `Oleh Khoma <https://github.com/okhoma>`_.
5153

5254
Documentation
5355
~~~~~~~~~~~~~

xarray/coding/times.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,8 @@ def encode(self, variable, name=None):
695695
def decode(self, variable, name=None):
696696
dims, data, attrs, encoding = unpack_for_decoding(variable)
697697

698-
if "units" in attrs and attrs["units"] in TIME_UNITS:
698+
units = attrs.get("units")
699+
if isinstance(units, str) and units in TIME_UNITS:
699700
units = pop_to(attrs, encoding, "units")
700701
transform = partial(decode_cf_timedelta, units=units)
701702
dtype = np.dtype("timedelta64[ns]")

xarray/tests/test_conventions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,10 @@ def test_encoding_kwarg(self) -> None:
416416
def test_encoding_kwarg_fixed_width_string(self) -> None:
417417
# CFEncodedInMemoryStore doesn't support explicit string encodings.
418418
pass
419+
420+
421+
class TestDecodeCFVariableWithArrayUnits:
422+
def test_decode_cf_variable_with_array_units(self) -> None:
423+
v = Variable(["t"], [1, 2, 3], {"units": np.array(["foobar"], dtype=object)})
424+
v_decoded = conventions.decode_cf_variable("test2", v)
425+
assert_identical(v, v_decoded)

0 commit comments

Comments
 (0)