Skip to content
Open
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
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ Deprecations

Bug Fixes
~~~~~~~~~
- ``DataArray.str.extract`` with a named output dimension now supports NumPy
variable-width strings, including unmatched values and optional capture groups
(:issue:`11785`).
By `jdotc1 <https://github.com/jdotc1>`_.
- Results of the string accessor methods ``strip``, ``pad``, ``center``,
``ljust``, ``rjust``, ``zfill`` and ``replace`` on dask arrays are no longer
truncated to a single character, and ``repeat`` no longer fails on dask arrays
Expand Down
4 changes: 2 additions & 2 deletions xarray/core/accessor_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -2154,10 +2154,10 @@ def _get_res_single(val, pat):
def _get_res_multi(val, pat):
match = pat.search(val)
if match is None:
return np.array([""], val.dtype)
return np.array([""], dtype=np.object_)
match = match.groups()
match = [grp if grp is not None else "" for grp in match]
return np.array(match, val.dtype)
return np.array(match, dtype=np.object_)

if dim is None:
return self._apply(func=_get_res_single, func_args=(pat,))
Expand Down
23 changes: 23 additions & 0 deletions xarray/tests/test_accessor_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,29 @@ def test_extract_single_nocase(dtype) -> None:
assert_equal(res_re_dim, targ_dim)


@pytest.mark.parametrize("string_dtype", ["T", "U", "S"])
@pytest.mark.parametrize("compiled", [False, True])
def test_extract_multi_string_dtypes(string_dtype, compiled) -> None:
value = xr.DataArray(
np.array(["alpha_1234", "beta", "!"], dtype=string_dtype), dims="X"
)
pattern_str = r"([a-z]+)(?:_(\d+))?"
pattern = (
re.compile(pattern_str.encode() if string_dtype == "S" else pattern_str)
if compiled
else pattern_str
)
expected = xr.DataArray(
np.array([["alpha", "1234"], ["beta", ""], ["", ""]], dtype=string_dtype),
dims=["X", "group"],
)

result = value.str.extract(pattern, dim="group")

assert result.dtype == expected.dtype
assert_equal(result, expected)


def test_extract_multi_case(dtype) -> None:
pat_str = r"(\w+)_Xy_(\d*)"
pat_re: str | bytes = (
Expand Down
Loading