Skip to content

Fix integral truediv and floordiv for pyarrow types with large divisors #56707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

rohanjain101
Copy link
Contributor


def floordiv_compat(
left: pa.ChunkedArray | pa.Array | pa.Scalar,
right: pa.ChunkedArray | pa.Array | pa.Scalar,
) -> pa.ChunkedArray:
# Ensure int // int -> int mirroring Python/Numpy behavior
# as pc.floor(pc.divide_checked(int, int)) -> float
converted_left = cast_for_truediv(left, right)
result = pc.floor(pc.divide(converted_left, right))
converted_left, converted_right = cast_for_truediv(left, right)
Copy link
Contributor Author

@rohanjain101 rohanjain101 Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#56677 also fixes this for floordiv, IMO that is a more suitable fix for floordiv, since cpython and numpy implement floordiv without going through a double, but this fix is similar to #56647, I changed it for floordiv in this PR, only because the current floordiv implementation for integral types goes through truediv.

This fix should still apply to truediv, since both numpy and python truediv does an unsafe cast to a double:

>>> a = pd.Series([18014398509481983], dtype="int64")
>>> b = pd.Series([18014398509481984], dtype="int64")
>>> a / b
0    1.0
dtype: float64
>>>

@rohanjain101
Copy link
Contributor Author

Combined fix into #56677 to reduce confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Integral truediv and floordiv with pyarrow types overflows with large divisor
1 participant