From ee999f76f515bf2ad8c77a9b3f2c7d9c9a0493cc Mon Sep 17 00:00:00 2001 From: cluo Date: Mon, 22 May 2017 15:35:40 -0400 Subject: [PATCH] BUG: handle nan values in DataFrame.update when overwrite=False (#15593) BUG: handle nan values in DataFrame.update when overwrite=False (#15593) add nan test for DataFrame.update update whatsnew v0.20.2 --- doc/source/whatsnew/v0.20.2.txt | 1 + pandas/core/frame.py | 8 ++++---- pandas/tests/frame/test_combine_concat.py | 10 ++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.20.2.txt b/doc/source/whatsnew/v0.20.2.txt index e0857019d2fd4c..21d3643641f1b4 100644 --- a/doc/source/whatsnew/v0.20.2.txt +++ b/doc/source/whatsnew/v0.20.2.txt @@ -37,6 +37,7 @@ Bug Fixes ~~~~~~~~~ - Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`) +- Bug in using ``DataFrame.update`` when overwrite=False and the second value is nan Conversion ^^^^^^^^^^ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 78a369761afc11..beea0e9d3d8adc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3921,13 +3921,13 @@ def update(self, other, join='left', overwrite=True, filter_func=None, if overwrite: mask = isnull(that) - - # don't overwrite columns unecessarily - if mask.all(): - continue else: mask = notnull(this) + # don't overwrite columns unecessarily + if mask.all(): + continue + self[col] = expressions.where(mask, this, that, raise_on_error=True) diff --git a/pandas/tests/frame/test_combine_concat.py b/pandas/tests/frame/test_combine_concat.py index 688cacdee263ee..e3b153b9b2c0fb 100644 --- a/pandas/tests/frame/test_combine_concat.py +++ b/pandas/tests/frame/test_combine_concat.py @@ -763,3 +763,13 @@ def test_concat_datetime_datetime64_frame(self): # it works! pd.concat([df1, df2_obj]) + + +class TestDataFrameUpdate(TestData): + + def test_update_nan(self): + # #15593 #15617 + df1 = DataFrame({'A': [1, None, 3], 'B': date_range('2000', periods=3)}) + df2 = DataFrame({'A': [None, 2, 3]}) + df1.update(df2, overwrite=False) + # it works!