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

使用tabulate打印DataFrame遇FutureWarning,寻求合规替代方案

解决tabulate打印DataFrame时的缺失值显示警告问题

你可以换一种方式实现需求,避免触发弃用警告:

  • 先将DataFrame所有元素转为字符串类型,此时NaN会变成'NaN'、NaT会变成'NaT'
  • 再把这两个字符串替换为空单元格

具体代码如下:

import pandas as pd
from tabulate import tabulate

# 假设df是你的目标DataFrame
formatted_df = df.astype(str).replace({"NaN": "", "NaT": ""})
print(tabulate(formatted_df))

如果需要更精细的类型控制(比如保留某些数据的原始格式表现但处理缺失值),也可以用applymap自定义处理逻辑:

def format_cell(value):
    if pd.isna(value):
        return ""
    return str(value)

formatted_df = df.applymap(format_cell)
print(tabulate(formatted_df))

这两种方式都能强制将所有内容转为字符串表示,同时把NaN/NaT替换为空单元格,而且不会触发那个FutureWarning。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 08:30:56