def test_column_cache(df): orca.add_injectable('x', 2) series = pd.Series([1, 2, 3], index=['x', 'y', 'z']) key = ('table', 'col') @orca.table() def table(): return df @orca.column(*key, cache=True) def column(variable='x'): return series * variable def c(): return orca._COLUMNS[key] pdt.assert_series_equal(c()(), series * 2) orca.add_injectable('x', 3) pdt.assert_series_equal(c()(), series * 2) c().clear_cached() pdt.assert_series_equal(c()(), series * 3) orca.add_injectable('x', 4) pdt.assert_series_equal(c()(), series * 3) orca.clear_cache() pdt.assert_series_equal(c()(), series * 4) orca.add_injectable('x', 5) pdt.assert_series_equal(c()(), series * 4) orca.get_table('table').clear_cached() pdt.assert_series_equal(c()(), series * 5) orca.add_injectable('x', 6) pdt.assert_series_equal(c()(), series * 5) orca.add_column(*key, column=column, cache=True) pdt.assert_series_equal(c()(), series * 6)
def test_columns_for_table(): orca.add_column( 'table1', 'col10', pd.Series([1, 2, 3], index=['a', 'b', 'c'])) orca.add_column( 'table2', 'col20', pd.Series([10, 11, 12], index=['x', 'y', 'z'])) @orca.column('table1') def col11(): return pd.Series([4, 5, 6], index=['a', 'b', 'c']) @orca.column('table2', 'col21') def asdf(): return pd.Series([13, 14, 15], index=['x', 'y', 'z']) t1_col_names = orca.list_columns_for_table('table1') assert set(t1_col_names) == {'col10', 'col11'} t2_col_names = orca.list_columns_for_table('table2') assert set(t2_col_names) == {'col20', 'col21'} t1_cols = orca._columns_for_table('table1') assert 'col10' in t1_cols and 'col11' in t1_cols t2_cols = orca._columns_for_table('table2') assert 'col20' in t2_cols and 'col21' in t2_cols
def test_table_func_local_cols(df): @orca.table() def table(): return df orca.add_column( 'table', 'new', pd.Series(['a', 'b', 'c'], index=df.index)) assert orca.get_table('table').local_columns == ['a', 'b']
def test_columns_and_tables(df): orca.add_table('test_frame', df) @orca.table() def test_func(test_frame): return test_frame.to_frame() / 2 orca.add_column('test_frame', 'c', pd.Series([7, 8, 9], index=df.index)) @orca.column('test_func', 'd') def asdf(test_func): return test_func.to_frame(columns=['b'])['b'] * 2 @orca.column('test_func') def e(column='test_func.d'): return column + 1 test_frame = orca.get_table('test_frame') assert set(test_frame.columns) == set(['a', 'b', 'c']) assert_frames_equal( test_frame.to_frame(), pd.DataFrame({ 'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9] }, index=['x', 'y', 'z'])) assert_frames_equal( test_frame.to_frame(columns=['a', 'c']), pd.DataFrame({ 'a': [1, 2, 3], 'c': [7, 8, 9] }, index=['x', 'y', 'z'])) test_func_df = orca._TABLES['test_func'] assert set(test_func_df.columns) == set(['d', 'e']) assert_frames_equal( test_func_df.to_frame(), pd.DataFrame( { 'a': [0.5, 1, 1.5], 'b': [2, 2.5, 3], 'c': [3.5, 4, 4.5], 'd': [4., 5., 6.], 'e': [5., 6., 7.] }, index=['x', 'y', 'z'])) assert_frames_equal( test_func_df.to_frame(columns=['b', 'd']), pd.DataFrame({ 'b': [2, 2.5, 3], 'd': [4., 5., 6.] }, index=['x', 'y', 'z'])) assert set(test_func_df.columns) == set(['a', 'b', 'c', 'd', 'e']) assert set(orca.list_columns()) == {('test_frame', 'c'), ('test_func', 'd'), ('test_func', 'e')}
def test_columns_and_tables(df): orca.add_table('test_frame', df) @orca.table() def test_func(test_frame): return test_frame.to_frame() / 2 orca.add_column('test_frame', 'c', pd.Series([7, 8, 9], index=df.index)) @orca.column('test_func', 'd') def asdf(test_func): return test_func.to_frame(columns=['b'])['b'] * 2 @orca.column('test_func') def e(column='test_func.d'): return column + 1 test_frame = orca.get_table('test_frame') assert set(test_frame.columns) == set(['a', 'b', 'c']) assert_frames_equal( test_frame.to_frame(), pd.DataFrame( {'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]}, index=['x', 'y', 'z'])) assert_frames_equal( test_frame.to_frame(columns=['a', 'c']), pd.DataFrame( {'a': [1, 2, 3], 'c': [7, 8, 9]}, index=['x', 'y', 'z'])) test_func_df = orca._TABLES['test_func'] assert set(test_func_df.columns) == set(['d', 'e']) assert_frames_equal( test_func_df.to_frame(), pd.DataFrame( {'a': [0.5, 1, 1.5], 'b': [2, 2.5, 3], 'c': [3.5, 4, 4.5], 'd': [4., 5., 6.], 'e': [5., 6., 7.]}, index=['x', 'y', 'z'])) assert_frames_equal( test_func_df.to_frame(columns=['b', 'd']), pd.DataFrame( {'b': [2, 2.5, 3], 'd': [4., 5., 6.]}, index=['x', 'y', 'z'])) assert set(test_func_df.columns) == set(['a', 'b', 'c', 'd', 'e']) assert set(orca.list_columns()) == { ('test_frame', 'c'), ('test_func', 'd'), ('test_func', 'e')}
def test_column_type(df): orca.add_table('test_frame', df) @orca.table() def test_func(): return df s = pd.Series(range(len(df)), index=df.index) def col_func(): return s orca.add_column('test_frame', 'col_series', s) orca.add_column('test_func', 'col_series', s) orca.add_column('test_frame', 'col_func', col_func) orca.add_column('test_func', 'col_func', col_func) tframe = orca.get_raw_table('test_frame') tfunc = orca.get_raw_table('test_func') assert tframe.column_type('a') == 'local' assert tframe.column_type('col_series') == 'series' assert tframe.column_type('col_func') == 'function' assert tfunc.column_type('a') == 'local' assert tfunc.column_type('col_series') == 'series' assert tfunc.column_type('col_func') == 'function'
def test_get_raw_column(df): orca.add_table('test_frame', df) s = pd.Series(range(len(df)), index=df.index) def col_func(): return s orca.add_column('test_frame', 'col_series', s) orca.add_column('test_frame', 'col_func', col_func) assert isinstance(orca.get_raw_column('test_frame', 'col_series'), orca._SeriesWrapper) assert isinstance(orca.get_raw_column('test_frame', 'col_func'), orca._ColumnFuncWrapper)
def test_get_raw_column(df): orca.add_table('test_frame', df) s = pd.Series(range(len(df)), index=df.index) def col_func(): return s orca.add_column('test_frame', 'col_series', s) orca.add_column('test_frame', 'col_func', col_func) assert isinstance( orca.get_raw_column('test_frame', 'col_series'), orca._SeriesWrapper) assert isinstance( orca.get_raw_column('test_frame', 'col_func'), orca._ColumnFuncWrapper)
def test_table_copy(df): orca.add_table('test_frame_copied', df, copy_col=True) orca.add_table('test_frame_uncopied', df, copy_col=False) orca.add_table('test_func_copied', lambda: df, copy_col=True) orca.add_table('test_func_uncopied', lambda: df, copy_col=False) @orca.table(copy_col=True) def test_funcd_copied(): return df @orca.table(copy_col=False) def test_funcd_uncopied(): return df @orca.table(copy_col=True) def test_funcd_copied2(test_frame_copied): # local returns original, but it is copied by copy_col. return test_frame_copied.local @orca.table(copy_col=True) def test_funcd_copied3(test_frame_uncopied): # local returns original, but it is copied by copy_col. return test_frame_uncopied.local @orca.table(copy_col=False) def test_funcd_uncopied2(test_frame_copied): # local returns original. return test_frame_copied.local @orca.table(copy_col=False) def test_funcd_uncopied3(test_frame_uncopied): # local returns original. return test_frame_uncopied.local orca.add_table('test_cache_copied', lambda: df, cache=True, copy_col=True) orca.add_table( 'test_cache_uncopied', lambda: df, cache=True, copy_col=False) @orca.table(cache=True, copy_col=True) def test_cached_copied(): return df @orca.table(cache=True, copy_col=False) def test_cached_uncopied(): return df # Create tables with computed columns. orca.add_table( 'test_copied_columns', pd.DataFrame(index=df.index), copy_col=True) orca.add_table( 'test_uncopied_columns', pd.DataFrame(index=df.index), copy_col=False) for column_name in ['a', 'b']: label = "test_frame_uncopied.{}".format(column_name) def func(col=label): return col for table_name in ['test_copied_columns', 'test_uncopied_columns']: orca.add_column(table_name, column_name, func) for name in ['test_frame_uncopied', 'test_func_uncopied', 'test_funcd_uncopied', 'test_funcd_uncopied2', 'test_funcd_uncopied3', 'test_cache_uncopied', 'test_cached_uncopied', 'test_uncopied_columns', 'test_frame_copied', 'test_func_copied', 'test_funcd_copied', 'test_funcd_copied2', 'test_funcd_copied3', 'test_cache_copied', 'test_cached_copied', 'test_copied_columns']: table = orca.get_table(name) table2 = orca.get_table(name) # to_frame will always return a copy. if 'columns' in name: assert_frames_equal(table.to_frame(), df) else: pdt.assert_frame_equal(table.to_frame(), df) assert table.to_frame() is not df pdt.assert_frame_equal(table.to_frame(), table.to_frame()) assert table.to_frame() is not table.to_frame() pdt.assert_series_equal(table.to_frame()['a'], df['a']) assert table.to_frame()['a'] is not df['a'] pdt.assert_series_equal(table.to_frame()['a'], table.to_frame()['a']) assert table.to_frame()['a'] is not table.to_frame()['a'] if 'uncopied' in name: pdt.assert_series_equal(table['a'], df['a']) assert table['a'] is df['a'] pdt.assert_series_equal(table['a'], table2['a']) assert table['a'] is table2['a'] else: pdt.assert_series_equal(table['a'], df['a']) assert table['a'] is not df['a'] pdt.assert_series_equal(table['a'], table2['a']) assert table['a'] is not table2['a']