Skip to content

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

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
Feb 21, 2024
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
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 @@ -175,7 +175,7 @@ Performance improvements
- Performance improvement in :meth:`RangeIndex.append` when appending the same index (:issue:`57252`)
- Performance improvement in :meth:`RangeIndex.take` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57445`)
- 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
11 changes: 9 additions & 2 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) -> list[Hashable] | range:
"""
Get named groups from compiled regex.

Expand All @@ -3571,8 +3571,15 @@ def _get_group_names(regex: re.Pattern) -> list[Hashable]:
-------
list of column labels
"""
rng = range(regex.groups)
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 rng
result: list[Hashable] = [names.get(1 + i, i) for i in rng]
arr = np.array(result)
if arr.dtype.kind == "i" and lib.is_range_indexer(arr, len(arr)):
return rng
return result


def str_extractall(arr, pat, flags: int = 0) -> DataFrame:
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/strings/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,13 @@ def test_extractall_no_matches(data, names, any_string_dtype):

# one un-named group.
result = s.str.extractall("(z)")
expected = DataFrame(columns=[0], index=expected_index, dtype=any_string_dtype)
tm.assert_frame_equal(result, expected)
expected = DataFrame(columns=range(1), index=expected_index, dtype=any_string_dtype)
tm.assert_frame_equal(result, expected, check_column_type=True)

# two un-named groups.
result = s.str.extractall("(z)(z)")
expected = DataFrame(columns=[0, 1], index=expected_index, dtype=any_string_dtype)
tm.assert_frame_equal(result, expected)
expected = DataFrame(columns=range(2), index=expected_index, dtype=any_string_dtype)
tm.assert_frame_equal(result, expected, check_column_type=True)

# one named group.
result = s.str.extractall("(?P<first>z)")
Expand Down