Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.15.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,5 @@ Bug Fixes


- Fixed ValueError raised by cummin/cummax when datetime64 Series contains NaT. (:issue:`8965`)

- Bug in ``Options`` where parsing the underlying price returns a ValueError when the price has a thousands separator in the HTML text. (:issue:`9010`)
17 changes: 16 additions & 1 deletion pandas/io/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings
import tempfile
import datetime as dt
import locale
import time

from collections import defaultdict
Expand Down Expand Up @@ -672,6 +673,20 @@ def _yahoo_url_from_expiry(self, expiry):

return self._FINANCE_BASE_URL + expiry_links[expiry]

@staticmethod
def _string_to_float(string):
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

much to do float(value) and catch the exception (where u can then substitute out the ,)
no need to do any locale stuff

Take a string representation of a floating number that may have a
thousands separator in it and turn it into a float
"""
old_loc = locale.getlocale()
# Yahoo seems to report its prices in English/US format,
# regardless of locale settings in the browser
locale.setlocale( locale.LC_NUMERIC, 'en_US' )
floatret = locale.atof( string )
locale.setlocale( locale.LC_NUMERIC, old_loc )
return floatret

def _option_frames_from_url(self, url):
frames = read_html(url)
nframes = len(frames)
Expand All @@ -698,7 +713,7 @@ def _option_frames_from_url(self, url):

def _get_underlying_price(self, url):
root = self._parse_url(url)
underlying_price = float(root.xpath('.//*[@class="time_rtq_ticker Fz-30 Fw-b"]')[0]\
underlying_price = self._string_to_float(root.xpath('.//*[@class="time_rtq_ticker Fz-30 Fw-b"]')[0]\
Copy link
Contributor

Choose a reason for hiding this comment

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

use a try: except around the conversion, if it hits the excpet try a comma subtitute and float conversion, if THAT fails then mark as NaN and move on

.getchildren()[0].text)

#Gets the time of the quote, note this is actually the time of the underlying price.
Expand Down
11 changes: 11 additions & 0 deletions pandas/io/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ def test_get_expiry_dates(self):
raise nose.SkipTest(e)
self.assertTrue(len(dates) > 1)

@network
def test_get_underlying_price(self):
try:
options_object = web.Options('^spxpm', 'yahoo')
expiry_dates, urls = options_object._get_expiry_dates_and_links()
url = options_object._FINANCE_BASE_URL + urls.values()[0]
quote_price, quote_time = options_object._get_underlying_price( url )
except RemoteDataError as e:
raise nose.SkipTest(e)
self.assertIsInstance( quote_price, float )

@network
def test_get_all_data(self):
try:
Expand Down