Skip to content

[ArrowStringArray] REF: str.extract - move code from function to accessor method #41541

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
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
79 changes: 39 additions & 40 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,11 @@ def extract(
2 NaN
dtype: object
"""
from pandas import (
DataFrame,
array as pd_array,
)

if not isinstance(expand, bool):
raise ValueError("expand must be True or False")

Expand All @@ -2389,7 +2394,40 @@ def extract(
raise ValueError("only one regex group is supported with Index")

# TODO: dispatch
return str_extract(self, pat, flags, expand=expand)

obj = self._data
result_dtype = _result_dtype(obj)

returns_df = regex.groups > 1 or expand

if returns_df:
name = None
columns = _get_group_names(regex)

if obj.array.size == 0:
result = DataFrame(columns=columns, dtype=result_dtype)

else:
result_list = _str_extract(
obj.array, pat, flags=flags, expand=returns_df
)

result_index: Index | None
if isinstance(obj, ABCSeries):
result_index = obj.index
else:
result_index = None

result = DataFrame(
result_list, columns=columns, index=result_index, dtype=result_dtype
)

else:
name = _get_single_group_name(regex)
result_arr = _str_extract(obj.array, pat, flags=flags, expand=returns_df)
# not dispatching, so we have to reconstruct here.
result = pd_array(result_arr, dtype=result_dtype)
return self._wrap_result(result, name=name)

@forbid_nonstring_types(["bytes"])
def extractall(self, pat, flags=0):
Expand Down Expand Up @@ -3103,45 +3141,6 @@ def f(x):
return np.array([f(val)[0] for val in np.asarray(arr)], dtype=object)


def str_extract(accessor: StringMethods, pat: str, flags: int = 0, expand: bool = True):
from pandas import (
DataFrame,
array as pd_array,
)

obj = accessor._data
result_dtype = _result_dtype(obj)
regex = re.compile(pat, flags=flags)
returns_df = regex.groups > 1 or expand

if returns_df:
name = None
columns = _get_group_names(regex)

if obj.array.size == 0:
result = DataFrame(columns=columns, dtype=result_dtype)

else:
result_list = _str_extract(obj.array, pat, flags=flags, expand=returns_df)

result_index: Index | None
if isinstance(obj, ABCSeries):
result_index = obj.index
else:
result_index = None

result = DataFrame(
result_list, columns=columns, index=result_index, dtype=result_dtype
)

else:
name = _get_single_group_name(regex)
result_arr = _str_extract(obj.array, pat, flags=flags, expand=returns_df)
# not dispatching, so we have to reconstruct here.
result = pd_array(result_arr, dtype=result_dtype)
return accessor._wrap_result(result, name=name)


def str_extractall(arr, pat, flags=0):
regex = re.compile(pat, flags=flags)
# the regex must contain capture groups.
Expand Down