Skip to content

Refactor GoogleQuotesReader #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/source/remote_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ Historical dividends from Yahoo! Finance.
f = web.DataReader("F", 'yahoo-dividends', start, end)
f

.. _remote_data.yahoo_quotes:

Yahoo! Finance Quotes
----------------------
***Experimental***

The YahooQuotesReader class allows to get quotes data from Yahoo! Finance.

.. ipython:: python

import pandas_datareader.data as web
amzn = web.get_quote_yahoo('AMZN')
amzn

.. _remote_data.yahoo_options:

Yahoo! Finance Options
Expand Down Expand Up @@ -145,6 +159,20 @@ Google Finance
f = web.DataReader("F", 'google', start, end)
f.ix['2010-01-04']

.. _remote_data.google_quotes:

Google Finance Quotes
----------------------
***Experimental***

The GoogleQuotesReader class allows to get quotes data from Google Finance.

.. ipython:: python

import pandas_datareader.data as web
q = web.get_quote_google(['AMZN', 'GOOG'])
q

.. _remote_data.google_options:

Google Finance Options
Expand Down
1 change: 1 addition & 0 deletions docs/source/whatsnew/v0.2.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ New features

- ``DataReader`` now supports pulling data for the TSP.
- ``DataReader`` now supports Google options data source, see :ref:`here<remote_data.google_options>` (:issue:`148`).
- ``DataReader`` now supports Google quotes, see :ref:`here<remote_data.google_quotes>` (:pull:`188`).

.. _whatsnew_023.api_breaking:

Expand Down
12 changes: 8 additions & 4 deletions pandas_datareader/google/quotes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pandas
import pandas as pd
from dateutil.parser import parse
import numpy as np

from pandas_datareader.base import _BaseReader
import json
import re
Expand All @@ -14,7 +17,7 @@ def url(self):

@property
def params(self):
if isinstance(self.symbols, pandas.compat.string_types):
if isinstance(self.symbols, pd.compat.string_types):
sym_list = self.symbols
else:
sym_list = ','.join(self.symbols)
Expand All @@ -25,5 +28,6 @@ def _read_lines(self, out):
buffer = out.read()
m = re.search('// ', buffer)
result = json.loads(buffer[m.start() + len('// '):])
return pandas.DataFrame([float(x['l']) for x in result],
index=[x['t'] for x in result])
return pd.DataFrame([[float(x['cp']), float(x['l']), np.datetime64(parse(x['lt']).isoformat())]
for x in result], columns=['change_pct', 'last', 'time'],
index=[x['t'] for x in result])
21 changes: 21 additions & 0 deletions pandas_datareader/tests/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,30 @@ def test_google(self):
self.assertRaises(Exception, web.DataReader, "NON EXISTENT TICKER",
'google', start, end)

def assert_option_result(self, df):
"""
Validate returned quote data has expected format.
"""
self.assertTrue(isinstance(df, pd.DataFrame))
self.assertTrue(len(df) > 0)

exp_columns = pd.Index(['change_pct', 'last', 'time'])
tm.assert_index_equal(df.columns, exp_columns)

dtypes = [np.dtype(x) for x in ['float64', 'float64', 'datetime64[ns]']]
tm.assert_series_equal(df.dtypes, pd.Series(dtypes, index=exp_columns))

def test_get_quote_string(self):
df = web.get_quote_google('GOOG')
self.assertTrue(df.ix['GOOG']['last'] > 0.0)
tm.assert_index_equal(df.index, pd.Index(['GOOG']))
self.assert_option_result(df)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls test index and columns are correct using tm.assert_index_equal also in test_get_quote_stringlist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

def test_get_quote_stringlist(self):
df = web.get_quote_google(['GOOG', 'AMZN', 'GOOG'])
assert_series_equal(df.ix[0], df.ix[2])
tm.assert_index_equal(df.index, pd.Index(['GOOG', 'AMZN', 'GOOG']))
self.assert_option_result(df)

def test_get_goog_volume(self):
for locale in self.locales:
Expand Down