Skip to content

Commit 33a7dfa

Browse files
Merge pull request #16 from dstephens99/issue7
CLN: Refactor Options methods for underlying price and time.
2 parents 5d7f044 + ee6f041 commit 33a7dfa

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

pandas_datareader/data.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ def _option_frames_from_url(self, url):
689689

690690
if not hasattr(self, 'underlying_price'):
691691
try:
692-
self.underlying_price, self.quote_time = self._get_underlying_price(url)
692+
self.underlying_price, self.quote_time = self._underlying_price_and_time_from_url(url)
693693
except IndexError:
694694
self.underlying_price, self.quote_time = np.nan, np.nan
695695

@@ -704,34 +704,38 @@ def _option_frames_from_url(self, url):
704704

705705
return {'calls': calls, 'puts': puts}
706706

707-
def _get_underlying_price(self, url):
707+
def _underlying_price_and_time_from_url(self, url):
708708
root = self._parse_url(url)
709+
underlying_price = self._underlying_price_from_root(root)
710+
quote_time = self._quote_time_from_root(root)
711+
return underlying_price, quote_time
712+
713+
@staticmethod
714+
def _underlying_price_from_root(root):
709715
underlying_price = root.xpath('.//*[@class="time_rtq_ticker Fz-30 Fw-b"]')[0]\
710716
.getchildren()[0].text
711-
712-
try:
713-
underlying_price = float(underlying_price)
714-
except ValueError:
715-
# check for comma
716-
underlying_price = underlying_price.replace(',', '')
717+
underlying_price = underlying_price.replace(',', '') #GH11
717718

718719
try:
719720
underlying_price = float(underlying_price)
720721
except ValueError:
721722
underlying_price = np.nan
722723

724+
return underlying_price
725+
726+
@staticmethod
727+
def _quote_time_from_root(root):
723728
#Gets the time of the quote, note this is actually the time of the underlying price.
724729
try:
725730
quote_time_text = root.xpath('.//*[@class="time_rtq Fz-m"]')[0].getchildren()[1].getchildren()[0].text
726731
##TODO: Enable timezone matching when strptime can match EST with %Z
727732
quote_time_text = quote_time_text.split(' ')[0]
728733
quote_time = dt.datetime.strptime(quote_time_text, "%I:%M%p")
729-
730734
quote_time = quote_time.replace(year=CUR_YEAR, month=CUR_MONTH, day=CUR_DAY)
731735
except ValueError:
732736
quote_time = np.nan
733737

734-
return underlying_price, quote_time
738+
return quote_time
735739

736740
def _get_option_data(self, expiry, name):
737741
frame_name = '_frames' + self._expiry_to_string(expiry)

pandas_datareader/tests/test_data.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,16 @@ def test_get_underlying_price(self):
332332
#GH7
333333
try:
334334
options_object = web.Options('^spxpm', 'yahoo')
335-
expiry_dates, urls = options_object._get_expiry_dates_and_links()
336-
url = options_object._FINANCE_BASE_URL + urls[expiry_dates[0]]
337-
quote_price, quote_time = options_object._get_underlying_price(url)
335+
url = options_object._yahoo_url_from_expiry(options_object.expiry_dates[0])
336+
root = options_object._parse_url(url)
337+
quote_price = options_object._underlying_price_from_root(root)
338338
except RemoteDataError as e:
339339
raise nose.SkipTest(e)
340340
self.assert_(isinstance(quote_price, float))
341341

342342
def test_sample_page_price_quote_time1(self):
343343
#Tests the weekend quote time format
344-
price, quote_time = self.aapl._get_underlying_price(self.html1)
344+
price, quote_time = self.aapl._underlying_price_and_time_from_url(self.html1)
345345
self.assert_(isinstance(price, (int, float, complex)))
346346
self.assert_(isinstance(quote_time, (datetime, Timestamp)))
347347

@@ -362,7 +362,7 @@ def test_chop_out_of_strike_range(self):
362362
def test_sample_page_price_quote_time2(self):
363363
#Tests the EDT page format
364364
#regression test for #8741
365-
price, quote_time = self.aapl._get_underlying_price(self.html2)
365+
price, quote_time = self.aapl._underlying_price_and_time_from_url(self.html2)
366366
self.assert_(isinstance(price, (int, float, complex)))
367367
self.assert_(isinstance(quote_time, (datetime, Timestamp)))
368368

0 commit comments

Comments
 (0)