Skip to content

Commit 5245d29

Browse files
jbrockmendelgaluhsahid
authored andcommitted
BUG: rfloordiv with fill_value, closes#27464 (pandas-dev#28024)
* BUG: rfloordiv with fill_value, pandas-dev#27464, pandas-dev#26793
1 parent 21d59b2 commit 5245d29

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
@@ -54,7 +54,7 @@ Numeric
5454
- Bug in :meth:`Series.interpolate` when using a timezone aware :class:`DatetimeIndex` (:issue:`27548`)
5555
- Bug when printing negative floating point complex numbers would raise an ``IndexError`` (:issue:`27484`)
5656
- 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`)
57-
-
57+
- Bug in :class:`DataFrame` arithmetic where missing values in results were incorrectly masked with ``NaN`` instead of ``Inf`` (:issue:`27464`)
5858

5959
Conversion
6060
^^^^^^^^^^

pandas/core/frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
sanitize_index,
109109
to_arrays,
110110
)
111+
from pandas.core.ops.missing import dispatch_fill_zeros
111112
from pandas.core.series import Series
112113

113114
from pandas.io.formats import console, format as fmt
@@ -5305,7 +5306,9 @@ def _arith_op(left, right):
53055306
# iterate over columns
53065307
return ops.dispatch_to_series(this, other, _arith_op)
53075308
else:
5308-
result = _arith_op(this.values, other.values)
5309+
with np.errstate(all="ignore"):
5310+
result = _arith_op(this.values, other.values)
5311+
result = dispatch_fill_zeros(func, this.values, other.values, result)
53095312
return self._constructor(
53105313
result, index=new_index, columns=new_columns, copy=False
53115314
)

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)