def test_check_is_fitted_called(self, mocker): """Test that BaseTransformer check_is_fitted called.""" df = d.create_df_1() x = NominalToIntegerTransformer(columns=["a", "b"]) x.fit(df) expected_call_args = {0: {"args": (["mappings"], ), "kwargs": {}}} with h.assert_function_call(mocker, tubular.base.BaseTransformer, "check_is_fitted", expected_call_args): x.transform(df)
def test_expected_output(self, df, expected): """Test that transform then inverse_transform gets back to the original df.""" x = NominalToIntegerTransformer(columns=["a", "b"]) # set the mapping dict directly rather than fitting x on df so test works with helpers x.mappings = { "a": { 1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5 }, "b": { "a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5 }, } df_transformed = x.transform(df) df_transformed_back = x.inverse_transform(df_transformed) h.assert_frame_equal_msg( actual=df_transformed_back, expected=expected, msg_tag="transform reverse does not get back to original", )
def test_expected_output(self, df, expected): """Test that the output is expected from transform.""" x = NominalToIntegerTransformer(columns=["a", "b"]) # set the mapping dict directly rather than fitting x on df so test works with helpers x.mappings = { "a": { 1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5 }, "b": { "a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5 }, } df_transformed = x.transform(df) h.assert_frame_equal_msg( actual=df_transformed, expected=expected, msg_tag= "Unexpected values in NominalToIntegerTransformer.transform", )
def test_non_mappable_rows_raises_error(self): """Test that rows that cannot be mapped result in an exception.""" df = d.create_df_1() x = NominalToIntegerTransformer(columns=["a", "b"]) x.fit(df) df["a"] = df["a"] + 1 with pytest.raises( ValueError, match= "nulls would be introduced into column a from levels not present in mapping", ): x.transform(df)
def test_super_transform_called(self, mocker): """Test that BaseTransformer.transform called.""" df = d.create_df_1() x = NominalToIntegerTransformer(columns="a") x.fit(df) expected_call_args = {0: {"args": (d.create_df_1(), ), "kwargs": {}}} with h.assert_function_call( mocker, tubular.base.BaseTransformer, "transform", expected_call_args, return_value=d.create_df_1(), ): x.transform(df)
def test_non_mappable_rows_raises_error(self): """Test that rows that cannot be mapped result in an exception.""" x = NominalToIntegerTransformer(columns=["a", "b"]) df = d.create_df_1() x.fit(df) df_transformed = x.transform(df) df_transformed["b"] = df_transformed["b"] + 1 with pytest.raises( ValueError, match= "nulls introduced from levels not present in mapping for column: b", ): x.inverse_transform(df_transformed)
def test_learnt_values_not_modified(self): """Test that the mappings from fit are not changed in inverse_transform.""" df = d.create_df_1() x = NominalToIntegerTransformer(columns=["a", "b"]) x.fit(df) x2 = NominalToIntegerTransformer(columns=["a", "b"]) x2.fit(df) df_transformed = x2.transform(df) x2.inverse_transform(df_transformed) h.assert_equal_dispatch( expected=x.mappings, actual=x2.mappings, msg="Impute values not changed in inverse_transform", )