diff --git a/RELEASE.rst b/RELEASE.rst index 970b89c99a7b1..84934bf3d51e1 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -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 diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7ae2cc6d5b6ed..13a2bb5a18db7 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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) #---------------------------------------------------------------------- diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 85f66148eba8a..85f50641965cc 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -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.],