本文整理汇总了Python中pandas.io.data.DataReader.reindex方法的典型用法代码示例。如果您正苦于以下问题:Python DataReader.reindex方法的具体用法?Python DataReader.reindex怎么用?Python DataReader.reindex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.io.data.DataReader
的用法示例。
在下文中一共展示了DataReader.reindex方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: float
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import reindex [as 别名]
from pandas.io.data import DataReader
from datetime import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
randn = np.random.randn
f = lambda x: float(x)
SPX = DataReader("SP500", "fred", datetime(2013,1,1), datetime(2013,05,05) ) #SPX
SPX=SPX['SP500'].replace('.',np.nan).fillna(method='ffill')
SPX=pd.DataFrame(SPX)
SPX=SPX.applymap(f)
#dr = pd.date_range('1/1/2013', periods=20, freq=3 * pd.datetools.bday)
start = datetime(2013, 1, 1)
end = datetime(2013, 5, 11)
#startrng = pd.bdate_range(start,end,freq='BQS-MAR')
#startrng = pd.bdate_range(start,end,freq='BMS') #first business day of month
startrng = pd.bdate_range(start,end,freq='WOM-2TUE') #second Tuesday of month
#startrng = startrng + pd.datetools.WeekOfMonth(week=1,weekday=4) - pd.datetools.BDay()
print startrng
SPX=SPX.reindex(startrng)
print SPX
示例2: float
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import reindex [as 别名]
LI3=pd.date_range(start, end, freq='D')
#functions--------------------
f = lambda x: float(x)
df = DataReader('SP500', "fred", start,end)
df=df.applymap(f)
#calculate average per year
annual_df = df.resample('M', how='mean')
print annual_df.to_string()
#calculate return index
df['ret'] = df['SP500'].pct_change()
df=df.reindex(columns=['SP500','ret'])
df['ri'] = (1 + df['ret']).cumprod()
# calculate monthly returns
m_returns = df['ri'].resample('BM', how='last').pct_change()
print m_returns
# turn business day into full days
df=df.resample('D')
# print df.to_string()
#plot DEC 2012 closes
plt.plot(df['SP500'].ix['2012-12'])
plt.show()