Skip to content

Commit b83430d

Browse files
committed
Merge pull request #188 from gliptak/googlequote1
ENH: Refactor GoogleQuotesReader
2 parents f20133d + 2e515d2 commit b83430d

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

docs/source/remote_data.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ Historical dividends from Yahoo! Finance.
7373
f = web.DataReader("F", 'yahoo-dividends', start, end)
7474
f
7575
76+
.. _remote_data.yahoo_quotes:
77+
78+
Yahoo! Finance Quotes
79+
----------------------
80+
***Experimental***
81+
82+
The YahooQuotesReader class allows to get quotes data from Yahoo! Finance.
83+
84+
.. ipython:: python
85+
86+
import pandas_datareader.data as web
87+
amzn = web.get_quote_yahoo('AMZN')
88+
amzn
89+
7690
.. _remote_data.yahoo_options:
7791

7892
Yahoo! Finance Options
@@ -145,6 +159,20 @@ Google Finance
145159
f = web.DataReader("F", 'google', start, end)
146160
f.ix['2010-01-04']
147161
162+
.. _remote_data.google_quotes:
163+
164+
Google Finance Quotes
165+
----------------------
166+
***Experimental***
167+
168+
The GoogleQuotesReader class allows to get quotes data from Google Finance.
169+
170+
.. ipython:: python
171+
172+
import pandas_datareader.data as web
173+
q = web.get_quote_google(['AMZN', 'GOOG'])
174+
q
175+
148176
.. _remote_data.google_options:
149177

150178
Google Finance Options

docs/source/whatsnew/v0.2.3.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ New features
2020

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

2425
.. _whatsnew_023.api_breaking:
2526

pandas_datareader/google/quotes.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import pandas
1+
import pandas as pd
2+
from dateutil.parser import parse
3+
import numpy as np
4+
25
from pandas_datareader.base import _BaseReader
36
import json
47
import re
@@ -14,7 +17,7 @@ def url(self):
1417

1518
@property
1619
def params(self):
17-
if isinstance(self.symbols, pandas.compat.string_types):
20+
if isinstance(self.symbols, pd.compat.string_types):
1821
sym_list = self.symbols
1922
else:
2023
sym_list = ','.join(self.symbols)
@@ -25,5 +28,6 @@ def _read_lines(self, out):
2528
buffer = out.read()
2629
m = re.search('// ', buffer)
2730
result = json.loads(buffer[m.start() + len('// '):])
28-
return pandas.DataFrame([float(x['l']) for x in result],
29-
index=[x['t'] for x in result])
31+
return pd.DataFrame([[float(x['cp']), float(x['l']), np.datetime64(parse(x['lt']).isoformat())]
32+
for x in result], columns=['change_pct', 'last', 'time'],
33+
index=[x['t'] for x in result])

pandas_datareader/tests/test_google.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,30 @@ def test_google(self):
5656
self.assertRaises(Exception, web.DataReader, "NON EXISTENT TICKER",
5757
'google', start, end)
5858

59+
def assert_option_result(self, df):
60+
"""
61+
Validate returned quote data has expected format.
62+
"""
63+
self.assertTrue(isinstance(df, pd.DataFrame))
64+
self.assertTrue(len(df) > 0)
65+
66+
exp_columns = pd.Index(['change_pct', 'last', 'time'])
67+
tm.assert_index_equal(df.columns, exp_columns)
68+
69+
dtypes = [np.dtype(x) for x in ['float64', 'float64', 'datetime64[ns]']]
70+
tm.assert_series_equal(df.dtypes, pd.Series(dtypes, index=exp_columns))
71+
72+
def test_get_quote_string(self):
73+
df = web.get_quote_google('GOOG')
74+
self.assertTrue(df.ix['GOOG']['last'] > 0.0)
75+
tm.assert_index_equal(df.index, pd.Index(['GOOG']))
76+
self.assert_option_result(df)
77+
5978
def test_get_quote_stringlist(self):
6079
df = web.get_quote_google(['GOOG', 'AMZN', 'GOOG'])
6180
assert_series_equal(df.ix[0], df.ix[2])
81+
tm.assert_index_equal(df.index, pd.Index(['GOOG', 'AMZN', 'GOOG']))
82+
self.assert_option_result(df)
6283

6384
def test_get_goog_volume(self):
6485
for locale in self.locales:

0 commit comments

Comments
 (0)