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

使用pandas_alive生成折线gif时报str无法拼接float错误如何解决

pandas_alive动态折线图报错解决方案

报错核心原因

你转置后的table数据框的索引为原CSV的列名,未转换为datetime类型,pandas_alive折线图模式解析period_fmt参数时需要时间类型索引,若索引为字符串/浮点型的时间标识,就会触发字符串和浮点型拼接的类型错误。

解决步骤

  • 首先将转置后的索引转换为合法datetime类型,针对你使用的季度时间格式,转换代码如下:
# 若时间列名格式为「2017 Q1」用这个参数
table.index = pd.to_datetime(table.index, format='%Y Q%q')

如果你的CSV时间列名无空格,格式为2017Q1,则将format参数调整为%YQ%q即可。

  • (可选)如果你的CSV存在无效列名/空列,先做数据清洗过滤无效列,避免转换失败:
# 仅保留含季度标识的列
valid_cols = [col for col in df.columns if 'Q' in str(col)]
df = df[valid_cols]

完整可运行代码

import pandas as pd
import pandas_alive as pda
import matplotlib.animation

df = pd.read_csv("price.csv", index_col="Geography")
# 过滤无效列
valid_cols = [col for col in df.columns if 'Q' in str(col)]
df = df[valid_cols]
table = df.T
# 转换索引为datetime类型,核心修复步骤
table.index = pd.to_datetime(table.index, format='%Y Q%q')
table.plot_animated(
    kind='line',
    filename="priceOfhomesIncanada.gif",
    period_fmt="%Y",
    title="Price Of Homes In Canada From Q1 2017 To Q2 2021"
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 19:36:02