Skip to content

BUG: support variable-width strings in str.extract - #11793

Merged
headtr1ck merged 8 commits into
pydata:mainfrom
jdotc1:fix/jdotc1-pr-10-xarray-string-extract-20261023
Oct 31, 2026
Merged

headtr1ck merged 8 commits into
pydata:mainfrom
jdotc1:fix/jdotc1-pr-10-xarray-string-extract-20261023

Conversation

@jdotc1

@jdotc1 jdotc1 commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Description

DataArray.str.extract(..., dim=...) fails for NumPy variable-width string arrays because elements reaching the capture helper are Python strings without a dtype attribute. Use object arrays for the intermediate captures, then retain the existing cast to the input string dtype kind.

The regression covers raw and compiled patterns for variable-width strings, Unicode and bytes, including optional groups and unmatched values. The entire string-accessor test module passed locally (265 tests), as did Ruff lint and formatting checks on the two Python files. Full project CI and the dependency/platform matrix remain outstanding.

Related to #11785; this addresses the extraction failure only.

Checklist

  • Regression tests added.
  • Visible bug fix documented in doc/whats-new.rst.
  • No public API or dependency added.

@headtr1ck headtr1ck left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jdotc1, this fixes the extract(..., dim=...) failure from #11785 (comment), and I checked it works for StringDType() and StringDType(na_object=np.nan).

While testing I found another bug in the same function, on main as well: if the first element doesn't match, extract(..., dim=...) fails, because np.vectorize sizes its output from the first result, and a non-match returns a single "" instead of one per group:

xr.DataArray(["a_1", "!"], dims="x").str.extract(r"(\w)_(\d)", dim="g")  # works
xr.DataArray(["!", "a_1"], dims="x").str.extract(r"(\w)_(\d)", dim="g")
# ValueError: could not broadcast input array from shape (2,) into shape (1,)

Since this PR touches exactly that line, could you fix it here as well?

return np.array([""] * maxgroups, dtype=np.object_)

and put the non-matching value first in the new test, e.g. ["!", "alpha_1234", "beta"].

#11785 is about converting pandas string indexes to StringDType, so the whats-new entry shouldn't link it. Could you reference this PR instead, e.g.

- :py:meth:`DataArray.str.extract` with ``dim`` now supports NumPy ``StringDType`` arrays,
  and no longer fails if the first value doesn't match (:pull:`11793`).
  By `jdotc1 <https://github.com/jdotc1>`_.

Note that the result still loses na_object (StringDType(na_object=np.nan) becomes StringDType()), since it's cast with astype(self._obj.dtype.kind). That affects several other methods as well, so it's fine to leave it for a separate PR.

@headtr1ck headtr1ck left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@headtr1ck headtr1ck added the plan to merge Final call for comments label Oct 28, 2026
@headtr1ck
headtr1ck merged commit a164fb3 into pydata:main Oct 31, 2026
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to merge Final call for comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants