Пример #1
0
    def test__no_filt_or_useless_filt__no_change(self, runfilter_test_df):

        # BEFORE: Copy dataframe
        df_copy1 = runfilter_test_df.copy()

        # Filter the copy of the dataframe with no filter
        result_indices, filt_map = df_util.run_filters(df_copy1, {})

        # AFTER:
        #   Confirm filt_map correct
        assert len(filt_map) == 0

        #   Confirm dataframe is unchanged
        assert df_copy1.equals(runfilter_test_df)

        ######

        # BEFORE: Copy dataframes
        df_copy2 = runfilter_test_df.copy()

        # Filter the copy of the dataframe with useless filter
        func_name = "useless"
        result_indices, filt_map = df_util.run_filters(
            df_copy1, {func_name: lambda df: (df == -1).any(axis='columns')})

        # AFTER:
        #   Confirm filt_map correct
        assert len(filt_map) == 1
        assert func_name in filt_map

        #   Confirm dataframe is unchanged
        assert df_copy2.equals(runfilter_test_df)
        assert not any(filt_map[func_name])
        assert not any(result_indices)
Пример #2
0
    def test__two_filt_no_overlap__return_expected_results(
            self, runfilter_test_df):

        # Identify value whose row to filter out and the expected filtering indices
        target_int1 = 0
        target_int2 = runfilter_test_df.size - 1
        expected_indices1 = (runfilter_test_df == target_int1).any(
            axis='columns')
        expected_indices2 = (runfilter_test_df == target_int2).any(
            axis='columns')

        # BEFORE: Confirm dataframe contains the target int once
        assert expected_indices1.sum() == 1
        assert expected_indices2.sum() == 1

        # Filter the dataframe
        func_name1 = "Hello"
        func_name2 = "World"
        result_indices, filt_map = df_util.run_filters(
            runfilter_test_df, {
                func_name1: lambda df: (df == target_int1).any(axis='columns'),
                func_name2: lambda df: (df == target_int2).any(axis='columns')
            })

        # AFTER:
        #   Confirm filt_map correct
        assert len(filt_map) == 2
        assert func_name1 in filt_map
        assert func_name2 in filt_map

        #   Confirm result indices correct
        assert all(result_indices == filt_map[func_name1]
                   | filt_map[func_name2])
        assert result_indices.sum() == 2
Пример #3
0
    def test__one_filt_duplicated__same_as_just_once(self, runfilter_test_df):

        # Identify value whose row to filter out and the expected filtering indices
        target_int = runfilter_test_df.size // 2
        expected_indices = (runfilter_test_df == target_int).any(
            axis='columns')

        # BEFORE: Confirm dataframe contains the target int once
        assert expected_indices.sum() == 1

        # Filter the dataframe
        func_name1 = "Hello, world"
        func_name2 = "Goodbye, everyone"
        result_indices, filt_map = df_util.run_filters(
            runfilter_test_df, {
                func_name1: lambda df: (df == target_int).any(axis='columns'),
                func_name2: lambda df: (df == target_int).any(axis='columns')
            })

        # AFTER:
        #   Confirm filt_map correct
        assert len(filt_map) == 2
        assert func_name1 in filt_map
        assert func_name2 in filt_map

        #   Confirm result indices correct
        assert all(result_indices == filt_map[func_name1])
        assert all(result_indices == filt_map[func_name2])
        assert all(result_indices == expected_indices)
Пример #4
0
    def test__failed_req_filt__throws_error(self, runfilter_test_df):

        # Identify value whose row to filter out and the expected filtering indices
        target_int = runfilter_test_df.size // 2
        expected_indices = (runfilter_test_df == target_int).any(
            axis='columns')

        # BEFORE: Confirm dataframe contains the target int once and has the correct number of rows
        assert expected_indices.sum() == 1

        # Filter the dataframe
        func_name = "Hello, world"
        with pytest.raises(ZeroDivisionError):
            result_indices, filt_map = df_util.run_filters(
                runfilter_test_df, {func_name: lambda df: 1 / 0})
Пример #5
0
    def test__empty_dataframe__no_change(self):
        # Fill DF with numbers 0..N-1 where there are N cells
        df = pd.DataFrame()

        # BEFORE: Confirm dataframe copy contains the correct number of rows
        assert len(df) == 0

        # Filter the dataframe with useless filter
        func_name = "useless"
        result_indices, filt_map = df_util.run_filters(
            df, {func_name: lambda df: (df == -1).any(axis='columns')})

        # AFTER:
        #   Confirm filt_map correct
        assert len(filt_map) == 1
        assert func_name in filt_map

        #   Confirm dataframe is unchanged
        assert len(df) == 0
        assert not any(filt_map[func_name])
        assert not any(result_indices)
Пример #6
0
    def test__single_filt__return_expected_results(self, runfilter_test_df):

        # Identify value whose row to filter out and the expected filtering indices
        target_int = runfilter_test_df.size // 2
        expected_indices = (runfilter_test_df == target_int).any(
            axis='columns')

        # BEFORE: Confirm dataframe contains the target int once and has the correct number of rows
        assert expected_indices.sum() == 1

        # Filter the dataframe
        func_name = "Hello, world"
        result_indices, filt_map = df_util.run_filters(
            runfilter_test_df,
            {func_name: lambda df: (df == target_int).any(axis='columns')})

        # AFTER:
        #   Confirm filt_map correct
        assert len(filt_map) == 1
        assert func_name in filt_map

        #   Confirm result indices correct
        assert all(result_indices == filt_map[func_name])
        assert all(result_indices == expected_indices)