Skip to content

BUG: Preserve string dtype in extract #31018

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 1 commit into from
Jan 15, 2020
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
3 changes: 2 additions & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,11 +884,12 @@ def _str_extract_noexpand(arr, pat, flags=0):
if arr.empty:
result = DataFrame(columns=columns, dtype=object)
else:
dtype = _result_dtype(arr)
result = DataFrame(
[groups_or_na(val) for val in arr],
columns=columns,
index=arr.index,
dtype=object,
dtype=dtype,
)
return result, name

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3573,3 +3573,18 @@ def test_string_array_boolean_array(method, expected):
result = getattr(s.str, method)()
expected = Series(expected, dtype="boolean")
tm.assert_series_equal(result, expected)


def test_string_array_extract():
# https://github.com/pandas-dev/pandas/issues/30969
# Only expand=False & multiple groups was failing
a = Series(["a1", "b2", "cc"], dtype="string")
b = Series(["a1", "b2", "cc"], dtype="object")
pat = r"(\w)(\d)"

result = a.str.extract(pat, expand=False)
expected = b.str.extract(pat, expand=False)
assert all(result.dtypes == "string")

result = result.astype(object)
tm.assert_equal(result, expected)