Beispiel #1
0
 def test_preserve_axis_names(self):
     r1 = totals.add(self.df)
     r2 = totals.add(self.df, axis=1)
     r3 = totals.add(self.df, level=1)
     r4 = totals.add(self.df, axis=1, level=1)
     self.assertEqual(self.df.index.names, r1.index.names)
     self.assertEqual(self.df.index.names, r2.index.names)
     self.assertEqual(self.df.index.names, r3.index.names)
     self.assertEqual(self.df.index.names, r4.index.names)
Beispiel #2
0
 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)
Beispiel #3
0
 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)
Beispiel #4
0
    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))
Beispiel #5
0
def totals(df: pd.DataFrame, level: Any = 0, **kwargs) -> pd.DataFrame:
    "Add totals to rows of ``df`` on ``level``."
    return totals.add(df, level=level, **kwargs)
Beispiel #6
0
 def test_add_both(self):
     value = totals.add(self.df, axis=2).iloc[-1, -1]
     self.assertTrue(value == self.df.sum().sum())
Beispiel #7
0
 def test_add_row_total_to_cols(self):
     s = totals.add(self.df, axis=1).iloc[:, -1]
     self.assertTrue(s.equals(self.df.sum(axis=1)))
Beispiel #8
0
 def test_add_column_total_to_rows(self):
     s = totals.add(self.df, axis=0).iloc[-1]
     self.assertTrue(s.equals(self.df.sum()))
Beispiel #9
0
 def test_add_both(self):
     v = totals.add(self.df, axis=2).loc[self.totals_name, self.totals_name]
     self.assertTrue(v == self.df.sum().sum())
Beispiel #10
0
 def test_add_both(self):
     rows, cols = self.totals_name, self.totals_name
     val = totals.add(self.df, axis=2).loc[rows, cols]
     self.assertTrue(val == self.df.sum().sum())
Beispiel #11
0
 def test_add_by_axis_name(self):
     s = totals.add(self.df, axis='columns').loc[:, self.totals_name]
     self.assertTrue(s.equals(self.df.sum(axis=1)))