Skip to content

Commit 5e04098

Browse files
jbrockmendelmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#51541: ENH: pyarrow dont raise on division by zero
1 parent a3894fd commit 5e04098

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ Other API changes
814814
- :func:`to_datetime` with ``unit`` of either "Y" or "M" will now raise if a sequence contains a non-round ``float`` value, matching the ``Timestamp`` behavior (:issue:`50301`)
815815
- The methods :meth:`Series.round`, :meth:`DataFrame.__invert__`, :meth:`Series.__invert__`, :meth:`DataFrame.swapaxes`, :meth:`DataFrame.first`, :meth:`DataFrame.last`, :meth:`Series.first`, :meth:`Series.last` and :meth:`DataFrame.align` will now always return new objects (:issue:`51032`)
816816
- :class:`DataFrame` and :class:`DataFrameGroupBy` aggregations (e.g. "sum") with object-dtype columns no longer infer non-object dtypes for their results, explicitly call ``result.infer_objects(copy=False)`` on the result to obtain the old behavior (:issue:`51205`, :issue:`49603`)
817+
- Division by zero with :class:`ArrowDtype` dtypes returns ``-inf``, ``nan``, or ``inf`` depending on the numerator, instead of raising (:issue:`51541`)
817818
- Added :func:`pandas.api.types.is_any_real_numeric_dtype` to check for real numeric dtypes (:issue:`51152`)
818819

819820
.. note::

pandas/core/arrays/arrow/array.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def floordiv_compat(
106106
) -> pa.ChunkedArray:
107107
# Ensure int // int -> int mirroring Python/Numpy behavior
108108
# as pc.floor(pc.divide_checked(int, int)) -> float
109-
result = pc.floor(pc.divide_checked(left, right))
109+
result = pc.floor(pc.divide(left, right))
110110
if pa.types.is_integer(left.type) and pa.types.is_integer(right.type):
111111
result = result.cast(left.type)
112112
return result
@@ -118,8 +118,8 @@ def floordiv_compat(
118118
"rsub": lambda x, y: pc.subtract_checked(y, x),
119119
"mul": pc.multiply_checked,
120120
"rmul": lambda x, y: pc.multiply_checked(y, x),
121-
"truediv": lambda x, y: pc.divide_checked(cast_for_truediv(x, y), y),
122-
"rtruediv": lambda x, y: pc.divide_checked(y, cast_for_truediv(x, y)),
121+
"truediv": lambda x, y: pc.divide(cast_for_truediv(x, y), y),
122+
"rtruediv": lambda x, y: pc.divide(y, cast_for_truediv(x, y)),
123123
"floordiv": lambda x, y: floordiv_compat(x, y),
124124
"rfloordiv": lambda x, y: floordiv_compat(y, x),
125125
"mod": NotImplemented,

pandas/tests/extension/test_arrow.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,16 @@ def test_basic_equals(self, data):
906906
class TestBaseArithmeticOps(base.BaseArithmeticOpsTests):
907907
divmod_exc = NotImplementedError
908908

909+
def get_op_from_name(self, op_name):
910+
short_opname = op_name.strip("_")
911+
if short_opname == "rtruediv":
912+
# use the numpy version that won't raise on division by zero
913+
return lambda x, y: np.divide(y, x)
914+
elif short_opname == "rfloordiv":
915+
return lambda x, y: np.floor_divide(y, x)
916+
917+
return tm.get_op_from_name(op_name)
918+
909919
def _patch_combine(self, obj, other, op):
910920
# BaseOpsUtil._combine can upcast expected dtype
911921
# (because it generates expected on python scalars)
@@ -996,8 +1006,8 @@ def _get_arith_xfail_marker(self, opname, pa_dtype):
9961006
),
9971007
)
9981008
elif (
999-
opname in {"__rtruediv__", "__rfloordiv__"}
1000-
and (pa.types.is_floating(pa_dtype) or pa.types.is_integer(pa_dtype))
1009+
opname in {"__rfloordiv__"}
1010+
and pa.types.is_integer(pa_dtype)
10011011
and not pa_version_under7p0
10021012
):
10031013
mark = pytest.mark.xfail(
@@ -1111,7 +1121,7 @@ def test_arith_series_with_array(
11111121
pa.types.is_floating(pa_dtype)
11121122
or (
11131123
pa.types.is_integer(pa_dtype)
1114-
and all_arithmetic_operators != "__truediv__"
1124+
and all_arithmetic_operators not in ["__truediv__", "__rtruediv__"]
11151125
)
11161126
or pa.types.is_duration(pa_dtype)
11171127
or pa.types.is_timestamp(pa_dtype)

0 commit comments

Comments
 (0)