Skip to content

Numpy string coding #5264

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

Merged
merged 13 commits into from
Dec 30, 2021
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Deprecations

Bug fixes
~~~~~~~~~
- Subclasses of ``byte`` and ``str`` (e.g. ``np.str_`` and ``np.bytes_``) will now serialise to disk rather than raising a ``ValueError: unsupported dtype for netCDF4 variable: object`` as they did previously (:pull:`5264`).
By `Zeb Nicholls <https://github.com/znicholls>`_.

- Fix applying function with non-xarray arguments using :py:func:`xr.map_blocks`.
By `Cindy Chiao <https://github.com/tcchiao>`_.

Expand Down
2 changes: 2 additions & 0 deletions xarray/coding/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@


def create_vlen_dtype(element_type):
if element_type not in (str, bytes):
raise TypeError("unsupported type for vlen_dtype: {!r}".format(element_type))
# based on h5py.special_dtype
return np.dtype("O", metadata={"element_type": element_type})

Expand Down
8 changes: 6 additions & 2 deletions xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ def _infer_dtype(array, name=None):
return np.dtype(float)

element = array[(0,) * array.ndim]
if isinstance(element, (bytes, str)):
return strings.create_vlen_dtype(type(element))
# We use the base types to avoid subclasses of bytes and str (which might
# not play nice with e.g. hdf5 datatypes), such as those from numpy
if isinstance(element, bytes):
return strings.create_vlen_dtype(bytes)
elif isinstance(element, str):
return strings.create_vlen_dtype(str)

dtype = np.array(element).dtype
if dtype.kind != "O":
Expand Down
21 changes: 21 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5393,3 +5393,24 @@ def test_h5netcdf_entrypoint(tmp_path):
assert entrypoint.guess_can_open("something-local.nc4")
assert entrypoint.guess_can_open("something-local.cdf")
assert not entrypoint.guess_can_open("not-found-and-no-extension")


@requires_netCDF4
@pytest.mark.parametrize("str_type", (str, np.str_))
def test_write_file_from_np_str(str_type, tmpdir) -> None:
# https://github.com/pydata/xarray/pull/5264
scenarios = [str_type(v) for v in ["scenario_a", "scenario_b", "scenario_c"]]
years = range(2015, 2100 + 1)
tdf = pd.DataFrame(
data=np.random.random((len(scenarios), len(years))),
columns=years,
index=scenarios,
)
tdf.index.name = "scenario"
tdf.columns.name = "year"
tdf = tdf.stack()
tdf.name = "tas"

txr = tdf.to_xarray()

txr.to_netcdf(tmpdir.join("test.nc"))
6 changes: 6 additions & 0 deletions xarray/tests/test_coding_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def test_vlen_dtype() -> None:
assert strings.check_vlen_dtype(np.dtype(object)) is None


@pytest.mark.parametrize("numpy_str_type", (np.str_, np.bytes_))
def test_numpy_subclass_handling(numpy_str_type) -> None:
with pytest.raises(TypeError, match="unsupported type for vlen_dtype"):
strings.create_vlen_dtype(numpy_str_type)


def test_EncodedStringCoder_decode() -> None:
coder = strings.EncodedStringCoder()

Expand Down