def test_fit_transform_cols_none(self): """If no cols are specify, all the columns are in one group. """ t = r.ColumnGroupsTransformer() t._transform_group = MagicMock(return_value=np.array([4, 5, 6])) df = pd.DataFrame({"a": [1, 2, 3], "b": [3, 2, 1]}) df_out = t.fit_transform(df) df_expected = pd.DataFrame({"a": [4, 5, 6]}) self.assertTrue(df_out.equals(df_expected))
def test_inverse_transform(self): """By default inverse copy the output per each input col. """ t = r.ColumnGroupsTransformer() t._transform_group = MagicMock(return_value=np.array([4, 5, 6])) df = pd.DataFrame({"a": [1, 2, 3], "b": [3, 2, 1]}) df_out = t.fit_transform(df) df_out = t.inverse_transform(df_out) df_expected = pd.DataFrame({"a": [4, 5, 6], "b": [4, 5, 6]}) self.assertTrue(df_out.equals(df_expected))
def test_fit_transform_no_groups(self): """If groups=False, all the columns are in one group. """ t = r.ColumnGroupsTransformer(["aa", "bb"]) t._transform_group = MagicMock(return_value=np.array([4, 5, 6])) df = pd.DataFrame({"aa": [1, 2, 3], "bb": [3, 2, 1], "cc": [0, 0, 0]}) df_out = t.fit_transform(df) df_expected = pd.DataFrame({"aa": [4, 5, 6], "cc": [0, 0, 0]}) self.assertTrue(df_out.equals(df_expected)) cols_call = [i[0][1] for i in t._transform_group.call_args_list] expected = [["aa", "bb"]] self.assertListEqual(cols_call, expected)
def test_fit_transform_groups(self): """If no cols are specify, all the columns are in one group. """ t = r.ColumnGroupsTransformer("*1", "*2", groups=True) t._transform_group = MagicMock(return_value=np.array([0, 0, 0])) df = pd.DataFrame({ "a1": [1, 2, 3], "a2": [3, 2, 1], "b1": [4, 5, 6], "b2": [6, 5, 4] }) df_out = t.fit_transform(df) df_expected = pd.DataFrame({"a": [0, 0, 0], "b": [0, 0, 0]}) self.assertTrue(df_out.equals(df_expected)) cols_call = [i[0][1] for i in t._transform_group.call_args_list] expected = [["a1", "a2"], ["b1", "b2"]] self.assertListEqual(cols_call, expected)
def test_init_cols_init_no_groups(self): t = r.ColumnGroupsTransformer("a", cols_init=["b"], groups=False) expected = ["a", "b"] self.assertListEqual(t.col_groups_init, expected) self.assertListEqual(t.cols_init, expected)
def test_get_column_name_no_match(self): """When no numbers in names, the first column is used. """ c = r.ColumnGroupsTransformer()._get_column_name(["Type", "ExtraType"]) self.assertEqual(c, "Type")
def test_get_column_name(self): c = r.ColumnGroupsTransformer()._get_column_name(["Type 1", "Type 2"]) self.assertEqual(c, "Type")