Python for Finance: Analyze Big Financial Data

(Elle) #1

The tail method provides us with the five last rows of the data set:


In  [ 49 ]: DAX.tail()
Out[49]: Open High Low Close Volume Adj Close
Date
2014-09-22 9748.53 9812.77 9735.69 9749.54 73981000 9749.54
2014-09-23 9713.40 9719.66 9589.03 9595.03 88196000 9595.03
2014-09-24 9598.77 9669.45 9534.77 9661.97 85850600 9661.97
2014-09-25 9644.36 9718.11 9482.54 9510.01 97697000 9510.01
2014-09-26 9500.55 9545.34 9454.88 9490.55 83499600 9490.55

To get a better overview of the index’s history, a plot is again generated easily with the


plot method (cf. Figure 6-3):


In  [ 50 ]: DAX[‘Close’].plot(figsize=( 8 ,  5 ))

Figure 6-3. Historical DAX index levels

Retrieving data and visualizing it is one thing. Implementing more complex analytics tasks


is another. Like NumPy ndarrays, pandas allows for vectorized mathematical operations on


whole, and even complex, DataFrame objects. Take the log returns based on the daily


closing prices as an example. Adding a column with the respective information could be


achieved with the following code, which first generates a new, empty column and then


iterates over all indexes to calculate the single log return values step by step:


In  [ 51 ]: %%time
DAX[‘Ret_Loop’] = 0.0
for i in range( 1 , len(DAX)):
DAX[‘Ret_Loop’][i] = np.log(DAX[‘Close’][i] /
DAX[‘Close’][i - 1 ])
Out[51]: CPU times: user 452 ms, sys: 12 ms, total: 464 ms
Wall time: 449 ms
In [ 52 ]: DAX[[‘Close’, ‘Ret_Loop’]].tail()
Out[52]: Close Ret_Loop
Date
2014-09-22 9749.54 -0.005087
2014-09-23 9595.03 -0.015975
2014-09-24 9661.97 0.006952
2014-09-25 9510.01 -0.015853
2014-09-26 9490.55 -0.002048

Alternatively, you can use vectorized code to reach the same result without looping. To


this end, the shift method is useful; it shifts Series or whole DataFrame objects relative


to their index, forward as well as backward. To accomplish our goal, we need to shift the


Close column by one day, or more generally, one index position:

Free download pdf