Skip to content

Commit be7a230

Browse files
committed
BUG: handle nan values in DataFrame.update when overwrite=False (pandas-dev#15593)
add nan test for DataFrame.update update whatsnew v0.20.2
1 parent 49ec31b commit be7a230

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
Conversion
4243
^^^^^^^^^^

pandas/core/frame.py

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

39223922
if overwrite:
39233923
mask = isnull(that)
3924-
3925-
# don't overwrite columns unecessarily
3926-
if mask.all():
3927-
continue
39283924
else:
39293925
mask = notnull(this)
39303926

3927+
# don't overwrite columns unecessarily
3928+
if mask.all():
3929+
continue
3930+
39313931
self[col] = expressions.where(mask, this, that,
39323932
raise_on_error=True)
39333933

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)