Skip to content

Commit c77c3f7

Browse files
author
Rohan Jain
committed
improve unsigned // unsigned
1 parent 263b8a2 commit c77c3f7

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

pandas/core/arrays/arrow/array.py

+28-22
Original file line numberDiff line numberDiff line change
@@ -138,29 +138,35 @@ def floordiv_compat(
138138
# Use divide_checked to ensure cases like -9223372036854775808 // -1
139139
# don't silently overflow.
140140
divided = pc.divide_checked(left, right)
141-
# GH 56676: avoid storing intermediate calculating in floating point type.
142-
has_remainder = pc.not_equal(pc.multiply(divided, right), left)
143-
result = pc.if_else(
144-
# Pass a typed arrow scalar rather than stdlib int
145-
# which always inferred as int64, to prevent overflow
146-
# in case of large uint64 values.
147-
pc.and_(
148-
pc.less(
149-
pc.bit_wise_xor(left, right), pa.scalar(0, type=divided.type)
141+
# If one operand is a signed integer, we need to ensure division
142+
# rounds towards negative infinity instead of 0. If divided.type
143+
# is a signed integer, that means at least one operand is signed
144+
# otherwise divided.type would be unsigned integer.
145+
if pa.types.is_signed_integer(divided.type):
146+
# GH 56676: avoid storing intermediate calculating in
147+
# floating point type.
148+
has_remainder = pc.not_equal(pc.multiply(divided, right), left)
149+
result = pc.if_else(
150+
pc.and_(
151+
pc.less(
152+
pc.bit_wise_xor(left, right),
153+
pa.scalar(0, type=divided.type),
154+
),
155+
has_remainder,
150156
),
151-
has_remainder,
152-
),
153-
# GH 55561: floordiv should round towards negative infinity.
154-
# pc.divide_checked for integral types rounds towards 0.
155-
# Avoid using subtract_checked which would incorrectly raise
156-
# for -9223372036854775808 // 1, because if integer overflow
157-
# occurs, then has_remainder should be false, and overflowed
158-
# value is discarded.
159-
pc.subtract(divided, pa.scalar(1, type=divided.type)),
160-
divided,
161-
)
162-
# Ensure compatibility with older versions of pandas where
163-
# int8 // int64 returned int8 rather than int64.
157+
# GH 55561: floordiv should round towards negative infinity.
158+
# pc.divide_checked for integral types rounds towards 0.
159+
# Avoid using subtract_checked which would incorrectly raise
160+
# for -9223372036854775808 // 1, because if integer overflow
161+
# occurs, then has_remainder should be false, and overflowed
162+
# value is discarded.
163+
pc.subtract(divided, pa.scalar(1, type=divided.type)),
164+
divided,
165+
)
166+
else:
167+
# For 2 unsigned integer operands, integer division is
168+
# same as floor division.
169+
result = divided
164170
result = result.cast(left.type)
165171
else:
166172
# Use divide instead of divide_checked to match numpy

0 commit comments

Comments
 (0)