Book Image

Mastering Pandas for Finance

By : Michael Heydt
Book Image

Mastering Pandas for Finance

By: Michael Heydt

Overview of this book

Table of Contents (16 chapters)
Mastering pandas for Finance
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Frequency conversion of time-series data


The frequency of the data in a time-series can be converted in pandas using the .asfreq() method of a Series or DataFrame. To demonstrate, we will use the following small subset of the MSFT stock closing values:

In [39]:
   sample = msftAC[:2]
   sample

Out[39]:
   Date
   2012-01-03    24.42183
   2012-01-04    24.99657
   Name: Adj Close, dtype: float64

We have extracted the first 2 days of adjusted close values. Let's suppose we want to resample this to have hourly sampling of data in-between the index labels. We can do this with the following command:

In [40]:
   sample.asfreq("H")

Out[40]:
   2012-01-03 00:00:00    24.42183
   2012-01-03 01:00:00         NaN
   2012-01-03 02:00:00         NaN
                            ...   
   2012-01-03 22:00:00         NaN
   2012-01-03 23:00:00         NaN
   2012-01-04 00:00:00    24.99657
   Freq: H, Name: Adj Close, dtype: float64

A new index with hourly index labels has been created by pandas, but...