使用FB Prophet预测出勤人数时遇日期解析错误求助
解决FB Prophet日期格式解析错误问题
你执行m <- prophet(df)时遇到以下错误:
Error in setup_dataframe(m, history, initialize_scales = TRUE) :
Unable to parse date format in column ds. Convert to date format (%Y-%m-%d or %Y-%m-%d %H:%M:%S) and check that there are no NAs.
问题根源是你的CSV中ds列的日期格式为月/日/年(如1/1/2020),而Prophet默认仅识别年-月-日或带时间戳的标准格式,需要手动将ds列转换为R的日期类型。
解决步骤:
- 读取CSV数据:你的数据是制表符分隔的,用
read.delim读取:df <- read.delim("your_file.csv", sep = "\t", stringsAsFactors = FALSE) - 转换日期格式:
方法1:使用lubridate包的mdy函数(推荐,自动识别月/日/年格式):
方法2:使用base R的library(lubridate) df$ds <- mdy(df$ds)as.Date函数,指定格式:df$ds <- as.Date(df$ds, format = "%m/%d/%Y") - 验证转换结果:检查是否存在NA值(确保所有日期都被正确解析):
若输出为0,说明转换成功。sum(is.na(df$ds)) - 重新运行Prophet:
library(prophet) m <- prophet(df)
内容的提问来源于stack exchange,提问作者Carlos Danso
相关产品推荐
相关产品推荐

