Skip to content

BUG: Series.__mul__ for pyarrow strings #56368

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
Dec 7, 2023
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/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ Strings
^^^^^^^
- Bug in :func:`pandas.api.types.is_string_dtype` while checking object array with no elements is of the string dtype (:issue:`54661`)
- Bug in :meth:`DataFrame.apply` failing when ``engine="numba"`` and columns or index have ``StringDtype`` (:issue:`56189`)
- Bug in :meth:`Series.__mul__` for :class:`ArrowDtype` with ``pyarrow.string`` dtype and ``string[pyarrow]`` for the pyarrow backend (:issue:`51970`)
- Bug in :meth:`Series.str.startswith` and :meth:`Series.str.endswith` with arguments of type ``tuple[str, ...]`` for ``string[pyarrow]`` (:issue:`54942`)

Interval
Expand Down
26 changes: 16 additions & 10 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,16 +668,22 @@ def _evaluate_op_method(self, other, op, arrow_funcs):
pa_type = self._pa_array.type
other = self._box_pa(other)

if (pa.types.is_string(pa_type) or pa.types.is_binary(pa_type)) and op in [
operator.add,
roperator.radd,
]:
sep = pa.scalar("", type=pa_type)
if op is operator.add:
result = pc.binary_join_element_wise(self._pa_array, other, sep)
else:
result = pc.binary_join_element_wise(other, self._pa_array, sep)
return type(self)(result)
if pa.types.is_string(pa_type) or pa.types.is_binary(pa_type):
if op in [operator.add, roperator.radd, operator.mul, roperator.rmul]:
sep = pa.scalar("", type=pa_type)
if op is operator.add:
result = pc.binary_join_element_wise(self._pa_array, other, sep)
elif op is roperator.radd:
result = pc.binary_join_element_wise(other, self._pa_array, sep)
else:
if not (
isinstance(other, pa.Scalar) and pa.types.is_integer(other.type)
):
raise TypeError("Can only string multiply by an integer.")
result = pc.binary_join_element_wise(
*([self._pa_array] * other.as_py()), sep
)
return type(self)(result)

if (
isinstance(other, pa.Scalar)
Expand Down
7 changes: 1 addition & 6 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,7 @@ def test_add_sequence(dtype):
tm.assert_extension_array_equal(result, expected)


def test_mul(dtype, request, arrow_string_storage):
if dtype.storage in arrow_string_storage:
reason = "unsupported operand type(s) for *: 'ArrowStringArray' and 'int'"
mark = pytest.mark.xfail(raises=NotImplementedError, reason=reason)
request.applymarker(mark)

def test_mul(dtype):
a = pd.array(["a", "b", None], dtype=dtype)
result = a * 2
expected = pd.array(["aa", "bb", None], dtype=dtype)
Expand Down
26 changes: 25 additions & 1 deletion pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,16 @@ def _get_arith_xfail_marker(self, opname, pa_dtype):
def test_arith_series_with_scalar(self, data, all_arithmetic_operators, request):
pa_dtype = data.dtype.pyarrow_dtype

if all_arithmetic_operators == "__rmod__" and (pa.types.is_binary(pa_dtype)):
if all_arithmetic_operators == "__rmod__" and pa.types.is_binary(pa_dtype):
pytest.skip("Skip testing Python string formatting")
elif all_arithmetic_operators in ("__rmul__", "__mul__") and (
pa.types.is_binary(pa_dtype) or pa.types.is_string(pa_dtype)
):
request.applymarker(
pytest.mark.xfail(
raises=TypeError, reason="Can only string multiply by an integer."
)
)

mark = self._get_arith_xfail_marker(all_arithmetic_operators, pa_dtype)
if mark is not None:
Expand All @@ -981,6 +989,14 @@ def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request):
pa.types.is_string(pa_dtype) or pa.types.is_binary(pa_dtype)
):
pytest.skip("Skip testing Python string formatting")
elif all_arithmetic_operators in ("__rmul__", "__mul__") and (
pa.types.is_binary(pa_dtype) or pa.types.is_string(pa_dtype)
):
request.applymarker(
pytest.mark.xfail(
raises=TypeError, reason="Can only string multiply by an integer."
)
)

mark = self._get_arith_xfail_marker(all_arithmetic_operators, pa_dtype)
if mark is not None:
Expand All @@ -1004,6 +1020,14 @@ def test_arith_series_with_array(self, data, all_arithmetic_operators, request):
),
)
)
elif all_arithmetic_operators in ("__rmul__", "__mul__") and (
pa.types.is_binary(pa_dtype) or pa.types.is_string(pa_dtype)
):
request.applymarker(
pytest.mark.xfail(
raises=TypeError, reason="Can only string multiply by an integer."
)
)

mark = self._get_arith_xfail_marker(all_arithmetic_operators, pa_dtype)
if mark is not None:
Expand Down