Esempio n. 1
0
    def test_super_transform_call(self, mocker):
        """Test the call to BaseMappingTransformer.transform."""

        df = d.create_df_1()

        mapping = {
            "b": {
                "a": 1.1,
                "b": 1.2,
                "c": 1.3,
                "d": 1.4,
                "e": 1.5,
                "f": 1.6
            }
        }

        x = CrossColumnAddTransformer(mappings=mapping, adjust_column="a")

        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)
Esempio n. 2
0
    def test_mappings_unchanged(self):
        """Test that mappings is unchanged in transform."""

        df = d.create_df_1()

        mapping = {
            "b": {
                "a": 1.1,
                "b": 1.2,
                "c": 1.3,
                "d": 1.4,
                "e": 1.5,
                "f": 1.6
            }
        }

        x = CrossColumnAddTransformer(mappings=mapping, adjust_column="a")

        x.transform(df)

        h.assert_equal_dispatch(
            expected=mapping,
            actual=x.mappings,
            msg=
            "CrossColumnAddTransformer.transform has changed self.mappings unexpectedly",
        )
Esempio n. 3
0
    def test_multiple_mappings_expected_output(self, df, expected):
        """Test that mappings by multiple columns are both applied in transform"""

        mapping = {"b": {"a": 1.1, "f": 1.2}, "c": {"a": 2, "e": 3}}

        x = CrossColumnAddTransformer(mappings=mapping, adjust_column="a")

        df_transformed = x.transform(df)

        h.assert_frame_equal_msg(
            actual=df_transformed,
            expected=expected,
            msg_tag="expected output from cross column add transformer",
        )
Esempio n. 4
0
    def test_non_specified_values_unchanged(self, df, expected):
        """Test that values not specified in mappings are left unchanged in transform."""

        mapping = {"b": {"a": 1.1, "b": 1.2}}

        x = CrossColumnAddTransformer(mappings=mapping, adjust_column="a")

        df_transformed = x.transform(df)

        h.assert_frame_equal_msg(
            actual=df_transformed,
            expected=expected,
            msg_tag="expected output from cross column add transformer",
        )
Esempio n. 5
0
    def test_super_init_called(self, mocker):
        """Test that init calls BaseMappingTransformer.init."""

        expected_call_args = {
            0: {
                "args": (),
                "kwargs": {
                    "mappings": {
                        "a": {
                            "a": 1
                        }
                    },
                    "verbose": True,
                    "copy": True,
                },
            }
        }

        with h.assert_function_call(
                mocker,
                tubular.mapping.BaseMappingTransformer,
                "__init__",
                expected_call_args,
        ):

            CrossColumnAddTransformer(mappings={"a": {
                "a": 1
            }},
                                      adjust_column="b",
                                      verbose=True,
                                      copy=True)
Esempio n. 6
0
    def test_mapping_values_not_numeric_error(self):
        """Test that an exception is raised if mappings values are not numeric."""

        with pytest.raises(TypeError, match="mapping values must be numeric"):

            CrossColumnAddTransformer(mappings={"a": {
                "a": "b"
            }},
                                      adjust_column="b")
Esempio n. 7
0
    def test_inheritance(self):
        """Test that CrossColumnAddTransformer inherits from BaseMappingTransformer."""

        x = CrossColumnAddTransformer(mappings={"a": {
            "a": 1
        }},
                                      adjust_column="b")

        h.assert_inheritance(x, tubular.mapping.BaseMappingTransformer)
Esempio n. 8
0
    def test_adjust_columns_non_string_error(self):
        """Test that an exception is raised if adjust_column is not a string."""

        with pytest.raises(TypeError,
                           match="adjust_column should be a string"):

            CrossColumnAddTransformer(mappings={"a": {
                "a": 1
            }},
                                      adjust_column=1)
Esempio n. 9
0
    def test_class_methods(self):
        """Test that CrossColumnAddTransformer has transform method."""

        x = CrossColumnAddTransformer(mappings={"a": {
            "a": 1
        }},
                                      adjust_column="b")

        h.test_object_method(obj=x,
                             expected_method="transform",
                             msg="transform")
Esempio n. 10
0
    def test_adjust_col_not_in_x_error(self):
        """Test that an exception is raised if the adjust_column is not present in the dataframe."""

        df = d.create_df_1()

        mapping = {
            "b": {
                "a": 1.1,
                "b": 1.2,
                "c": 1.3,
                "d": 1.4,
                "e": 1.5,
                "f": 1.6
            }
        }

        x = CrossColumnAddTransformer(mappings=mapping, adjust_column="c")

        with pytest.raises(ValueError, match="variable c is not in X"):

            x.transform(df)
Esempio n. 11
0
    def test_adjust_col_not_numeric_error(self):
        """Test that an exception is raised if the adjust_column is not numeric."""

        df = d.create_df_2()

        mapping = {
            "b": {
                "a": 1.1,
                "b": 1.2,
                "c": 1.3,
                "d": 1.4,
                "e": 1.5,
                "f": 1.6
            }
        }

        x = CrossColumnAddTransformer(mappings=mapping, adjust_column="c")

        with pytest.raises(TypeError,
                           match="variable c must have numeric dtype."):

            x.transform(df)
Esempio n. 12
0
    def test_expected_output(self, df, expected):
        """Test that transform is giving the expected output."""

        mapping = {
            "b": {
                "a": 1.1,
                "b": 1.2,
                "c": 1.3,
                "d": 1.4,
                "e": 1.5,
                "f": 1.6
            }
        }

        x = CrossColumnAddTransformer(mappings=mapping, adjust_column="a")

        df_transformed = x.transform(df)

        h.assert_frame_equal_msg(
            actual=df_transformed,
            expected=expected,
            msg_tag="expected output from cross column add transformer",
        )
Esempio n. 13
0
    def test_adjust_column_set_to_attribute(self):
        """Test that the value passed for adjust_column is saved in an attribute of the same name."""

        value = "b"

        x = CrossColumnAddTransformer(mappings={"a": {
            "a": 1
        }},
                                      adjust_column=value)

        h.test_object_attributes(
            obj=x,
            expected_attributes={"adjust_column": value},
            msg="Attributes for CrossColumnAddTransformer set in init",
        )
Esempio n. 14
0
    def test_check_is_fitted_call(self, mocker):
        """Test the call to check_is_fitted."""

        df = d.create_df_1()

        mapping = {
            "b": {
                "a": 1.1,
                "b": 1.2,
                "c": 1.3,
                "d": 1.4,
                "e": 1.5,
                "f": 1.6
            }
        }

        x = CrossColumnAddTransformer(mappings=mapping, adjust_column="a")

        expected_call_args = {0: {"args": (["adjust_column"], ), "kwargs": {}}}

        with h.assert_function_call(mocker, tubular.base.BaseTransformer,
                                    "check_is_fitted", expected_call_args):

            x.transform(df)