def get_data(): df = data_processor.merge({ index_code: get_index_daily(), stock_code: get_stock_daily() }) df.fillna(method='ffill', inplace=True) df.dropna(inplace=True) return df
def test_merge_doc(): def app(df, **kwargs): name = kwargs.pop('name', 'o') return pd.Series( ['{0}_{1}'.format(name, i) for i in range(df.shape[0])], index=df.index) def drop(df, **kwargs): return df.drop(**kwargs) df = pd.DataFrame({ 'key': ['K0', 'K1', 'K2', 'K3'], 'A': ['A0', 'A1', 'A2', 'A3'], 'B': ['A4', 'A5', 'A6', 'A7'] }).set_index('key') other = pd.DataFrame({ 'key': ['K0', 'K1', 'K2'], 'A': ['B0', 'B1', 'B2'], 'B': ['B3', 'B4', 'B5'] }).set_index('key') print(data_processor.merge({'df': df, '_other': other})) print( data_processor.merge({ 'df': df, '_other': other }, append_funcs={ 'drop_column': (drop, { 'columns': ['B'], 'replace': True }) })) merge_df = data_processor.merge({ 'df': df, '_other': other }, append_funcs={'C': [app, { 'name': 'N' }]}) print(merge_df)
def test_skew(): logging.debug("测试偏度") for i in range(2010, 2018): s = '{0}-01-01'.format(i) e = '{0}-12-31'.format(i) df = data_processor.merge({ test.index_code: get_index_daily().loc[s:e], test.stock_code: get_stock_daily().loc[s:e] }) v = calcs.skew(df['close'].dropna()) print('{0}:{1}'.format(i, v))
def test_is_trade_suspension(): """测试是否停牌的计算""" df = data_processor.merge( { test.stock_code: get_stock_daily(), test.index_code: get_index_daily() }, how='right') is_sus = calcs.is_trade_suspension(df) assert not is_sus['2012-02-22'] assert is_sus['2012-02-23'] assert not is_sus['2012-02-24'] print(is_sus['2012-02-23'])
def test_merge_dropcolumn(): columns = ['up_count', 'down_count'] df = data_processor.merge( { test.index_code: get_index_daily(), test.stock_code: get_stock_daily() }, append_funcs={ 'drop_columns': (calcs.drop_column, { 'columns': columns, 'replace': True }) }) for col in columns: assert col not in df.columns print(df.dtypes)
def test_sharpe_ratio(): rate = get_deposit_rate().sort_index() df_s = get_stock_daily().index[0].date() df_e = get_stock_daily().index[-1].date() for i in range(len(rate.index)): s = str2date(rate.index[i]).date() e = str2date(rate.index[i + 1]).date() if rate[i] != rate[-1] else df_e if s >= df_s and e <= df_e: df = data_processor.merge({ test.index_code: get_index_daily().loc[s:e], test.stock_code: get_stock_daily().loc[s:e] }) k = '{0}~{1}'.format(s, e) v = calcs.sharpe_ratio(df['close'].dropna(), rate[i]) print('{0}:{1}'.format(k, v))
def merged_dataframe() -> dict: return data_processor.merge({ index_code: get_index_daily(), stock_code: get_stock_daily() })