为什么pandas.DataFrame.update会更改更新数据帧的dtypes?

Bre*_*ire 3 python pandas

由于显而易见的原因,我想在更新后将列的dtypes保持为int.任何想法为什么这不能按预期工作?

import pandas as pd

df1 = pd.DataFrame([
    {'a': 1, 'b': 2, 'c': 'foo'},
    {'a': 3, 'b': 4, 'c': 'baz'},
])

df2 = pd.DataFrame([
    {'a': 1, 'b': 8, 'c': 'bar'},
])

print 'dtypes before update:\n%s\n%s' % (df1.dtypes, df2.dtypes)

df1.update(df2)

print '\ndtypes after update:\n%s\n%s' % (df1.dtypes, df2.dtypes)
Run Code Online (Sandbox Code Playgroud)

输出如下所示:

dtypes before update:
a     int64
b     int64
c    object
dtype: object
a     int64
b     int64
c    object
dtype: object

dtypes after update:
a    float64
b    float64
c     object
dtype: object
a     int64
b     int64
c    object
dtype: object
Run Code Online (Sandbox Code Playgroud)

感谢任何有一些建议的人

JAB*_*JAB 6

这是一个已知的问题.https://github.com/pydata/pandas/issues/4094我认为您目前唯一的选择是astype(int)在更新后调用.