Skip to content

[ArrowStringArray] PERF: str.extract object fallback #41490

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 4 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 35 additions & 0 deletions asv_bench/benchmarks/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ def time_rsplit(self, dtype, expand):
self.s.str.rsplit("--", expand=expand)


class Extract:

params = (["str", "string", "arrow_string"], [True, False])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prob should make a base class to have these params

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and include setup there

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea.

param_names = ["dtype", "expand"]

def setup(self, dtype, expand):
from pandas.core.arrays.string_arrow import ArrowStringDtype # noqa: F401

try:
self.s = Series(tm.makeStringIndex(10 ** 5), dtype=dtype)
except ImportError:
raise NotImplementedError

def time_extract_single_group(self, dtype, expand):
with warnings.catch_warnings(record=True):
self.s.str.extract("(\\w*)A", expand=expand)


class Dummies:
params = ["str", "string", "arrow_string"]
param_names = ["dtype"]
Expand Down Expand Up @@ -279,3 +297,20 @@ def setup(self):
def time_vector_slice(self):
# GH 2602
self.s.str[:5]


class Iter:
params = ["str", "string", "arrow_string"]
param_names = ["dtype"]

def setup(self, dtype):
from pandas.core.arrays.string_arrow import ArrowStringDtype # noqa: F401

try:
self.s = Series(["abcdefg", np.nan] * 500000, dtype=dtype)
except ImportError:
raise NotImplementedError

def time_iter(self, dtype):
for i in self.s:
pass
8 changes: 5 additions & 3 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3102,7 +3102,9 @@ def _str_extract_noexpand(arr, pat, flags=0):
result_dtype = _result_dtype(arr)

if regex.groups == 1:
result = np.array([groups_or_na(val)[0] for val in arr], dtype=object)
result = np.array(
[groups_or_na(val)[0] for val in np.asarray(arr)], dtype=object
)
name = _get_single_group_name(regex)
# not dispatching, so we have to reconstruct here.
result = pd_array(result, dtype=result_dtype)
Expand All @@ -3119,7 +3121,7 @@ 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]
[groups_or_na(val) for val in arr],
[groups_or_na(val) for val in np.asarray(arr)],
columns=columns,
index=arr.index,
dtype=result_dtype,
Expand Down Expand Up @@ -3148,7 +3150,7 @@ def _str_extract_frame(arr, pat, flags=0):
except AttributeError:
result_index = None
return DataFrame(
[groups_or_na(val) for val in arr],
[groups_or_na(val) for val in np.asarray(arr)],
columns=columns,
index=result_index,
dtype=result_dtype,
Expand Down
24 changes: 11 additions & 13 deletions pandas/tests/strings/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,23 @@ def test_extract_expand_kwarg(any_string_dtype):


def test_extract_expand_False_mixed_object():
mixed = Series(
[
"aBAD_BAD",
np.nan,
"BAD_b_BAD",
True,
datetime.today(),
"foo",
None,
1,
2.0,
]
ser = Series(
["aBAD_BAD", np.nan, "BAD_b_BAD", True, datetime.today(), "foo", None, 1, 2.0]
)

result = Series(mixed).str.extract(".*(BAD[_]+).*(BAD)", expand=False)
# two groups
result = ser.str.extract(".*(BAD[_]+).*(BAD)", expand=False)
er = [np.nan, np.nan] # empty row
expected = DataFrame([["BAD_", "BAD"], er, ["BAD_", "BAD"], er, er, er, er, er, er])
tm.assert_frame_equal(result, expected)

# single group
result = ser.str.extract(".*(BAD[_]+).*BAD", expand=False)
expected = Series(
["BAD_", np.nan, "BAD_", np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]
)
tm.assert_series_equal(result, expected)


def test_extract_expand_index_raises():
# GH9980
Expand Down