如何解决Pandas中设置不兼容数据类型的FutureWarning问题?
修复Pandas FutureWarning:类型不兼容赋值问题
问题背景
现有代码当前可运行,但触发FutureWarning,未来版本会直接报错:
FutureWarning: Setting an item of incompatible dtype is deprecated and
will raise in a future error of pandas. Value 'GRAND TOTAL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
total_df.at['New_Row', 'COMPOSITE'] = 'GRAND TOTAL'
原代码:
total.name = 'New_Row' total_df = total.to_frame().T total_df.at['New_Row', 'CURRENCY'] = '' total_df.at['New_Row', 'MANDATE'] = Portfolio total_df.at['New_Row', 'COMPOSITE'] = 'GRAND TOTAL' total_df.set_index('COMPOSITE',inplace=True)
变量total的信息:
CURRENCY MANDATE Mandate_Test USD AMOUNT 123 LOCAL AMOUNT 12 Beg. Mkt 123 End. Mkt 456 Name: New_Row, dtype: object
问题原因
total这个Series里没有COMPOSITE字段,转成DataFrame后,该列会被自动初始化为float64类型(缺失值默认用NaN,对应float类型)。直接给这个列赋值字符串类型的GRAND TOTAL,就会触发类型不兼容的告警。
修复方案
以下是几种直接可行的修复方式:
方案1:创建DataFrame时统一指定类型
转DataFrame时直接把所有列的类型设为object,避免后续赋值冲突:
total.name = 'New_Row' # 转换时指定所有列为object类型 total_df = total.to_frame().T.astype(object) total_df.at['New_Row', 'CURRENCY'] = '' total_df.at['New_Row', 'MANDATE'] = Portfolio total_df.at['New_Row', 'COMPOSITE'] = 'GRAND TOTAL' total_df.set_index('COMPOSITE', inplace=True)
方案2:单独转换COMPOSITE列类型后赋值
先将COMPOSITE列转为object类型,再进行字符串赋值:
total.name = 'New_Row' total_df = total.to_frame().T total_df.at['New_Row', 'CURRENCY'] = '' total_df.at['New_Row', 'MANDATE'] = Portfolio # 先转换列类型 total_df['COMPOSITE'] = total_df['COMPOSITE'].astype(object) total_df.at['New_Row', 'COMPOSITE'] = 'GRAND TOTAL' total_df.set_index('COMPOSITE', inplace=True)
方案3:从源头构造包含COMPOSITE的Series
先给total添加COMPOSITE字段,避免转DataFrame时生成默认类型的列:
# 提前给total添加COMPOSITE字段,用空字符串占位 total['COMPOSITE'] = '' total.name = 'New_Row' total_df = total.to_frame().T total_df.at['New_Row', 'CURRENCY'] = '' total_df.at['New_Row', 'MANDATE'] = Portfolio total_df.at['New_Row', 'COMPOSITE'] = 'GRAND TOTAL' total_df.set_index('COMPOSITE', inplace=True)
内容的提问来源于stack exchange,提问作者TourEiffel
相关产品推荐
相关产品推荐

