Skip to content

PERF: Return RangeIndex columns instead of Index for str.partition with ArrowDtype #57768

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 2 commits into from
Mar 11, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- :meth:`RangeIndex.append` returns a :class:`RangeIndex` instead of a :class:`Index` when appending values that could continue the :class:`RangeIndex` (:issue:`57467`)
- :meth:`Series.str.extract` returns a :class:`RangeIndex` columns instead of an :class:`Index` column when possible (:issue:`57542`)
- :meth:`Series.str.partition` with :class:`ArrowDtype` returns a :class:`RangeIndex` columns instead of an :class:`Index` column when possible (:issue:`57768`)
- Performance improvement in :class:`DataFrame` when ``data`` is a ``dict`` and ``columns`` is specified (:issue:`24368`)
- Performance improvement in :meth:`DataFrame.join` for sorted but non-unique indexes (:issue:`56941`)
- Performance improvement in :meth:`DataFrame.join` when left and/or right are non-unique and ``how`` is ``"left"``, ``"right"``, or ``"inner"`` (:issue:`56817`)
Expand Down
8 changes: 3 additions & 5 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,16 @@ def _wrap_result(
new_values.append(row)
pa_type = result._pa_array.type
result = ArrowExtensionArray(pa.array(new_values, type=pa_type))
if name is not None:
labels = name
else:
labels = range(max_len)
if name is None:
name = range(max_len)
result = (
pa.compute.list_flatten(result._pa_array)
.to_numpy()
.reshape(len(result), max_len)
)
result = {
label: ArrowExtensionArray(pa.array(res))
for label, res in zip(labels, result.T)
for label, res in zip(name, result.T)
}
elif is_object_dtype(result):

Expand Down
12 changes: 8 additions & 4 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2267,19 +2267,23 @@ def test_str_partition():
ser = pd.Series(["abcba", None], dtype=ArrowDtype(pa.string()))
result = ser.str.partition("b")
expected = pd.DataFrame(
[["a", "b", "cba"], [None, None, None]], dtype=ArrowDtype(pa.string())
[["a", "b", "cba"], [None, None, None]],
dtype=ArrowDtype(pa.string()),
columns=pd.RangeIndex(3),
)
tm.assert_frame_equal(result, expected)
tm.assert_frame_equal(result, expected, check_column_type=True)

result = ser.str.partition("b", expand=False)
expected = pd.Series(ArrowExtensionArray(pa.array([["a", "b", "cba"], None])))
tm.assert_series_equal(result, expected)

result = ser.str.rpartition("b")
expected = pd.DataFrame(
[["abc", "b", "a"], [None, None, None]], dtype=ArrowDtype(pa.string())
[["abc", "b", "a"], [None, None, None]],
dtype=ArrowDtype(pa.string()),
columns=pd.RangeIndex(3),
)
tm.assert_frame_equal(result, expected)
tm.assert_frame_equal(result, expected, check_column_type=True)

result = ser.str.rpartition("b", expand=False)
expected = pd.Series(ArrowExtensionArray(pa.array([["abc", "b", "a"], None])))
Expand Down