Skip to content

Commit

Permalink
BUG: handle nan values in DataFrame.update when overwrite=False (#15593
Browse files Browse the repository at this point in the history
…) (#16430)
  • Loading branch information
pcluo authored and jreback committed May 24, 2017
1 parent 58775f7 commit 85080aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Bug Fixes
~~~~~~~~~

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

Conversion
^^^^^^^^^^
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/frame/test_combine_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,25 @@ def test_concat_datetime_datetime64_frame(self):

# it works!
pd.concat([df1, df2_obj])


class TestDataFrameUpdate(TestData):

def test_update_nan(self):
# #15593 #15617
# test 1
df1 = DataFrame({'A': [1.0, 2, 3], 'B': date_range('2000', periods=3)})
df2 = DataFrame({'A': [None, 2, 3]})
expected = df1.copy()
df1.update(df2, overwrite=False)

tm.assert_frame_equal(df1, expected)

# test 2
df1 = DataFrame({'A': [1.0, None, 3], 'B': date_range('2000', periods=3)})
df2 = DataFrame({'A': [None, 2, 3]})
expected = DataFrame({'A': [1.0, 2, 3], 'B': date_range('2000', periods=3)})
df1.update(df2, overwrite=False)

tm.assert_frame_equal(df1, expected)

0 comments on commit 85080aa

Please sign in to comment.