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

时间:2015-01-29 14:30:25

标签: 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)

输出如下:

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

感谢任何有建议的人

1 个答案:

答案 0 :(得分:5)

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