def sample_415(): """ 4.1.5 重采样数据 :return """ days = pd.date_range('2017-1-1', periods=stock_day_change.shape[1], freq='1d') stock_symbols = ['股票 ' + str(x) for x in xrange(stock_day_change.shape[0])] df = pd.DataFrame(stock_day_change, index=stock_symbols, columns=days) df = df.T df_stock0 = df['股票 0'] # 以5天为周期重采样(周k) df_stock0_5 = pd_resample(df_stock0.cumsum(), '5D', how='ohlc') # 以21天为周期重采样(月k), # noinspection PyUnusedLocal df_stock0_20 = pd_resample(df_stock0.cumsum(), '21D', how='ohlc') # 打印5天重采样,如下输出2017-01-01, 2017-01-06, 2017-01-11, 表4-6所示 print('df_stock0_5.head():\n', df_stock0_5.head()) from abupy import ABuMarketDrawing # 图4-2所示 ABuMarketDrawing.plot_candle_stick(df_stock0_5.index, df_stock0_5['open'].values, df_stock0_5['high'].values, df_stock0_5['low'].values, df_stock0_5['close'].values, np.random.random(len(df_stock0_5)), None, 'stock', day_sum=False, html_bk=False, save=False) print('type(df_stock0_5.open.values):', type(df_stock0_5['open'].values)) print('df_stock0_5.open.index:\n', df_stock0_5['open'].index) print('df_stock0_5.columns:\n', df_stock0_5.columns)
def sample_413(): """ 4.1.3 金融时间序列 :return: """ days = pd.date_range('2017-1-1', periods=stock_day_change.shape[1], freq='1d') stock_symbols = ['股票 ' + str(x) for x in xrange(stock_day_change.shape[0])] df = pd.DataFrame(stock_day_change, index=stock_symbols, columns=days) # df做个转置 df = df.T # 表4-4所示 print('df.head():\n', df.head()) df_20 = pd_resample(df, '21D', how='mean') # 表4-5所示 print('df_20.head():\n', df_20.head())