熊猫 - pct_change()

时间:2017-05-21 14:35:16

标签: python pandas dataframe

我已经从雅虎财经下载了一些财务数据,并使用Pandas在Python中加载。现在我正在尝试使用pct_change(),但它给了我一个错误

使用的代码

sp = pd.read_csv('SP500.csv')
sp=sp.set_index('Date')
print(sp.head())
ret=sp.pct_change()

数据框头

             Open   High    Low  Close Adj Close   Volume
Date                                                     
03-01-1950  16.66  16.66  16.66  16.66     16.66  1260000
04-01-1950  16.85  16.85  16.85  16.85     16.85  1890000
05-01-1950  16.93  16.93  16.93  16.93     16.93  2550000
06-01-1950  16.98  16.98  16.98  16.98     16.98  2010000
09-01-1950  17.09  17.09  17.08  17.08     17.08  3850000

pct_change()

后出错
TypeError: unsupported operand type(s) for /: 'str' and 'str'

CSV文件的顶行

Date,Open,High,Low,Close,Adj Close,Volume
03-01-1950,16.66,16.66,16.66,16.66,16.66,1260000
04-01-1950,16.85,16.85,16.85,16.85,16.85,1890000
05-01-1950,16.93,16.93,16.93,16.93,16.93,2550000
06-01-1950,16.98,16.98,16.98,16.98,16.98,2010000
09-01-1950,17.09,17.09,17.08,17.08,17.08,3850000
10-01-1950,17.030001,17.030001,17.030001,17.030001,17.030001,2160000
11-01-1950,17.09,17.09,17.09,17.09,17.09,2630000
12-01-1950,16.76,16.76,16.76,16.76,16.76,2970000
13-01-1950,16.67,16.67,16.67,16.67,16.67,3330000
16-01-1950,16.65,16.719999,16.65,16.719999,16.719999,2640000
17-01-1950,16.860001,16.860001,16.860001,16.860001,16.860001,1790000
18-01-1950,16.85,16.85,16.85,16.85,16.85,1570000
19-01-1950,16.870001,16.870001,16.870001,16.870001,16.870001,1170000
20-01-1950,16.9,16.9,16.9,16.9,16.9,1440000

2 个答案:

答案 0 :(得分:0)

您需要to_numeric才能将非数字转换为NaN

import pandas as pd
from pandas.compat import StringIO

temp=u"""Date,Open,High,Low,Close Adj,Close,Volume

03-01-1950,16.66,16.66,16.66,16.66,16.66,1260000
04-01-1950,16.85,16.85,16.85,16.85,16.85,1890000
05-01-1950,16.93,16.93,16.93,16.93,16.93,2550000
06-01-1950,16.98,16.98,16.98,16.98,16.98,2010000
09-01-1950,qqq,17.09,17.08,17.08,17.08,3850000"""
#after testing replace 'StringIO(temp)' to 'SP500.csv'

sp = pd.read_csv(StringIO(temp), index_col=['Date'], parse_dates=True)
print(sp.head())
             Open   High    Low  Close Adj  Close   Volume
Date                                                      
1950-03-01  16.66  16.66  16.66      16.66  16.66  1260000
1950-04-01  16.85  16.85  16.85      16.85  16.85  1890000
1950-05-01  16.93  16.93  16.93      16.93  16.93  2550000
1950-06-01  16.98  16.98  16.98      16.98  16.98  2010000
1950-09-01    qqq  17.09  17.08      17.08  17.08  3850000 <- add bad value qqq

ret = sp.apply(pd.to_numeric, errors='coerce').pct_change()
print (ret)
                Open      High       Low  Close Adj     Close    Volume
Date                                                                   
1950-03-01       NaN       NaN       NaN        NaN       NaN       NaN
1950-04-01  0.011405  0.011405  0.011405   0.011405  0.011405  0.500000
1950-05-01  0.004748  0.004748  0.004748   0.004748  0.004748  0.349206
1950-06-01  0.002953  0.002953  0.002953   0.002953  0.002953 -0.211765
1950-09-01       NaN  0.006478  0.005889   0.005889  0.005889  0.915423 <- replaced to NaN

答案 1 :(得分:0)

df['column_name'].pct_change() 

这将有助于获取特定列的百分比变化。