本文整理汇总了Python中pandas.io.data.DataReader.ix[to_date]方法的典型用法代码示例。如果您正苦于以下问题:Python DataReader.ix[to_date]方法的具体用法?Python DataReader.ix[to_date]怎么用?Python DataReader.ix[to_date]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.io.data.DataReader
的用法示例。
在下文中一共展示了DataReader.ix[to_date]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retrieve_stock
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import ix[to_date] [as 别名]
def retrieve_stock(symbol,from_date,to_date):
'''retrieve stock's prices'''
import pandas
from pandas.io.data import DataReader
from math import ceil
#try read it from local file
csv_path = get_csv_path(symbol)
if isfile(csv_path):
with local_file_pool:
data = pandas.read_csv(csv_path)
if not data.empty:
if data.columns[0] != 'Date':
data.rename(columns ={data.columns[0]:'Date'},inplace = True)
data.set_index('Date',inplace = True)
data.index = pandas.to_datetime(data.index)
if data.index.min() <= from_date and to_date <= data.index.max():
return data[data.Volume != 0]
else:
#request more data ( not incremental)
from_date = min(data.index.min(),from_date)
to_date = max(data.index.max(),to_date)
#Fetch from yahoo finance or google finance
with network_pool:
yahoo_sym = convert_to_yahoo_symbol(symbol).lower()
try:
prices = DataReader(yahoo_sym,'yahoo',start = from_date,end = to_date)
prices = prices[prices.Volume != 0]
except:
# Cannot fetch from yahoo finance
prices = pandas.DataFrame()
for page in range(0,ceil((to_date-from_date).days / 200)):
url = google_historical_price_page.format(symbol.replace(':','%3A'),from_date.strftime('%b+%d%%2C+%Y'),to_date.strftime('%b+%d%%2C+%Y'),page * 200)
raw = pandas.read_html(url)
if len(raw) <= 2:
break
else:
data = raw[-1]
if len(data.keys()) >= 6:
data.rename(columns = { 0:'Date',1:'Open' ,2: 'High', 3: 'Low',4: 'Close',5:'Volume'},inplace = True)
data = data[data.Volume !='0']
data = data[data.Volume !='-']
else:
data.rename(columns = { 0:'Date',1:'Close'},inplace = True)
data.set_index('Date',inplace = True)
data = data.ix[1:]
data.index = pandas.to_datetime(data.index)
prices = prices.append(data.astype('float'))
if to_date.date() == datetime.today().date():
import urllib.request
from bs4 import BeautifulSoup
try:
html = urllib.request.urlopen(yahoo_daliy_price_page.format(yahoo_sym)).read()
soup = BeautifulSoup(html)
if soup.title.string.lower().startswith(yahoo_sym):
span = soup.find(attrs = {'class':'time_rtq_ticker'})
if span != None:
close = float(span.contents[0].string.replace(',',''))
if len(prices.columns) == 6:
open,high,low,vol = 0,0,0,0
for elem in soup.find_all(attrs = {'class':'yfnc_tabledata1'}):
if elem.previous.startswith('Open'):
open = float(elem.contents[0].string.replace(',',''))
elif elem.previous.startswith('Day'): #Day's Range
low = float(elem.contents[0].string.replace(',',''))
high = float(elem.contents[2].string.replace(',',''))
elif elem.previous.startswith('Volume'):
vol = float(elem.contents[0].string.replace(',',''))
prices.ix[to_date] = open,high,low,close,vol,0
else:
prices.ix[to_date] = close
except:
import traceback
traceback.print_exc()
pass
#save to local
prices.to_csv(csv_path,index_label = 'Date')
return prices