Skip to content

Commit f99f4d6

Browse files
Backport PR #56647 on branch 2.2.x (floordiv fix for large values) (#56655)
Backport PR #56647: floordiv fix for large values Co-authored-by: rohanjain101 <[email protected]>
1 parent 3732cc4 commit f99f4d6

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ Timezones
728728
Numeric
729729
^^^^^^^
730730
- Bug in :func:`read_csv` with ``engine="pyarrow"`` causing rounding errors for large integers (:issue:`52505`)
731+
- Bug in :meth:`Series.__floordiv__` for :class:`ArrowDtype` with integral dtypes raising for large values (:issue:`56645`)
731732
- Bug in :meth:`Series.pow` not filling missing values correctly (:issue:`55512`)
732733

733734
Conversion

pandas/core/arrays/arrow/array.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ def cast_for_truediv(
115115
if pa.types.is_integer(arrow_array.type) and pa.types.is_integer(
116116
pa_object.type
117117
):
118-
return arrow_array.cast(pa.float64())
118+
# https://github.com/apache/arrow/issues/35563
119+
# Arrow does not allow safe casting large integral values to float64.
120+
# Intentionally not using arrow_array.cast because it could be a scalar
121+
# value in reflected case, and safe=False only added to
122+
# scalar cast in pyarrow 13.
123+
return pc.cast(arrow_array, pa.float64(), safe=False)
119124
return arrow_array
120125

121126
def floordiv_compat(

pandas/tests/extension/test_arrow.py

+8
Original file line numberDiff line numberDiff line change
@@ -3238,6 +3238,14 @@ def test_arrow_floordiv():
32383238
tm.assert_series_equal(result, expected)
32393239

32403240

3241+
def test_arrow_floordiv_large_values():
3242+
# GH 55561
3243+
a = pd.Series([1425801600000000000], dtype="int64[pyarrow]")
3244+
expected = pd.Series([1425801600000], dtype="int64[pyarrow]")
3245+
result = a // 1_000_000
3246+
tm.assert_series_equal(result, expected)
3247+
3248+
32413249
def test_string_to_datetime_parsing_cast():
32423250
# GH 56266
32433251
string_dates = ["2020-01-01 04:30:00", "2020-01-02 00:00:00", "2020-01-03 00:00:00"]

0 commit comments

Comments
 (0)