Skip to content

BUG: Series.str.extract with StringArray returning object dtype #41441

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 3 commits into from
May 14, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ Strings

- Bug in the conversion from ``pyarrow.ChunkedArray`` to :class:`~arrays.StringArray` when the original had zero chunks (:issue:`41040`)
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` ignoring replacements with ``regex=True`` for ``StringDType`` data (:issue:`41333`, :issue:`35977`)
- Bug in :meth:`Series.str.extract` with :class:`~arrays.StringArray` returning object dtype for empty :class:`DataFrame` (:issue:`41441`)

Interval
^^^^^^^^
Expand Down
11 changes: 5 additions & 6 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3108,17 +3108,16 @@ def _str_extract_noexpand(arr, pat, flags=0):
# error: Incompatible types in assignment (expression has type
# "DataFrame", variable has type "ndarray")
result = DataFrame( # type: ignore[assignment]
columns=columns, dtype=object
columns=columns, dtype=result_dtype
)
else:
dtype = _result_dtype(arr)
# error: Incompatible types in assignment (expression has type
# "DataFrame", variable has type "ndarray")
result = DataFrame( # type:ignore[assignment]
[groups_or_na(val) for val in arr],
columns=columns,
index=arr.index,
dtype=dtype,
dtype=result_dtype,
)
return result, name

Expand All @@ -3135,19 +3134,19 @@ def _str_extract_frame(arr, pat, flags=0):
regex = re.compile(pat, flags=flags)
groups_or_na = _groups_or_na_fun(regex)
columns = _get_group_names(regex)
result_dtype = _result_dtype(arr)

if len(arr) == 0:
return DataFrame(columns=columns, dtype=object)
return DataFrame(columns=columns, dtype=result_dtype)
try:
result_index = arr.index
except AttributeError:
result_index = None
dtype = _result_dtype(arr)
return DataFrame(
[groups_or_na(val) for val in arr],
columns=columns,
index=result_index,
dtype=dtype,
dtype=result_dtype,
)


Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/strings/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,19 @@ def test_empty_str_methods(any_string_dtype):
tm.assert_series_equal(empty_str, empty.str.repeat(3))
tm.assert_series_equal(empty_bool, empty.str.match("^a"))
tm.assert_frame_equal(
DataFrame(columns=[0], dtype=str), empty.str.extract("()", expand=True)
DataFrame(columns=[0], dtype=any_string_dtype),
empty.str.extract("()", expand=True),
)
tm.assert_frame_equal(
DataFrame(columns=[0, 1], dtype=str), empty.str.extract("()()", expand=True)
DataFrame(columns=[0, 1], dtype=any_string_dtype),
empty.str.extract("()()", expand=True),
)
tm.assert_series_equal(empty_str, empty.str.extract("()", expand=False))
tm.assert_frame_equal(
DataFrame(columns=[0, 1], dtype=str),
DataFrame(columns=[0, 1], dtype=any_string_dtype),
empty.str.extract("()()", expand=False),
)
tm.assert_frame_equal(DataFrame(dtype=str), empty.str.get_dummies())
tm.assert_frame_equal(DataFrame(), empty.str.get_dummies())
tm.assert_series_equal(empty_str, empty_str.str.join(""))
tm.assert_series_equal(empty_int, empty.str.len())
tm.assert_series_equal(empty_object, empty_str.str.findall("a"))
Expand Down