Skip to content

PERF: Return RangeIndex columns in str.extract when possible #57465

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Performance improvements
- Performance improvement in :meth:`MultiIndex.equals` for equal length indexes (:issue:`56990`)
- Performance improvement in :meth:`RangeIndex.append` when appending the same index (:issue:`57252`)
- Performance improvement in indexing operations for string dtypes (:issue:`56997`)
-
- :meth:`Series.str.extract` returns a :class:`RangeIndex` columns instead of an :class:`Index` column when possible (:issue:`?``)

.. ---------------------------------------------------------------------------
.. _whatsnew_300.bug_fixes:
Expand Down
20 changes: 17 additions & 3 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3557,7 +3557,7 @@ def _get_single_group_name(regex: re.Pattern) -> Hashable:
return None


def _get_group_names(regex: re.Pattern) -> list[Hashable]:
def _get_group_names(regex: re.Pattern) -> Index:
"""
Get named groups from compiled regex.

Expand All @@ -3569,10 +3569,24 @@ def _get_group_names(regex: re.Pattern) -> list[Hashable]:

Returns
-------
list of column labels
Index
"""
from pandas import Index

names = {v: k for k, v in regex.groupindex.items()}
return [names.get(1 + i, i) for i in range(regex.groups)]
if not names:
return Index(range(1))
return_rangeindex = True
result = []
for i in range(regex.groups):
name = names.get(1 + i, i)
if return_rangeindex and name != i:
return_rangeindex = False
result.append(name)
if return_rangeindex:
return Index(range(regex.groups))
else:
return Index(result)


def str_extractall(arr, pat, flags: int = 0) -> DataFrame:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/strings/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def test_extract_dataframe_capture_groups_index(index, any_string_dtype):

result = s.str.extract(r"(\d)", expand=True)
expected = DataFrame(["1", "2", np.nan], index=index, dtype=any_string_dtype)
tm.assert_frame_equal(result, expected)
tm.assert_frame_equal(result, expected, check_column_type=True)

result = s.str.extract(r"(?P<letter>\D)(?P<number>\d)?", expand=True)
expected = DataFrame(
Expand Down