File tree 2 files changed +25
-2
lines changed 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -127,8 +127,10 @@ def floordiv_compat(
127
127
left : pa .ChunkedArray | pa .Array | pa .Scalar ,
128
128
right : pa .ChunkedArray | pa .Array | pa .Scalar ,
129
129
) -> 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 )
132
134
# GH 56676: avoid storing intermediate calculating in floating point type.
133
135
has_remainder = pc .not_equal (pc .multiply (divided , right ), left )
134
136
result = pc .if_else (
@@ -154,6 +156,10 @@ def floordiv_compat(
154
156
# int8 // int64 returned int8 rather than int64.
155
157
result = result .cast (left .type )
156
158
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 )
157
163
result = pc .floor (divided )
158
164
return result
159
165
Original file line number Diff line number Diff line change @@ -3261,6 +3261,23 @@ def test_arrow_floordiv_larger_divisor():
3261
3261
tm .assert_series_equal (result , expected )
3262
3262
3263
3263
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
+
3264
3281
def test_arrow_floordiv_no_overflow ():
3265
3282
# GH 56676
3266
3283
a = pd .Series ([9223372036854775808 ], dtype = "uint64[pyarrow]" )
You can’t perform that action at this time.
0 commit comments