Skip to content

Commit 64655e7

Browse files
pcluoTomAugspurger
authored andcommitted
BUG: handle nan values in DataFrame.update when overwrite=False (#15593) (#16430)
(cherry picked from commit 85080aa)
1 parent bc7bbf8 commit 64655e7

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Bug Fixes
3737
~~~~~~~~~
3838

3939
- Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`)
40+
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
4041

4142

4243
- Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on Categoricals (:issue:`16409`)

pandas/core/frame.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3852,13 +3852,13 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
38523852

38533853
if overwrite:
38543854
mask = isnull(that)
3855-
3856-
# don't overwrite columns unecessarily
3857-
if mask.all():
3858-
continue
38593855
else:
38603856
mask = notnull(this)
38613857

3858+
# don't overwrite columns unecessarily
3859+
if mask.all():
3860+
continue
3861+
38623862
self[col] = expressions.where(mask, this, that,
38633863
raise_on_error=True)
38643864

pandas/tests/frame/test_combine_concat.py

+22
Original file line numberDiff line numberDiff line change
@@ -763,3 +763,25 @@ def test_concat_datetime_datetime64_frame(self):
763763

764764
# it works!
765765
pd.concat([df1, df2_obj])
766+
767+
768+
class TestDataFrameUpdate(TestData):
769+
770+
def test_update_nan(self):
771+
# #15593 #15617
772+
# test 1
773+
df1 = DataFrame({'A': [1.0, 2, 3], 'B': date_range('2000', periods=3)})
774+
df2 = DataFrame({'A': [None, 2, 3]})
775+
expected = df1.copy()
776+
df1.update(df2, overwrite=False)
777+
778+
tm.assert_frame_equal(df1, expected)
779+
780+
# test 2
781+
df1 = DataFrame({'A': [1.0, None, 3], 'B': date_range('2000', periods=3)})
782+
df2 = DataFrame({'A': [None, 2, 3]})
783+
expected = DataFrame({'A': [1.0, 2, 3], 'B': date_range('2000', periods=3)})
784+
df1.update(df2, overwrite=False)
785+
786+
tm.assert_frame_equal(df1, expected)
787+

0 commit comments

Comments
 (0)