Skip to content

Commit

Permalink
BUG: handle nan values in DataFrame.update when overwrite=False (pand…
Browse files Browse the repository at this point in the history
…as-dev#15593)

add nan test for DataFrame.update

update whatsnew v0.20.2
  • Loading branch information
pcluo committed May 24, 2017
1 parent 49ec31b commit c832fdd
Show file tree
Hide file tree
Showing 3 changed files with 25 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
20 changes: 20 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,23 @@ 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
expected = df1 = DataFrame({'A': [1.0, 2, 3], 'B': date_range('2000', periods=3)})
df2 = DataFrame({'A': [None, 2, 3]})
df1.update(df2, overwrite=False)

tm.assert_frame_equal(df1, expected)

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

tm.assert_frame_equal(df1, expected)

0 comments on commit c832fdd

Please sign in to comment.