Chapter 8. Financial Time Series

[T]ime is what keeps everything from happening at once.

Ray Cummings

Financial time series data is one of the most important types of data in finance. This is data indexed by date and/or time. For example, prices of stocks over time represent financial time series data. Similarly, the EUR/USD exchange rate over time represents a financial time series; the exchange rate is quoted in brief intervals of time, and a collection of such quotes then is a time series of exchange rates.

There is no financial discipline that gets by without considering time an important factor. This mainly is the same as with physics and other sciences. The major tool to cope with time series data in Python is pandas. Wes McKinney, the original and main author of pandas, started developing the library when working as an analyst at AQR Capital Management, a large hedge fund. It is safe to say that pandas has been designed from the ground up to work with financial time series data.

The chapter is mainly based on two financial time series data sets in the form of comma-separated values (CSV) files. It proceeds along the following lines:

“Financial Data”

This section is about the basics of working with financial times series data using pandas: data import, deriving summary statistics, calculating changes over time, and resampling.

“Rolling Statistics”

In financial analysis, rolling statistics play an important role. These are statistics calculated in general over a fixed ...

Get Python for Finance, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.