Skip to content

Commit dbad693

Browse files
committed
Fixed issue by adding a conversion clause to _cmp_method in string_.py
1 parent 6b20039 commit dbad693

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

pandas/core/arrays/string_.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from pandas.core.dtypes.common import (
4545
is_array_like,
4646
is_bool_dtype,
47+
is_float_dtype,
4748
is_integer_dtype,
4849
is_object_dtype,
4950
is_string_dtype,
@@ -1080,7 +1081,21 @@ def _cmp_method(self, other, op):
10801081
if op.__name__ in ops.ARITHMETIC_BINOPS:
10811082
result = np.empty_like(self._ndarray, dtype="object")
10821083
result[mask] = self.dtype.na_value
1083-
result[valid] = op(self._ndarray[valid], other)
1084+
try:
1085+
result[valid] = op(self._ndarray[valid], other)
1086+
except TypeError:
1087+
if is_array_like(other):
1088+
if is_float_dtype(other.dtype):
1089+
# Shorten whole numbers to be ints to match pyarrow behavior
1090+
other = [
1091+
str(int(x)) if x.is_integer() else str(x) for x in other
1092+
]
1093+
else:
1094+
other = other.astype(str)
1095+
result[valid] = op(self._ndarray[valid], other)
1096+
else:
1097+
raise
1098+
10841099
return self._from_backing_data(result)
10851100
else:
10861101
# 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: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,6 @@ def test_add_frame(dtype):
273273

274274
assert arr.__add__(df) is NotImplemented
275275

276-
# TODO
277-
# pyarrow returns a different dtype despite the values being the same
278-
# could be addressed this PR if needed
279276
result = arr + df
280277
expected = pd.DataFrame([["ax", np.nan, np.nan, np.nan]]).astype(dtype)
281278
tm.assert_frame_equal(result, expected, check_dtype=False)
@@ -285,16 +282,16 @@ def test_add_frame(dtype):
285282
tm.assert_frame_equal(result, expected, check_dtype=False)
286283

287284

288-
def test_add_frame_int(dtype):
289-
arr = pd.array(["a", "b", np.nan, np.nan], dtype=dtype)
290-
df = pd.DataFrame([[1, np.nan, 3, np.nan]])
285+
def test_add_frame_mixed_type(dtype):
286+
arr = pd.array(["a", "bc", 3, np.nan], dtype=dtype)
287+
df = pd.DataFrame([[1, 2, 3.3, 4]])
291288

292289
result = arr + df
293-
expected = pd.DataFrame([["a1", np.nan, np.nan, np.nan]]).astype(dtype)
290+
expected = pd.DataFrame([["a1", "bc2", "33.3", np.nan]]).astype(dtype)
294291
tm.assert_frame_equal(result, expected, check_dtype=False)
295292

296293
result = df + arr
297-
expected = pd.DataFrame([["1a", np.nan, np.nan, np.nan]]).astype(dtype)
294+
expected = pd.DataFrame([["1a", "2bc", "3.33", np.nan]]).astype(dtype)
298295
tm.assert_frame_equal(result, expected, check_dtype=False)
299296

300297

0 commit comments

Comments
 (0)