Exemplo n.º 1
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 = CrossColumnMultiplyTransformer(mappings=mapping, adjust_column="c")

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

            x.transform(df)
Exemplo n.º 2
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 = CrossColumnMultiplyTransformer(mappings=mapping, adjust_column="c")

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

            x.transform(df)
Exemplo 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 = CrossColumnMultiplyTransformer(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 multiply transformer",
        )
Exemplo 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 = CrossColumnMultiplyTransformer(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 multiply transformer",
        )
Exemplo n.º 5
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 = CrossColumnMultiplyTransformer(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 multiply transformer",
        )
Exemplo n.º 6
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 = CrossColumnMultiplyTransformer(mappings=mapping, adjust_column="a")

        x.transform(df)

        h.assert_equal_dispatch(
            expected=mapping,
            actual=x.mappings,
            msg="CrossColumnMultiplyTransformer.transform has changed self.mappings unexpectedly",
        )
Exemplo n.º 7
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 = CrossColumnMultiplyTransformer(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)
Exemplo n.º 8
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"):

            CrossColumnMultiplyTransformer(
                mappings={"a": {"a": "b"}}, adjust_column="b"
            )
Exemplo n.º 9
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 = CrossColumnMultiplyTransformer(mappings=mapping, adjust_column="a")

        expected_call_args = {0: {"args": (d.create_df_1(),), "kwargs": {}}}

        with h.assert_function_call(
            mocker,
            tubular.mapping.BaseMappingTransformer,
            "transform",
            expected_call_args,
            return_value=d.create_df_1(),
        ):

            x.transform(df)
Exemplo n.º 10
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 = CrossColumnMultiplyTransformer(
            mappings={"a": {"a": 1}}, adjust_column=value
        )

        h.test_object_attributes(
            obj=x,
            expected_attributes={"adjust_column": value},
            msg="Attributes for CrossColumnMultiplyTransformer set in init",
        )
Exemplo n.º 11
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,
        ):

            CrossColumnMultiplyTransformer(
                mappings={"a": {"a": 1}}, adjust_column="b", verbose=True, copy=True
            )
Exemplo n.º 12
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"):

            CrossColumnMultiplyTransformer(mappings={"a": {"a": 1}}, adjust_column=1)
Exemplo n.º 13
0
    def test_inheritance(self):
        """Test that CrossColumnMultiplyTransformer inherits from BaseMappingTransformer."""

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

        h.assert_inheritance(x, tubular.mapping.BaseMappingTransformer)
Exemplo n.º 14
0
    def test_class_methods(self):
        """Test that CrossColumnMultiplyTransformer has transform method."""

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

        h.test_object_method(obj=x, expected_method="transform", msg="transform")