Skip to content

Commit 91d09a0

Browse files
beluga9femtotrader
authored andcommitted
BUG: Fix yahoo options (#244)
Closes #212
1 parent 9797c9c commit 91d09a0

File tree

7 files changed

+232
-14899
lines changed

7 files changed

+232
-14899
lines changed

pandas_datareader/tests/data/yahoo_options1.html

Lines changed: 0 additions & 6065 deletions
This file was deleted.

pandas_datareader/tests/data/yahoo_options1.json

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

pandas_datareader/tests/data/yahoo_options2.html

Lines changed: 0 additions & 5853 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"optionChain":{"result":[],"error":null}}

pandas_datareader/tests/data/yahoo_options3.html

Lines changed: 0 additions & 2807 deletions
This file was deleted.

pandas_datareader/tests/test_yahoo_options.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import nose
99
import pandas.util.testing as tm
10-
from pandas_datareader.tests._utils import _skip_if_no_lxml
1110

1211
import pandas_datareader.data as web
1312
from pandas_datareader._utils import RemoteDataError
@@ -18,7 +17,6 @@ class TestYahooOptions(tm.TestCase):
1817
@classmethod
1918
def setUpClass(cls):
2019
super(TestYahooOptions, cls).setUpClass()
21-
_skip_if_no_lxml()
2220

2321
# aapl has monthlies
2422
cls.aapl = web.Options('aapl', 'yahoo')
@@ -30,10 +28,9 @@ def setUpClass(cls):
3028
cls.year = cls.year + 1
3129
cls.expiry = datetime(cls.year, cls.month, 1)
3230
cls.dirpath = tm.get_data_path()
33-
cls.html1 = 'file://' + os.path.join(cls.dirpath, 'yahoo_options1.html')
34-
cls.html2 = 'file://' + os.path.join(cls.dirpath, 'yahoo_options2.html')
35-
cls.html3 = 'file://' + os.path.join(cls.dirpath, 'yahoo_options3.html') # Empty table GH#22
36-
cls.data1 = cls.aapl._option_frames_from_url(cls.html1)['puts']
31+
cls.json1 = 'file://' + os.path.join(cls.dirpath, 'yahoo_options1.json')
32+
cls.json2 = 'file://' + os.path.join(cls.dirpath, 'yahoo_options2.json') # Empty table GH#22
33+
cls.data1 = cls.aapl._process_data(cls.aapl._parse_url(cls.json1))
3734

3835
@classmethod
3936
def tearDownClass(cls):
@@ -49,12 +46,13 @@ def assert_option_result(self, df):
4946

5047
exp_columns = pd.Index(['Last', 'Bid', 'Ask', 'Chg', 'PctChg', 'Vol', 'Open_Int',
5148
'IV', 'Root', 'IsNonstandard', 'Underlying',
52-
'Underlying_Price', 'Quote_Time'])
49+
'Underlying_Price', 'Quote_Time', 'Last_Trade_Date', 'JSON'])
5350
tm.assert_index_equal(df.columns, exp_columns)
5451
tm.assert_equal(df.index.names, [u'Strike', u'Expiry', u'Type', u'Symbol'])
5552

5653
dtypes = [np.dtype(x) for x in ['float64'] * 5 +
57-
['int64', 'int64', 'float64', 'object', 'bool', 'object', 'float64', 'datetime64[ns]']]
54+
['int64', 'int64', 'float64', 'object', 'bool', 'object', 'float64', 'datetime64[ns]',
55+
'datetime64[ns]', 'object']]
5856
tm.assert_series_equal(df.dtypes, pd.Series(dtypes, index=exp_columns))
5957

6058
def test_get_options_data(self):
@@ -103,7 +101,7 @@ def test_get_put_data(self):
103101

104102
def test_get_expiry_dates(self):
105103
try:
106-
dates, _ = self.aapl._get_expiry_dates_and_links()
104+
dates = self.aapl._get_expiry_dates()
107105
except RemoteDataError as e: # pragma: no cover
108106
raise nose.SkipTest(e)
109107
self.assertTrue(len(dates) > 1)
@@ -139,21 +137,13 @@ def test_get_underlying_price(self):
139137
# GH7
140138
try:
141139
options_object = web.Options('^spxpm', 'yahoo')
142-
url = options_object._yahoo_url_from_expiry(options_object.expiry_dates[0])
143-
root = options_object._parse_url(url)
144-
quote_price = options_object._underlying_price_from_root(root)
140+
quote_price = options_object.underlying_price
145141
except RemoteDataError as e: # pragma: no cover
146142
raise nose.SkipTest(e)
147143
self.assertTrue(isinstance(quote_price, float))
148144

149145
# Tests the weekend quote time format
150-
price, quote_time = self.aapl._underlying_price_and_time_from_url(self.html1)
151-
self.assertTrue(isinstance(price, (int, float, complex)))
152-
self.assertTrue(isinstance(quote_time, (datetime, pd.Timestamp)))
153-
154-
# Tests the EDT page format
155-
# regression test for #8741
156-
price, quote_time = self.aapl._underlying_price_and_time_from_url(self.html2)
146+
price, quote_time = self.aapl.underlying_price, self.aapl.quote_time
157147
self.assertTrue(isinstance(price, (int, float, complex)))
158148
self.assertTrue(isinstance(quote_time, (datetime, pd.Timestamp)))
159149

@@ -194,7 +184,7 @@ def test_month_year(self):
194184

195185
def test_empty_table(self):
196186
# GH22
197-
empty = self.aapl._option_frames_from_url(self.html3)['puts']
187+
empty = self.aapl._process_data(self.aapl._parse_url(self.json2))
198188
self.assertTrue(len(empty) == 0)
199189

200190
if __name__ == '__main__':

0 commit comments

Comments
 (0)