本文整理汇总了Python中pandas.io.data.DataReader.reset_index方法的典型用法代码示例。如果您正苦于以下问题:Python DataReader.reset_index方法的具体用法?Python DataReader.reset_index怎么用?Python DataReader.reset_index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.io.data.DataReader
的用法示例。
在下文中一共展示了DataReader.reset_index方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: readData
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import reset_index [as 别名]
def readData(self, lookupTicker, source, start, end):
'''Read the data - assumes start and end are datetime.date objects'''
try:
lookupTicker = str(lookupTicker)
if source == 'Quandl':
#use Quandl reader
start = str(start)
end = str(end)
data = Quandl.get(lookupTicker,
authtoken = self.quandlAuthToken,
trim_start = start,
trim_end= end)
else:
#use pandas.io DataReader
data = DataReader(lookupTicker, source , start, end)
data = data.reset_index()
logging.info("Read ticker {}".format(lookupTicker))
except:
logging.error("importData: Can't read ticker {}".format(lookupTicker))
raise
else:
return data
示例2: importData
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import reset_index [as 别名]
def importData():
#Start Time
start = datetime(2010,1,1)
end = datetime.date(datetime.now())
data = DataReader(sp500constituents[0], "yahoo", start, end)
en = enumerate(sp500constituents)
[i for i, x in en if x=='WFMI']
sp500constituents[200:len(sp500constituents)]
problems = []
dataImportProblems = []
for series in sp500constituents[485:len(sp500constituents)]:
print series
try:
data = DataReader(series, "yahoo", start, end)
data = data.reset_index()
except:
print "Can't read {}".format(series)
dataImportProblems.append(series)
continue
con = sqlite3.connect("/home/phcostello/Documents/Data/FinanceData.sqlite")
try:
psql.write_frame( data, series, con)
con.commit()
except:
print "Problems with {}".format(series)
problems.append(series)
finally:
con.close()
#changing tables to have date formats so RODBC driver recognizes
#Should check that this is occuring above.
con = sqlite3.connect("/home/phcostello/Documents/Data/FinanceData.sqlite")
for tb in sp500constituents:
if psql.has_table(tb, con):
sqltxt = "SELECT * FROM {}".format(tb)
#print sqltxt
data = psql.read_frame(sqltxt, con)
sqlDropTxt = 'DROP TABLE "main"."{}"'.format(tb)
#print sqlDropTxt
psql.execute(sqlDropTxt, con)
con.commit()
psql.write_frame( data, tb, con)
con.commit()
con.close()
示例3: determine_trend
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import reset_index [as 别名]
def determine_trend(symbol, trade_date=datetime.datetime.now(),
trend_length=20, trend_end_days_ago=1):
"""
returns a "trend score" derived from performing a linear
regression on the daily closing price of the stock
identified by symbol.
This score is on the following scale:
"Negative Trend" ----- "Positive Trend"
-1.0 -----------0-----------1.0
The score considers both the slope of the linear model and
the "fit" (based on the r^2 output of the ols function)
trade_date -- date used to determine the trend from
symbol -- the stock symbol to determine trend for
trend_length -- the number of days to derive trend for
trend_end_days_ago -- the number of days prior to trend_date to determine
when to end the trend analysis
"""
end_date = datetime.date.today() - datetime.timedelta(days=trend_end_days_ago)
start_date = end_date - datetime.timedelta(days=trend_length)
stock_df = DataReader(symbol, "yahoo", start=start_date, end=end_date)
stock_df = stock_df.reset_index()
result = ols(y=stock_df['Adj Close'], x=Series(stock_df.index))
# This is the formula for the score without adjusting to fit within the
# -1.0 - 1.0 scale. Basically this takes the slope/starting price to get
# the % change per day. This is divided by a somewhat arbitrary value of
score = (result.beta['x']/result.beta['intercept'])/LARGE_DAILY_GAIN * result.r2
# Now adjust the score to keep it in our trend range:
if score > 1.0:
return 1.0
elif score < -1.0:
return -1.0
else:
return score
示例4: DataReader
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import reset_index [as 别名]
trades['MARKET'] = 'OTHER'
trades['MARKET'][trades['MARKETSEGMENT']==4] = 'HG'
trades['MARKET'][trades['MARKETSEGMENT']==7] = 'FRN'
trades['MARKET'][trades['MARKETSEGMENT']==0] = 'HY'
trades['MARKET'][trades['MARKETSEGMENT']==8] = 'AGY'
trades = trades.join(pd.get_dummies(trades['MARKET'], prefix='MKT'))
trades = trades.drop(['MARKET'], axis=1)
del trades['ISCVTPREFSTOCK']
del trades['MARKETSEGMENT']
#Pull ETF data in liey of indices
#Scrape from Yahoo
LQD = DataReader("LQD", "yahoo", datetime(2013,1,1), datetime(2013,12,31))
LQD['Date'] = LQD.index
LQD = LQD.reset_index(drop=True)
#Get the vix
VIX = DataReader("VIX", "yahoo", datetime(2013,1,1), datetime(2013,12,31))
VIX['Date'] = VIX.index
VIX = VIX.reset_index(drop=True)
stocks =pd.merge(VIX, LQD, left_on='Date', right_on='Date', how='outer', suffixes=('_VIX','_LQD') )
stocks=stocks[['Date','Close_VIX','Close_LQD']]
#Get Treasury Rates
UST_2 = DataReader("DGS2", "fred", datetime(2013,1,1), datetime(2013,12,31))
UST_10 = DataReader("DGS10", "fred", datetime(2013,1,1), datetime(2013,12,31))
UST_30 = DataReader("DGS30", "fred", datetime(2013,1,1), datetime(2013,12,31))
UST = pd.DataFrame({'Date': UST_2['DGS2'].index.values,'UST2': UST_2['DGS2'].values ,'UST10': UST_10['DGS10'].values,'UST30': UST_30['DGS30'].values})
ex_data =pd.merge(stocks, UST, left_on='Date', right_on='Date', how='outer', suffixes=('_ST','_UST') )
ex_data = ex_data.dropna()
data =pd.merge(trades, ex_data, left_on='CURRDATE', right_on='Date', how='outer', suffixes=('_BND','_STK') )
data = data.drop(['Date','CURRDATE'],axis=1)
示例5: datetime
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import reset_index [as 别名]
#print sqltxt
data = psql.read_frame(sqltxt, con)
sqlDropTxt = 'DROP TABLE "main"."{}"'.format(tb)
#print sqlDropTxt
psql.execute(sqlDropTxt, con)
con.commit()
psql.write_frame( data, tb, con)
con.commit()
con.close()
start = datetime(2007,1,1)
end = datetime.date(datetime.now())
data = DataReader("^GSPC", "yahoo", start, end)
data = data.reset_index()
data.Date = [unicode(d) for d in data.Date]
data.Date[0]
data.head()
con = sqlite3.connect("/home/phcostello/Documents/Data/FinanceData.sqlite")
try:
psql.write_frame( data, "SP500", con)
con.commit()
except Exception as e:
print "Problems with {}".format(series[0]), e
con.close()
示例6: dt
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import reset_index [as 别名]
start = dt(2011, 1, 1)
end = dt(2015, 5, 1)
df = DR("1295.KL", 'yahoo', start, end)
dw = DR("^KLSE", 'yahoo', start, end)
df['5D_MA'] = pd.rolling_mean(df['Close'],5) # calculate moving average
dw['5D_MA'] = pd.rolling_mean(dw['Close'],5)
#start plotting
plt.subplot(2,1,1)
plt.plot(df.index,df['5D_MA'],'r',label='Public Bank 5-day MA')
plt.xlabel('Years')
plt.ylabel('5-day MA')
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=4, mode="expand", borderaxespad=0.)
plt.subplot(2,1,2)
plt.plot(dw.index,dw['5D_MA'],'b',label='KLSE 5-day MA')
plt.xlabel('Years')
plt.ylabel('5-day MA')
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=4, mode="expand", borderaxespad=0.)
plt.show()
df = df.reset_index()
dw = dw.reset_index()
mm = pd.merge(df, dw, on='Date', suffixes=['_pbb', '_KLSE']) # merge dataframe
a = np.corrcoef(mm['Close_pbb'],mm['Close_KLSE']) # calculate correlation coefficient
print ('Correlation coefficient=', a)
示例7: DataReader
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import reset_index [as 别名]
import numpy as np
import pandas
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick, candlestick2
import matplotlib.dates as mdates
from pandas.io.data import DataReader
# get daily stock price data from yahoo finance for S&P500
SP = DataReader("^GSPC", "yahoo")
SP.reset_index(inplace=True)
print(SP.columns)
SP['Date2'] = SP['Date'].apply(lambda date: mdates.date2num(date.to_pydatetime()))
fig, ax = plt.subplots()
csticks = candlestick(ax, SP[['Date2', 'Open', 'Close', 'High', 'Low']].values)
plt.show()