Skip to content

Commit

Permalink
Backport PR #28024: BUG: rfloordiv with fill_value, closes#27464 (#28040
Browse files Browse the repository at this point in the history
)
  • Loading branch information
meeseeksmachine authored and TomAugspurger committed Aug 20, 2019
1 parent af049e3 commit 23ce0b0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Expand Up @@ -55,7 +55,7 @@ Numeric
- Bug in :meth:`Series.interpolate` when using a timezone aware :class:`DatetimeIndex` (:issue:`27548`)
- Bug when printing negative floating point complex numbers would raise an ``IndexError`` (:issue:`27484`)
- 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`)
-
- Bug in :class:`DataFrame` arithmetic where missing values in results were incorrectly masked with ``NaN`` instead of ``Inf`` (:issue:`27464`)

Conversion
^^^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/frame.py
Expand Up @@ -111,6 +111,7 @@
sanitize_index,
to_arrays,
)
from pandas.core.ops.missing import dispatch_fill_zeros
from pandas.core.series import Series

from pandas.io.formats import console, format as fmt
Expand Down Expand Up @@ -5365,7 +5366,9 @@ def _arith_op(left, right):
# iterate over columns
return ops.dispatch_to_series(this, other, _arith_op)
else:
result = _arith_op(this.values, other.values)
with np.errstate(all="ignore"):
result = _arith_op(this.values, other.values)
result = dispatch_fill_zeros(func, this.values, other.values, result)
return self._constructor(
result, index=new_index, columns=new_columns, copy=False
)
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/arithmetic/test_numeric.py
Expand Up @@ -1227,3 +1227,36 @@ def test_addsub_arithmetic(self, dtype, delta):
tm.assert_index_equal(index + index, 2 * index)
tm.assert_index_equal(index - index, 0 * index)
assert not (index - index).empty


def test_fill_value_inf_masking():
# GH #27464 make sure we mask 0/1 with Inf and not NaN
df = pd.DataFrame({"A": [0, 1, 2], "B": [1.1, None, 1.1]})

other = pd.DataFrame({"A": [1.1, 1.2, 1.3]}, index=[0, 2, 3])

result = df.rfloordiv(other, fill_value=1)

expected = pd.DataFrame(
{"A": [np.inf, 1.0, 0.0, 1.0], "B": [0.0, np.nan, 0.0, np.nan]}
)
tm.assert_frame_equal(result, expected)


def test_dataframe_div_silenced():
# GH#26793
pdf1 = pd.DataFrame(
{
"A": np.arange(10),
"B": [np.nan, 1, 2, 3, 4] * 2,
"C": [np.nan] * 10,
"D": np.arange(10),
},
index=list("abcdefghij"),
columns=list("ABCD"),
)
pdf2 = pd.DataFrame(
np.random.randn(10, 4), index=list("abcdefghjk"), columns=list("ABCX")
)
with tm.assert_produces_warning(None):
pdf1.div(pdf2, fill_value=0)

0 comments on commit 23ce0b0

Please sign in to comment.