Pandas DataReaders with support of requests
and requests-cache
What is it ?
pandas is a Python package providing fast, flexible, and expressive data structures designed to make working with "relational" or "labeled" data both easy and intuitive.
DataReaders are objects to fetch data from remote source:
- Google Finance (daily, intraday, options)
- Yahoo Finance (daily, quotes, options)
- Federal Reserve Economic Data - FRED - St. Louis Fed
- Fama-French
- World Bank
- Open Exchange Rates
- ...
This project is an unofficial rewrite of DataReaders using requests also called "HTTP for Humans". But it can be easy to use classic urlopen
using this code.
Thanks to requests-cache we can now use SQLite, mongoDB, Redis or memory as cache database (backend) and expiration time to avoid too much requests to remote servers (and speed-up execution of sprits when they are run several times). It make also possible to use some remote API calls offline (if same query was performed before).
from pandas_datareaders_unofficial import DataReader
import datetime
expire_after = 60*60 # seconds - 0: no cache - None: no cache expiration
symbol = ["GOOG", "AAPL", "MSFT"]
end_date = datetime.datetime.now()
num_days = 200
start_date = end_date - datetime.timedelta(days=num_days)
data = DataReader("GoogleFinanceDaily", expire_after=expire_after).get(symbol, start_date, end_date)
print(data)
It should return a Pandas Panel with OHLCV data like:
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 141 (major_axis) x 5 (minor_axis)
Items axis: AAPL to MSFT
Major_axis axis: 2014-05-27 00:00:00 to 2014-12-12 00:00:00
Minor_axis axis: Open to Volume
We can get a Pandas DataFrame with OHLCV data of "GOOG" using:
print(data["GOOG"])
It should display:
Open High Low Close Volume
Date
2014-05-27 556.00 566.00 554.35 565.95 2100298
2014-05-28 564.57 567.84 561.00 561.68 1647717
2014-05-29 563.35 564.00 558.71 560.08 1350657
2014-05-30 560.80 561.35 555.91 559.89 1766794
2014-06-02 560.70 560.90 545.73 553.93 1434989
... ... ... ... ... ...
2014-12-08 527.13 531.00 523.79 526.98 2327127
2014-12-09 522.14 534.19 520.50 533.37 1871268
2014-12-10 533.08 536.33 525.56 526.06 1716835
2014-12-11 527.80 533.92 527.10 528.34 1610964
2014-12-12 523.51 528.50 518.66 518.66 1989117
[141 rows x 5 columns]
Caution! This project is still experimental.
Install
From Python package index
$ pip install pandas_datareaders_unofficial
From source
Get latest version using Git
$ git clone https://github.com/femtotrader/pandas_datareaders_unofficial.git
$ cd pandas_datareaders_unofficial
$ python setup.py install
Links
- Documentation can be found at Read The Docs ;
- Source code and issue tracking can be found at GitHub.
- Feel free to tip me!
- Official Pandas DataReader can be found at https://github.com/pydata/pandas-datareader