コード例 #1
0
    def test_fit_not_changing_data(self):
        """Test fit does not change X."""

        df = d.create_df_1()

        x = NominalToIntegerTransformer(columns=["a", "b"])

        x.fit(df)

        h.assert_equal_dispatch(
            expected=d.create_df_1(),
            actual=df,
            msg="Check X not changing during fit",
        )
コード例 #2
0
    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)
コード例 #3
0
    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)
コード例 #4
0
    def test_learnt_values_not_modified(self):
        """Test that the mappings from fit are not changed in transform."""

        df = d.create_df_1()

        x = NominalToIntegerTransformer(columns=["a", "b"])

        x.fit(df)

        x2 = NominalToIntegerTransformer(columns=["a", "b"])

        x2.fit_transform(df)

        h.assert_equal_dispatch(
            expected=x.mappings,
            actual=x2.mappings,
            msg="Impute values not changed in transform",
        )
コード例 #5
0
    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)
コード例 #6
0
    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)
コード例 #7
0
    def test_fit_returns_self(self):
        """Test fit returns self?"""

        df = d.create_df_1()

        x = NominalToIntegerTransformer(columns=["a", "b"])

        x_fitted = x.fit(df)

        assert (
            x_fitted is x
        ), "Returned value from NominalToIntegerTransformer.fit not as expected."
コード例 #8
0
    def test_learnt_values(self):
        """Test that the impute values learnt during fit are expected."""

        df = d.create_df_1()

        x = NominalToIntegerTransformer(columns=["a", "b"], start_encoding=1)

        x.fit(df)

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "mappings": {
                    "a": {k: i
                          for i, k in enumerate(df["a"].unique(), 1)},
                    "b": {k: i
                          for i, k in enumerate(df["b"].unique(), 1)},
                }
            },
            msg="mappings attribute",
        )
コード例 #9
0
    def test_super_fit_called(self, mocker):
        """Test that fit calls BaseTransformer.fit."""

        df = d.create_df_1()

        x = NominalToIntegerTransformer(columns=["a", "b"])

        spy = mocker.spy(tubular.base.BaseTransformer, "fit")

        x.fit(df)

        assert spy.call_count == 1, "unexpected number of calls to BaseTransformer.fit"

        call_args = spy.call_args_list[0]
        call_pos_args = call_args[0]
        call_kwargs = call_args[1]

        expected_kwargs = {}

        assert (call_kwargs == expected_kwargs
                ), "unexpected kwargs in BaseTransformer.fit call"

        expected_pos_args = (x, d.create_df_1(), None)

        assert len(expected_pos_args) == len(
            call_pos_args
        ), "unexpected # positional args in BaseTransformer.fit call"

        assert (expected_pos_args[0] == call_pos_args[0]
                ), "unexpected 1st positional arg in BaseTransformer.fit call"

        h.assert_equal_dispatch(
            expected_pos_args[1:3],
            call_pos_args[1:3],
            "unexpected 2nd, 3rd positional arg in BaseTransformer.fit call",
        )