Skip to content

Commit 2f7b0e4

Browse files
committed
Merge pull request #3021 from jreback/update_3016
BUG: Bug in DataFrame update where non-specified values could cause dtype changes
2 parents 7b03ff2 + 78c9bcf commit 2f7b0e4

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pandas 0.11.0
143143
- Bug in ``icol`` with negative indicies was incorrect producing incorrect return values (see GH2922_)
144144
- Bug in DataFrame column insertion when the column creation fails, existing frame is left in
145145
an irrecoverable state (GH3010_)
146+
- Bug in DataFrame update where non-specified values could cause dtype changes (GH3016_)
146147

147148
.. _GH622: https://github.com/pydata/pandas/issues/622
148149
.. _GH797: https://github.com/pydata/pandas/issues/797

pandas/core/frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -3843,8 +3843,13 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
38433843

38443844
if overwrite:
38453845
mask = isnull(that)
3846+
3847+
# don't overwrite columns unecessarily
3848+
if mask.all():
3849+
continue
38463850
else:
38473851
mask = notnull(this)
3852+
38483853
self[col] = np.where(mask, this, that)
38493854

38503855
#----------------------------------------------------------------------

pandas/tests/test_frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -7265,6 +7265,19 @@ def test_update(self):
72657265
[1.5, nan, 7.]])
72667266
assert_frame_equal(df, expected)
72677267

7268+
def test_update_dtypes(self):
7269+
7270+
# gh 3016
7271+
df = DataFrame([[1.,2.,False, True],[4.,5.,True,False]],
7272+
columns=['A','B','bool1','bool2'])
7273+
7274+
other = DataFrame([[45,45]],index=[0],columns=['A','B'])
7275+
df.update(other)
7276+
7277+
expected = DataFrame([[45.,45.,False, True],[4.,5.,True,False]],
7278+
columns=['A','B','bool1','bool2'])
7279+
assert_frame_equal(df, expected)
7280+
72687281
def test_update_nooverwrite(self):
72697282
df = DataFrame([[1.5, nan, 3.],
72707283
[1.5, nan, 3.],

0 commit comments

Comments
 (0)