为什么 pandas.DataFrame.update 会更改更新数据框的数据类型?

Why does pandas.DataFrame.update change the dtypes of the updated dataframe?

出于显而易见的原因,我想在更新后将列的数据类型保留为 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)

输出如下所示:

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

感谢任何有建议的人

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