2017-02-27 88 views
2

我使用fbprophet数据集进行时间序列分析。数据集有两列,分别为dateyTypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'切片'

date     y 
January 01, 1992  146376 
February 01, 1992  147079 
March 01, 1992  159336 
April 01, 1992  163669 
May 01, 1992   170068  


    date  y 
01/01/92 146376 
01/02/92 147079 
01/03/92 159336 
01/04/92 163669 
01/05/92 170068 

我首先通过使用pd.to_datetime然后拟合模型model = Prophet().fit(df)变更日期成日期时间格式。但是,结果不断显示我TypeError: int() argument must be a string, a bytes-like object or a number, not 'slice'。无论如何解决这个问题?

这里是我的代码,

df.date = pd.to_datetime(df.date) 
df['date'] = df['date'].dt.strftime('%Y-%m-%d') 
model = Prophet() 
model.fit(df) 

当我运行model.fit(df)我上面提到的TypeError显示出来。

回答

2

大多数回归和分类器方法只接受数字或字符串的dtypes,因此此错误消息抱怨您的datetime列。

假设我们有以下的数据帧:

In [63]: df 
Out[63]: 
     date  y 
0 1992-01-01 146376 
1 1992-01-02 147079 
2 1992-01-03 159336 
3 1992-01-04 163669 
4 1992-01-05 170068 

我们可以创建一个数字列 - UNIX时间戳(自1970-01-01 00:00:00 UTC秒#):

In [64]: df['unix_ts'] = df.date.astype(np.int64) // 10**9 

In [65]: df 
Out[65]: 
     date  y unix_ts 
0 1992-01-01 146376 694224000 
1 1992-01-02 147079 694310400 
2 1992-01-03 159336 694396800 
3 1992-01-04 163669 694483200 
4 1992-01-05 170068 694569600 

这是我们如何能够把它转换返回datetime dtype:

In [66]: pd.to_datetime(df.unix_ts, unit='s') 
Out[66]: 
0 1992-01-01 
1 1992-01-02 
2 1992-01-03 
3 1992-01-04 
4 1992-01-05 
Name: unix_ts, dtype: datetime64[ns] 
+0

感谢您的回答。但在使用 'df ['unix_ts'] = df.date.astype(np.int64)// 10 ** 9后,我仍然得到相同的TypeError; df ['date'] = pd.to_datetime(df.unix_ts,unit ='s'); df = df.drop(labels = ['unix_ts'],axis = 1)' – Peggy

2

我有类似的使用先知的问题。在我的情况下,问题是在“DS”列重复日期(即日期)

我加

df=df.drop_duplicates(['date'], keep='last') 

(显然,在功能上这没有任何意义,但它可能会孤立你的问题)

+1

是的。我使用'resample()'按日期收集重复日期。非常感谢你! – Peggy

相关问题