Repository navigation
Conversation
Preserve the string extraction fix on the current upstream base. Co-authored-by: ChatGPT <[email protected]>
headtr1ck
left a comment
There was a problem hiding this comment.
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.
Description
DataArray.str.extract(..., dim=...)fails for NumPy variable-width string arrays because elements reaching the capture helper are Python strings without adtypeattribute. 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
doc/whats-new.rst.