为什么pandas.DataFrame.update会更改已更新数据框的dtypes? [英] Why does pandas.DataFrame.update change the dtypes of the updated dataframe?

查看:76
本文介绍了为什么pandas.DataFrame.update会更改已更新数据框的dtypes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于明显的原因,我想在更新后将列的dtype保留为int.有什么想法为什么不能按预期工作?

I would like to keep the dtypes of the columns as int after the update for obvious reasons. Any ideas why this doesn't work as expected?

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).

This is a known issue. https://github.com/pydata/pandas/issues/4094 I think your only option currently is calling astype(int) after the update.

这篇关于为什么pandas.DataFrame.update会更改已更新数据框的dtypes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆