Skip to content

Commit 0734df0

Browse files
facaiyjreback
authored andcommitted
BUG: Unexpected behavior with behavior with binary operators and fill_value
closes #12723 closes #12791
1 parent e0ee3a1 commit 0734df0

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

doc/source/whatsnew/v0.18.1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,5 @@ Bug Fixes
321321
- Bug where ``loffset`` argument was not applied when calling ``resample().count()`` on a timeseries (:issue:`12725`)
322322
- ``pd.read_excel()`` now accepts path objects (e.g. ``pathlib.Path``, ``py.path.local``) for the file path, in line with other ``read_*`` functions (:issue:`12655`)
323323
- ``pd.read_excel()`` now accepts column names associated with keyword argument ``names``(:issue `12870`)
324+
325+
- Bug in ``fill_value`` is ignored if the argument to a binary operator is a constant (:issue `12723`)

pandas/core/ops.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,9 @@ def flex_wrapper(self, other, level=None, fill_value=None, axis=0):
933933
return self._binop(self._constructor(other, self.index), op,
934934
level=level, fill_value=fill_value)
935935
else:
936+
if fill_value is not None:
937+
self = self.fillna(fill_value)
938+
936939
return self._constructor(op(self.values, other),
937940
self.index).__finalize__(self)
938941

@@ -1088,6 +1091,9 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
10881091
raise ValueError("Incompatible argument shape: %s" %
10891092
(other.shape, ))
10901093
else:
1094+
if fill_value is not None:
1095+
self = self.fillna(fill_value)
1096+
10911097
return self._combine_const(other, na_op)
10921098

10931099
f.__name__ = name

pandas/sparse/tests/test_frame.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ def setUp(self):
6060

6161
self.empty = SparseDataFrame()
6262

63+
def test_fill_value_when_combine_const(self):
64+
# GH12723
65+
dat = np.array([0, 1, np.nan, 3, 4, 5], dtype='float')
66+
df = SparseDataFrame({'foo': dat}, index=range(6))
67+
68+
exp = df.fillna(0).add(2)
69+
res = df.add(2, fill_value=0)
70+
tm.assert_sp_frame_equal(res, exp)
71+
6372
def test_as_matrix(self):
6473
empty = self.empty.as_matrix()
6574
self.assertEqual(empty.shape, (0, 0))

pandas/sparse/tests/test_series.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,14 @@ def test_fill_value_corner(self):
740740
result = cop2 / cop
741741
self.assertTrue(np.isnan(result.fill_value))
742742

743+
def test_fill_value_when_combine_const(self):
744+
# GH12723
745+
s = SparseSeries([0, 1, np.nan, 3, 4, 5], index=np.arange(6))
746+
747+
exp = s.fillna(0).add(2)
748+
res = s.add(2, fill_value=0)
749+
self.assert_series_equal(res, exp)
750+
743751
def test_shift(self):
744752
series = SparseSeries([nan, 1., 2., 3., nan, nan], index=np.arange(6))
745753

pandas/tests/frame/test_missing.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,19 @@ def test_fill_corner(self):
425425

426426
# TODO(wesm): unused?
427427
result = empty_float.fillna(value=0) # noqa
428+
429+
def test_fill_value_when_combine_const(self):
430+
# GH12723
431+
dat = np.array([0, 1, np.nan, 3, 4, 5], dtype='float')
432+
df = DataFrame({'foo': dat}, index=range(6))
433+
434+
exp = df.fillna(0).add(2)
435+
res = df.add(2, fill_value=0)
436+
assert_frame_equal(res, exp)
437+
438+
439+
if __name__ == '__main__':
440+
import nose
441+
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
442+
# '--with-coverage', '--cover-package=pandas.core']
443+
exit=False)

pandas/tests/series/test_missing.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,18 @@ def test_dropna_preserve_name(self):
452452
ts = self.ts.copy()
453453
ts.dropna(inplace=True)
454454
self.assertEqual(ts.name, name)
455+
456+
def test_fill_value_when_combine_const(self):
457+
# GH12723
458+
s = Series([0, 1, np.nan, 3, 4, 5])
459+
460+
exp = s.fillna(0).add(2)
461+
res = s.add(2, fill_value=0)
462+
assert_series_equal(res, exp)
463+
464+
465+
if __name__ == '__main__':
466+
import nose
467+
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
468+
# '--with-coverage', '--cover-package=pandas.core']
469+
exit=False)

0 commit comments

Comments
 (0)