Python for Finance: Analyze Big Financial Data

(Elle) #1
A Year end frequency

BA

Business year end frequency

AS

Year start frequency

BAS

Business year start frequency

H

Hourly frequency

T

Minutely frequency

S

Secondly frequency

L

Milliseonds

U

Microseconds

In this subsection, we start with a NumPy ndarray object and end with an enriched version


in the form of a pandas DataFrame object. But does this procedure work the other way


around as well? Yes, it does:


In  [ 26 ]: np.array(df).round( 6 )
Out[26]: array([[-0.737304, 1.065173, 0.073406, 1.301174],
[-0.788818, -0.985819, 0.403796, -1.753784],
[-0.155881, -1.752672, 1.037444, -0.400793],
[-0.777546, 1.730278, 0.417114, 0.184079],
[-1.76366 , -0.375469, 0.098678, -1.553824],
[-1.134258, 1.401821, 1.227124, 0.979389],
[ 0.458838, -0.143187, 1.565701, -2.085863],
[-0.103058, -0.36617 , -0.478036, -0.03281 ],
[ 1.040318, -0.128799, 0.786187, 0.414084]])

ARRAYS AND DATAFRAMES

You can generate a DataFrame object in general from an ndarray object. But you can also easily generate an

ndarray object out of a DataFrame by using the function array of NumPy.

Basic Analytics


Like NumPy arrays, the pandas DataFrame class has built in a multitude of convenience


methods. For example, you can easily get the column-wise sums, means, and cumulative


sums as follows:


In  [ 27 ]: df.sum()
Out[27]: No1 -3.961370
No2 0.445156
No3 5.131414
No4 -2.948346
dtype: float64
In [ 28 ]: df.mean()
Out[28]: No1 -0.440152
Free download pdf