Skip to content

Commit 23ce0b0

Browse files
meeseeksmachineTomAugspurger
authored andcommitted
Backport PR #28024: BUG: rfloordiv with fill_value, closes#27464 (#28040)
1 parent af049e3 commit 23ce0b0

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

doc/source/whatsnew/v0.25.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Numeric
5555
- Bug in :meth:`Series.interpolate` when using a timezone aware :class:`DatetimeIndex` (:issue:`27548`)
5656
- Bug when printing negative floating point complex numbers would raise an ``IndexError`` (:issue:`27484`)
5757
- Bug where :class:`DataFrame` arithmetic operators such as :meth:`DataFrame.mul` with a :class:`Series` with axis=1 would raise an ``AttributeError`` on :class:`DataFrame` larger than the minimum threshold to invoke numexpr (:issue:`27636`)
58-
-
58+
- Bug in :class:`DataFrame` arithmetic where missing values in results were incorrectly masked with ``NaN`` instead of ``Inf`` (:issue:`27464`)
5959

6060
Conversion
6161
^^^^^^^^^^

pandas/core/frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
sanitize_index,
112112
to_arrays,
113113
)
114+
from pandas.core.ops.missing import dispatch_fill_zeros
114115
from pandas.core.series import Series
115116

116117
from pandas.io.formats import console, format as fmt
@@ -5365,7 +5366,9 @@ def _arith_op(left, right):
53655366
# iterate over columns
53665367
return ops.dispatch_to_series(this, other, _arith_op)
53675368
else:
5368-
result = _arith_op(this.values, other.values)
5369+
with np.errstate(all="ignore"):
5370+
result = _arith_op(this.values, other.values)
5371+
result = dispatch_fill_zeros(func, this.values, other.values, result)
53695372
return self._constructor(
53705373
result, index=new_index, columns=new_columns, copy=False
53715374
)

pandas/tests/arithmetic/test_numeric.py

+33
Original file line numberDiff line numberDiff line change
@@ -1227,3 +1227,36 @@ def test_addsub_arithmetic(self, dtype, delta):
12271227
tm.assert_index_equal(index + index, 2 * index)
12281228
tm.assert_index_equal(index - index, 0 * index)
12291229
assert not (index - index).empty
1230+
1231+
1232+
def test_fill_value_inf_masking():
1233+
# GH #27464 make sure we mask 0/1 with Inf and not NaN
1234+
df = pd.DataFrame({"A": [0, 1, 2], "B": [1.1, None, 1.1]})
1235+
1236+
other = pd.DataFrame({"A": [1.1, 1.2, 1.3]}, index=[0, 2, 3])
1237+
1238+
result = df.rfloordiv(other, fill_value=1)
1239+
1240+
expected = pd.DataFrame(
1241+
{"A": [np.inf, 1.0, 0.0, 1.0], "B": [0.0, np.nan, 0.0, np.nan]}
1242+
)
1243+
tm.assert_frame_equal(result, expected)
1244+
1245+
1246+
def test_dataframe_div_silenced():
1247+
# GH#26793
1248+
pdf1 = pd.DataFrame(
1249+
{
1250+
"A": np.arange(10),
1251+
"B": [np.nan, 1, 2, 3, 4] * 2,
1252+
"C": [np.nan] * 10,
1253+
"D": np.arange(10),
1254+
},
1255+
index=list("abcdefghij"),
1256+
columns=list("ABCD"),
1257+
)
1258+
pdf2 = pd.DataFrame(
1259+
np.random.randn(10, 4), index=list("abcdefghjk"), columns=list("ABCX")
1260+
)
1261+
with tm.assert_produces_warning(None):
1262+
pdf1.div(pdf2, fill_value=0)

0 commit comments

Comments
 (0)