Skip to content

Commit 23767fe

Browse files
committed
Fixed issue by adding a conversion clause to _cmp_method in string_.py
1 parent 17664e2 commit 23767fe

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

pandas/core/arrays/string_.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from pandas.core.dtypes.common import (
4646
is_array_like,
4747
is_bool_dtype,
48+
is_float_dtype,
4849
is_integer_dtype,
4950
is_object_dtype,
5051
is_string_dtype,
@@ -1110,10 +1111,24 @@ def _cmp_method(self, other, op):
11101111
if op.__name__ in ops.ARITHMETIC_BINOPS:
11111112
result = np.empty_like(self._ndarray, dtype="object")
11121113
result[mask] = self.dtype.na_value
1113-
result[valid] = op(self._ndarray[valid], other)
1114-
if isinstance(other, Path):
1115-
# GH#61940
1116-
return result
1114+
try:
1115+
result[valid] = op(self._ndarray[valid], other)
1116+
if isinstance(other, Path):
1117+
# GH#61940
1118+
return result
1119+
except TypeError:
1120+
if is_array_like(other):
1121+
if is_float_dtype(other.dtype):
1122+
# Shorten whole numbers to be ints to match pyarrow behavior
1123+
other = [
1124+
str(int(x)) if x.is_integer() else str(x) for x in other
1125+
]
1126+
else:
1127+
other = other.astype(str)
1128+
result[valid] = op(self._ndarray[valid], other)
1129+
else:
1130+
raise
1131+
11171132
return self._from_backing_data(result)
11181133
else:
11191134
# logical

pandas/tests/arrays/floating/test_arithmetic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def test_error_invalid_values(data, all_arithmetic_operators):
144144
"not implemented",
145145
"not supported for dtype",
146146
"Can only string multiply by an integer",
147+
"can't multiply sequence by non-int of type 'str'",
147148
]
148149
)
149150
with pytest.raises(TypeError, match=msg):

pandas/tests/arrays/string_/test_string.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,6 @@ def test_add_frame(dtype):
269269

270270
assert arr.__add__(df) is NotImplemented
271271

272-
# TODO
273-
# pyarrow returns a different dtype despite the values being the same
274-
# could be addressed this PR if needed
275272
result = arr + df
276273
expected = pd.DataFrame([["ax", np.nan, np.nan, np.nan]]).astype(dtype)
277274
tm.assert_frame_equal(result, expected, check_dtype=False)
@@ -281,6 +278,19 @@ def test_add_frame(dtype):
281278
tm.assert_frame_equal(result, expected, check_dtype=False)
282279

283280

281+
def test_add_frame_mixed_type(dtype):
282+
arr = pd.array(["a", "bc", 3, np.nan], dtype=dtype)
283+
df = pd.DataFrame([[1, 2, 3.3, 4]])
284+
285+
result = arr + df
286+
expected = pd.DataFrame([["a1", "bc2", "33.3", np.nan]]).astype(dtype)
287+
tm.assert_frame_equal(result, expected, check_dtype=False)
288+
289+
result = df + arr
290+
expected = pd.DataFrame([["1a", "2bc", "3.33", np.nan]]).astype(dtype)
291+
tm.assert_frame_equal(result, expected, check_dtype=False)
292+
293+
284294
def test_comparison_methods_scalar(comparison_op, dtype):
285295
op_name = f"__{comparison_op.__name__}__"
286296
a = pd.array(["a", None, "c"], dtype=dtype)

0 commit comments

Comments
 (0)