Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6367 Fix for time units checking could produce "unhashable type" error #6368

Merged
merged 4 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Bug fixes
- Many bugs fixed by the explicit indexes refactor, mainly related to multi-index (virtual)
coordinates. See the corresponding pull-request on GitHub for more details. (:pull:`5692`).
By `Benoît Bovy <https://github.com/benbovy>`_.
- 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 <https://github.com/okhoma>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
3 changes: 2 additions & 1 deletion xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
okhoma marked this conversation as resolved.
Show resolved Hide resolved
units = pop_to(attrs, encoding, "units")
transform = partial(decode_cf_timedelta, units=units)
dtype = np.dtype("timedelta64[ns]")
Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)