Skip to content

CLN: Refactor Options methods for underlying price and time. #16

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
Jan 26, 2015
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
24 changes: 14 additions & 10 deletions pandas_datareader/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def _option_frames_from_url(self, url):

if not hasattr(self, 'underlying_price'):
try:
self.underlying_price, self.quote_time = self._get_underlying_price(url)
self.underlying_price, self.quote_time = self._underlying_price_and_time_from_url(url)
except IndexError:
self.underlying_price, self.quote_time = np.nan, np.nan

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

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

def _get_underlying_price(self, url):
def _underlying_price_and_time_from_url(self, url):
root = self._parse_url(url)
underlying_price = self._underlying_price_from_root(root)
quote_time = self._quote_time_from_root(root)
return underlying_price, quote_time

@staticmethod
def _underlying_price_from_root(root):
underlying_price = root.xpath('.//*[@class="time_rtq_ticker Fz-30 Fw-b"]')[0]\
.getchildren()[0].text

try:
underlying_price = float(underlying_price)
except ValueError:
# check for comma
underlying_price = underlying_price.replace(',', '')
underlying_price = underlying_price.replace(',', '') #GH11

try:
underlying_price = float(underlying_price)
except ValueError:
underlying_price = np.nan

return underlying_price

@staticmethod
def _quote_time_from_root(root):
#Gets the time of the quote, note this is actually the time of the underlying price.
try:
quote_time_text = root.xpath('.//*[@class="time_rtq Fz-m"]')[0].getchildren()[1].getchildren()[0].text
##TODO: Enable timezone matching when strptime can match EST with %Z
quote_time_text = quote_time_text.split(' ')[0]
quote_time = dt.datetime.strptime(quote_time_text, "%I:%M%p")

quote_time = quote_time.replace(year=CUR_YEAR, month=CUR_MONTH, day=CUR_DAY)
except ValueError:
quote_time = np.nan

return underlying_price, quote_time
return quote_time

def _get_option_data(self, expiry, name):
frame_name = '_frames' + self._expiry_to_string(expiry)
Expand Down
10 changes: 5 additions & 5 deletions pandas_datareader/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,16 @@ def test_get_underlying_price(self):
#GH7
try:
options_object = web.Options('^spxpm', 'yahoo')
expiry_dates, urls = options_object._get_expiry_dates_and_links()
url = options_object._FINANCE_BASE_URL + urls[expiry_dates[0]]
quote_price, quote_time = options_object._get_underlying_price(url)
url = options_object._yahoo_url_from_expiry(options_object.expiry_dates[0])
root = options_object._parse_url(url)
quote_price = options_object._underlying_price_from_root(root)
except RemoteDataError as e:
raise nose.SkipTest(e)
self.assert_(isinstance(quote_price, float))

def test_sample_page_price_quote_time1(self):
#Tests the weekend quote time format
price, quote_time = self.aapl._get_underlying_price(self.html1)
price, quote_time = self.aapl._underlying_price_and_time_from_url(self.html1)
self.assert_(isinstance(price, (int, float, complex)))
self.assert_(isinstance(quote_time, (datetime, Timestamp)))

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

Expand Down