Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Using Pandas and statsmodels


We give a few examples in the following section for the two modules we are going to use intensively in the rest of the book. Again, the Pandas module is for data manipulation and the statsmodels module is for the statistical analysis.

Using Pandas

In the following example, we generate two time series starting from January 1, 2013. The names of those two time series (columns) are A and B:

>>>import numpy as np
>>>import pandas as pd
>>>dates=pd.date_range('20130101',periods=5)
>>>np.random.seed(12345)
>>>x=pd.DataFrame(np.random.rand(5,2),index=dates,columns=('A','B'))

First, we import both NumPy and Pandas modules. The pd.date_range() function is used to generate an index array. The x variable is a Pandas' data frame with dates as its index. Later in this chapter, we will discuss pd.DataFrame(). The columns() function defines the names of those columns. Because the seed() function is used in the program, anyone can generate...