Skip to content

Commit

Permalink
Backport PR #53532 on branch 2.0.x (BUG: Series.str.split(expand=True…
Browse files Browse the repository at this point in the history
…) for ArrowDtype(pa.string())) (#53549)

* Backport PR #53532: BUG: Series.str.split(expand=True) for ArrowDtype(pa.string())

* _pa_array -> _data
  • Loading branch information
lukemanley authored Jun 7, 2023
1 parent c1247a7 commit 70ac1c8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.0.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Fixed regressions
Bug fixes
~~~~~~~~~
- Bug in :func:`read_csv` when defining ``dtype`` with ``bool[pyarrow]`` for the ``"c"`` and ``"python"`` engines (:issue:`53390`)
- Bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` with ``expand=True`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`53532`)
-

.. ---------------------------------------------------------------------------
.. _whatsnew_203.other:
Expand Down
36 changes: 31 additions & 5 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,40 @@ def _wrap_result(
if isinstance(result.dtype, ArrowDtype):
import pyarrow as pa

from pandas.compat import pa_version_under11p0

from pandas.core.arrays.arrow.array import ArrowExtensionArray

max_len = pa.compute.max(
result._data.combine_chunks().value_lengths()
).as_py()
if result.isna().any():
value_lengths = result._data.combine_chunks().value_lengths()
max_len = pa.compute.max(value_lengths).as_py()
min_len = pa.compute.min(value_lengths).as_py()
if result._hasna:
# ArrowExtensionArray.fillna doesn't work for list scalars
result._data = result._data.fill_null([None] * max_len)
result = ArrowExtensionArray(
result._data.fill_null([None] * max_len)
)
if min_len < max_len:
# append nulls to each scalar list element up to max_len
if not pa_version_under11p0:
result = ArrowExtensionArray(
pa.compute.list_slice(
result._data,
start=0,
stop=max_len,
return_fixed_size_list=True,
)
)
else:
all_null = np.full(max_len, fill_value=None, dtype=object)
values = result.to_numpy()
new_values = []
for row in values:
if len(row) < max_len:
nulls = all_null[: max_len - len(row)]
row = np.append(row, nulls)
new_values.append(row)
pa_type = result._data.type
result = ArrowExtensionArray(pa.array(new_values, type=pa_type))
if name is not None:
labels = name
else:
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2315,6 +2315,15 @@ def test_str_split():
)
tm.assert_frame_equal(result, expected)

result = ser.str.split("1", expand=True)
expected = pd.DataFrame(
{
0: ArrowExtensionArray(pa.array(["a", "a2cbcb", None])),
1: ArrowExtensionArray(pa.array(["cbcb", None, None])),
}
)
tm.assert_frame_equal(result, expected)


def test_str_rsplit():
# GH 52401
Expand All @@ -2340,6 +2349,15 @@ def test_str_rsplit():
)
tm.assert_frame_equal(result, expected)

result = ser.str.rsplit("1", expand=True)
expected = pd.DataFrame(
{
0: ArrowExtensionArray(pa.array(["a", "a2cbcb", None])),
1: ArrowExtensionArray(pa.array(["cbcb", None, None])),
}
)
tm.assert_frame_equal(result, expected)


def test_str_unsupported_extract():
ser = pd.Series(["abc", None], dtype=ArrowDtype(pa.string()))
Expand Down

0 comments on commit 70ac1c8

Please sign in to comment.