简体   繁体   中英

Pandas - pct_change()

I have downloaded some financial data from Yahoo finance and loaded in Python using Pandas. Now I am trying to use pct_change() but it's giving me an error

Code used

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

Data Frame Head

             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

Error after pct_change()

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

Few top lines of the CSV file

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

You need to_numeric for convert non numeric to 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
df['column_name'].pct_change() 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM