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

Tableau Prep对接Python调用Prophet报Unknown string format错误如何解决

问题根因

你本地单独运行代码时读取的是原始DD/MM/YYYY格式的日期字符串,但Tableau Prep会将内部识别为日期类型的字段自动序列化为带UTC标记的ISO 8601格式(后缀带Z,即错误提示中的2018-09-12Z格式)传递给Python脚本,原代码未兼容该格式,同时pd.to_datetime未指定日期解析规则,容易触发格式解析错误。

解决方案

修改代码中的日期解析逻辑,兼容两种日期格式,同时明确日期解析规则避免日月混淆,补充预测值的类型转换适配Tableau的输出schema:

  • 先移除日期字符串末尾的Z后缀
  • 日期解析时指定dayfirst=True适配DD/MM/YYYY格式
  • 输出前将浮点型的预测值yhat转为整数,匹配输出schema中prep_int()的要求

修改后可运行代码

import pandas as pd
from fbprophet import Prophet

def fbprophet_usage(df):
    df = df[["Date","Activated"]]  
    df.columns = ["ds", "y"]   
    # 兼容Tableau传入的带Z后缀的ISO日期和本地DD/MM/YYYY格式
    df["ds"] = df["ds"].astype(str).str.rstrip('Z')
    df["ds"] = pd.to_datetime(df["ds"], dayfirst=True, errors='coerce')
    # 过滤解析失败的空值行
    df = df.dropna(subset=['ds', 'y'])
    
    model = Prophet()
    model.fit(df)
    
    future = model.make_future_dataframe(periods = 52, freq = 'W-MON')
    forecast = model.predict(future)
    
    # 适配Tableau输出格式要求
    forecast['ds'] = forecast['ds'].dt.date
    # 预测值转整数匹配prep_int()类型
    forecast['yhat'] = forecast['yhat'].round().astype(int)
    return forecast[['ds', 'yhat']]


def get_output_schema():
    return pd.DataFrame({
            'ds' : prep_date(),
            'yhat': prep_int()
            })

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 00:06:07