Skip to content

Commit 63bf5d8

Browse files
author
Rohan Jain
committed
fix overflow
1 parent e56b1b3 commit 63bf5d8

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

pandas/core/arrays/arrow/array.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ def floordiv_compat(
127127
left: pa.ChunkedArray | pa.Array | pa.Scalar,
128128
right: pa.ChunkedArray | pa.Array | pa.Scalar,
129129
) -> pa.ChunkedArray:
130-
divided = pc.divide(left, right)
131-
if pa.types.is_integer(divided.type):
130+
if pa.types.is_integer(left.type) and pa.types.is_integer(right.type):
131+
# Use divide_checked to ensure cases like -9223372036854775808 // -1
132+
# don't silently overflow.
133+
divided = pc.divide_checked(left, right)
132134
# GH 56676: avoid storing intermediate calculating in floating point type.
133135
has_remainder = pc.not_equal(pc.multiply(divided, right), left)
134136
result = pc.if_else(
@@ -154,6 +156,10 @@ def floordiv_compat(
154156
# int8 // int64 returned int8 rather than int64.
155157
result = result.cast(left.type)
156158
else:
159+
# Use divide instead of divide_checked to match numpy
160+
# floordiv where divide by 0 returns infinity for floating
161+
# point types.
162+
divided = pc.divide(left, right)
157163
result = pc.floor(divided)
158164
return result
159165

pandas/tests/extension/test_arrow.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,6 +3261,23 @@ def test_arrow_floordiv_larger_divisor():
32613261
tm.assert_series_equal(result, expected)
32623262

32633263

3264+
def test_arrow_floordiv_integral_invalid():
3265+
# GH 56676
3266+
a = pd.Series([-9223372036854775808], dtype="int64[pyarrow]")
3267+
with pytest.raises(pa.lib.ArrowInvalid, match="overflow"):
3268+
a // -1
3269+
with pytest.raises(pa.lib.ArrowInvalid, match="divide by zero"):
3270+
a // 0
3271+
3272+
3273+
def test_arrow_floordiv_floating_0_divisor():
3274+
# GH 56676
3275+
a = pd.Series([2], dtype="double[pyarrow]")
3276+
result = a // 0
3277+
expected = pd.Series([float("inf")], dtype="double[pyarrow]")
3278+
tm.assert_series_equal(result, expected)
3279+
3280+
32643281
def test_arrow_floordiv_no_overflow():
32653282
# GH 56676
32663283
a = pd.Series([9223372036854775808], dtype="uint64[pyarrow]")

0 commit comments

Comments
 (0)