Skip to content

Commit fada873

Browse files
authored
ENH: pyarrow dont raise on division by zero (#51541)
1 parent a1c6a22 commit fada873

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
@@ -888,6 +888,16 @@ def test_basic_equals(self, data):
888888
class TestBaseArithmeticOps(base.BaseArithmeticOpsTests):
889889
divmod_exc = NotImplementedError
890890

891+
def get_op_from_name(self, op_name):
892+
short_opname = op_name.strip("_")
893+
if short_opname == "rtruediv":
894+
# use the numpy version that won't raise on division by zero
895+
return lambda x, y: np.divide(y, x)
896+
elif short_opname == "rfloordiv":
897+
return lambda x, y: np.floor_divide(y, x)
898+
899+
return tm.get_op_from_name(op_name)
900+
891901
def _patch_combine(self, obj, other, op):
892902
# BaseOpsUtil._combine can upcast expected dtype
893903
# (because it generates expected on python scalars)
@@ -988,8 +998,8 @@ def _get_arith_xfail_marker(self, opname, pa_dtype):
988998
),
989999
)
9901000
elif (
991-
opname in {"__rtruediv__", "__rfloordiv__"}
992-
and (pa.types.is_floating(pa_dtype) or pa.types.is_integer(pa_dtype))
1001+
opname in {"__rfloordiv__"}
1002+
and pa.types.is_integer(pa_dtype)
9931003
and not pa_version_under7p0
9941004
):
9951005
mark = pytest.mark.xfail(
@@ -1105,7 +1115,7 @@ def test_arith_series_with_array(
11051115
pa.types.is_floating(pa_dtype)
11061116
or (
11071117
pa.types.is_integer(pa_dtype)
1108-
and all_arithmetic_operators != "__truediv__"
1118+
and all_arithmetic_operators not in ["__truediv__", "__rtruediv__"]
11091119
)
11101120
or pa.types.is_duration(pa_dtype)
11111121
or pa.types.is_timestamp(pa_dtype)

0 commit comments

Comments
 (0)