def setUp(self): self.totals_name = config.get_value('aggregation', 'totals_name') self.label_abs = config.get_value('aggregation', 'label_abs') self.ndigits = config.get_value('aggregation', 'ndigits') self.df = pd._testing.makeCustomDataframe( nrows=5, ncols=4, data_gen_f=lambda r, c: randint(1, 100), )
def setUp(self): self.na_rep = config.get_value('aggregation', 'na_rep') categories = { key:[self.na_rep if val is None else val for val in values] for key, values in test.CATEGORIES.items() } self.taste = [i for i in test.CATEGORIES['taste'] if i is not None] self.touch = [i for i in test.CATEGORIES['touch'] if i is not None] kwargs = dict( columns = 'taste', index = ['smell', 'touch'], aggfunc = 'size' ) self.df = test.dataset( seed='squid', categories=categories ).pivot_table(**kwargs) self.df_cats = test.dataset( categories=categories, to_categories=True, ).pivot_table(**kwargs)
def test_add_rows_within(self): left = (totals.add(self.df, level=-1).xs(config.get_value( 'aggregation', 'subtotals_name'), level=-1).values) right = self.df.groupby(level=-2).sum().values comparison = (left == right).all() self.assertTrue(comparison)
def test_add_cols_within(self): subtotals_name = config.get_value('aggregation', 'subtotals_name') left = (totals.add(self.df, axis=1, level=1).xs(subtotals_name, axis=1, level=1).values) right = self.df.groupby(level=0, axis=1).sum().values comparison = (left == right).all() self.assertTrue(comparison)
def test_add_multiple_levels(self): subtotals_name = config.get_value('aggregation', 'subtotals_name') result = totals.add(self.df, axis=1, level=[0, 'C1']) subtotals_result = result.xs(subtotals_name, axis=1, level=1) calc_subtotals = self.df.groupby(level=0, axis=1).sum() self.assertTrue(subtotals_result.eq(calc_subtotals).all().all()) totals_result = result.iloc[:, -1] calc_totals = self.df.sum(axis=1) self.assertTrue(totals_result.equals(calc_totals))
def setUp(self): self.totals_name = config.get_value('aggregation', 'totals_name') df = pd._testing.makeDataFrame().head(5) df.columns = pd.Categorical(df.columns) df.index = pd.Categorical(df.index) self.df = df