Skip to content

BUG: Bug in DataFrame update where non-specified values could cause dtype changes #3021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pandas 0.11.0
- Bug in ``icol`` with negative indicies was incorrect producing incorrect return values (see GH2922_)
- Bug in DataFrame column insertion when the column creation fails, existing frame is left in
an irrecoverable state (GH3010_)
- Bug in DataFrame update where non-specified values could cause dtype changes (GH3016_)

.. _GH622: https://github.com/pydata/pandas/issues/622
.. _GH797: https://github.com/pydata/pandas/issues/797
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3843,8 +3843,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)

self[col] = np.where(mask, this, that)

#----------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7265,6 +7265,19 @@ def test_update(self):
[1.5, nan, 7.]])
assert_frame_equal(df, expected)

def test_update_dtypes(self):

# gh 3016
df = DataFrame([[1.,2.,False, True],[4.,5.,True,False]],
columns=['A','B','bool1','bool2'])

other = DataFrame([[45,45]],index=[0],columns=['A','B'])
df.update(other)

expected = DataFrame([[45.,45.,False, True],[4.,5.,True,False]],
columns=['A','B','bool1','bool2'])
assert_frame_equal(df, expected)

def test_update_nooverwrite(self):
df = DataFrame([[1.5, nan, 3.],
[1.5, nan, 3.],
Expand Down