You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas升级后Dash-Plotly服务出现Incompatible Dtype弃用警告求助

问题

升级Pandas等包后,Dash-Plotly服务脚本出现以下FutureWarning:

FutureWarning:
Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '999999999' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.

服务可正常运行但会输出该警告,本地Python解释器运行示例代码无报错,仅启动服务时出现。相关代码如下:

import pandas as pd
import numpy as np

data = {
'subject': ['site0.microsoft.com', 'site1.microsoft.com', 'site2.microsoft.com', 'site3.microsoft.com', 'site4.microsoft.com', 'site5.microsoft.com'],
'issuer': ["Let's Encrypt", "Let's Encrypt", np.nan, "GoDaddy.com, Inc.", "GoDaddy.com, Inc.", np.nan],
'expiry': ['2023-10-02 19:24:31', '2023-09-12 12:03:23', '2023-08-03 00:00:00', '2022-09-15 07:04:04', np.nan, '2023-07-28 19:29:16'], 
'owner': [ 'Kubernetes Support L1', 'Kubernetes Support L2', 'john.doe@microsoft.com', 'IT-Applications Support L1', 'IT-Applications Support L2', 'Wildcard - Network']
}

df = pd.DataFrame(data)
df.fillna('999999999', inplace=True)

需求:用数值999999999填充DataFrame中的NaN值,寻求一行代码解决方案。

原因

Pandas升级后对数据类型兼容性的检查更严格:代码中用字符串类型的'999999999'填充,但DataFrame中存在float64类型的列(这类列因包含np.nan被自动推断为float64),字符串与float64类型不兼容,因此触发警告。

解决方案

根据需求(用数值999999999填充),直接使用数值类型的填充值即可解决,一行代码修改如下:

df.fillna(999999999, inplace=True)

如果业务需要将填充值以字符串形式存入所有列(包括原数值列),可先将整个DataFrame转为object类型再填充:

df = df.astype(object).fillna('999999999')

内容的提问来源于stack exchange,提问作者AliasSyed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 21:13:30