本文整理汇总了Python中pandas.io.data.DataReader.itertuples方法的典型用法代码示例。如果您正苦于以下问题:Python DataReader.itertuples方法的具体用法?Python DataReader.itertuples怎么用?Python DataReader.itertuples使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.io.data.DataReader
的用法示例。
在下文中一共展示了DataReader.itertuples方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: savetickerinfo
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import itertuples [as 别名]
def savetickerinfo(SPH_id, ticker, security_id):
rp_tkr = ticker.replace('.', '-')
yrs_of_pricing = 10
yrs_of_adj = 10
enddate = date.today()
tickerdata =\
DataReader(rp_tkr, "yahoo", datetime(enddate.year-yrs_of_adj,
enddate.month,
enddate.day))
tickerdata['Adj Factor'] =\
tickerdata['Close'].divide(tickerdata['Adj Close'])
#
tickerdata['Adj Factor Shifted'] =\
tickerdata['Adj Factor'].shift(1)
#
tickerdata['Adj Factor Old/New'] =\
tickerdata['Adj Factor Shifted'].divide(tickerdata['Adj Factor'])
#
# The data_to_cp does not need to exit if we are not limited on rows
# to the extent we need past prices for the full period,
# we may collapse these two calls into one DataFrame
if yrs_of_pricing != yrs_of_adj:
data_to_cp =\
DataReader(rp_tkr, "yahoo", datetime(enddate.year-yrs_of_pricing,
enddate.month,
enddate.day))
data_to_cp['Adj Factor'] =\
data_to_cp['Close'].divide(data_to_cp['Adj Close'])
#
data_to_cp['Adj Factor Shifted'] =\
data_to_cp['Adj Factor'].shift(1)
#
data_to_cp['Adj Factor Old/New'] =\
data_to_cp['Adj Factor Shifted'].divide(data_to_cp['Adj Factor'])
else:
data_to_cp = tickerdata
#
closepricesforsave = []
for a in data_to_cp.itertuples():
newcloseprice = ClosePrice(close_price=a[4],
adj_close_price=a[6],
close_date=str(datetime.date(a[0])),
securitypricehist_id=SPH_id)
closepricesforsave.append(newcloseprice)
ClosePrice.objects.filter(securitypricehist_id=SPH_id)\
.delete()
ClosePrice.objects.bulk_create(closepricesforsave)
#
splitrecords = tickerdata.loc[tickerdata['Adj Factor Old/New'] >= 1.1]
#
dictforsave = splitrecords.to_dict()['Adj Factor Old/New']
#
for key in dictforsave:
if not SplitOrAdjustmentEvent.objects.filter(security_id=security_id)\
.filter(event_date=str(datetime.date(key)))\
.exists():
SplitOrAdjustmentEvent(
security_id=security_id,
adjustment_factor=round(dictforsave[key], 2),
event_date=str(datetime.date(key)))\
.save()