Skip to content

Commit

Permalink
Backport PR pandas-dev#56647 on branch 2.2.x (floordiv fix for large …
Browse files Browse the repository at this point in the history
…values) (pandas-dev#56655)

Backport PR pandas-dev#56647: floordiv fix for large values

Co-authored-by: rohanjain101 <[email protected]>
  • Loading branch information
meeseeksmachine and rohanjain101 authored Dec 28, 2023
1 parent 3732cc4 commit f99f4d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ Timezones
Numeric
^^^^^^^
- Bug in :func:`read_csv` with ``engine="pyarrow"`` causing rounding errors for large integers (:issue:`52505`)
- Bug in :meth:`Series.__floordiv__` for :class:`ArrowDtype` with integral dtypes raising for large values (:issue:`56645`)
- Bug in :meth:`Series.pow` not filling missing values correctly (:issue:`55512`)

Conversion
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ def cast_for_truediv(
if pa.types.is_integer(arrow_array.type) and pa.types.is_integer(
pa_object.type
):
return arrow_array.cast(pa.float64())
# https://github.com/apache/arrow/issues/35563
# Arrow does not allow safe casting large integral values to float64.
# Intentionally not using arrow_array.cast because it could be a scalar
# value in reflected case, and safe=False only added to
# scalar cast in pyarrow 13.
return pc.cast(arrow_array, pa.float64(), safe=False)
return arrow_array

def floordiv_compat(
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3238,6 +3238,14 @@ def test_arrow_floordiv():
tm.assert_series_equal(result, expected)


def test_arrow_floordiv_large_values():
# GH 55561
a = pd.Series([1425801600000000000], dtype="int64[pyarrow]")
expected = pd.Series([1425801600000], dtype="int64[pyarrow]")
result = a // 1_000_000
tm.assert_series_equal(result, expected)


def test_string_to_datetime_parsing_cast():
# GH 56266
string_dates = ["2020-01-01 04:30:00", "2020-01-02 00:00:00", "2020-01-03 00:00:00"]
Expand Down

0 comments on commit f99f4d6

Please sign in to comment.