2021年5月7日星期五

Calculate slope by pandas timestamp index

I have the following dataset:

dates       A   B   C  2005-01-01  1.0 2.0 1.0  2005-01-02  2.0 1.0 1.0  2005-01-04  3.0 0.0 1.0  

I want to calculate the slopes based on the timestamp index. This should be the result:

slope:  A 0.4  B -0.7  C -0.1  

I tried this solution:

slope = df.apply(lambda x: np.polyfit(df.index), x, 1)[0])  

But it returns an error:

TypeError: float() argument must be a string or a number, not 'Timestamp'  

Any help will be greatly appreciated.

https://stackoverflow.com/questions/67443287/calculate-slope-by-pandas-timestamp-index May 08, 2021 at 09:28AM

没有评论:

发表评论