|
4 | 4 |
|
5 | 5 | from feature_engine.variable_handling.retain_variables import retain_variables_if_in_df |
6 | 6 |
|
7 | | - |
8 | | -def make_empty_df(is_pandas: bool, columns): |
9 | | - if is_pandas: |
10 | | - return pd.DataFrame(columns=columns) |
11 | | - return pl.DataFrame(schema=columns) |
12 | | - |
13 | | - |
14 | 7 | test_dict = [ |
15 | 8 | (["A", "C", "B", "G", "H"], ["A", "C", "B"], ["X", "Y"]), |
16 | 9 | ("C", ["C"], "G"), |
17 | 10 | ] |
18 | 11 |
|
19 | 12 |
|
20 | | -@pytest.mark.parametrize("is_pandas", [True, False]) |
| 13 | +@pytest.mark.parametrize("make_df", [pd.DataFrame, pl.DataFrame]) |
21 | 14 | @pytest.mark.parametrize("variables, overlap, col_not_in_df", test_dict) |
22 | | -def test_retain_variables_if_in_df(is_pandas, variables, overlap, col_not_in_df): |
23 | | - df = make_empty_df(is_pandas, ["A", "B", "C", "D", "E"]) |
| 15 | +def test_retain_variables_if_in_df(make_df, variables, overlap, col_not_in_df): |
| 16 | + df = make_df({"A": [1], "B": [1], "C": [1], "D": [1], "E": [1]}) |
24 | 17 |
|
25 | 18 | msg = "None of the variables in the list are present in the dataframe." |
26 | 19 |
|
27 | 20 | assert retain_variables_if_in_df(df, variables) == overlap |
28 | 21 |
|
29 | | - with pytest.raises(ValueError) as record: |
| 22 | + with pytest.raises(ValueError, match=msg): |
30 | 23 | retain_variables_if_in_df(df, col_not_in_df) |
31 | | - assert str(record.value) == msg |
32 | 24 |
|
33 | 25 |
|
34 | 26 | def test_retain_variables_if_in_df_int_column_names(): |
35 | | - # polars requires string column names, so int-named columns are pandas-only |
36 | | - df = pd.DataFrame(columns=[1, 2, 3, 4, 5]) |
| 27 | + # polars requires string column names. int-named columns are pandas-only |
| 28 | + df = pd.DataFrame({1: [1], 2: [1], 3: [1], 4: [1], 5: [1]}) |
37 | 29 |
|
38 | 30 | msg = "None of the variables in the list are present in the dataframe." |
39 | 31 |
|
40 | 32 | assert retain_variables_if_in_df(df, [1, 2, 4, 6]) == [1, 2, 4] |
41 | 33 | assert retain_variables_if_in_df(df, 1) == [1] |
42 | 34 |
|
43 | | - with pytest.raises(ValueError) as record: |
| 35 | + with pytest.raises(ValueError, match=msg): |
44 | 36 | retain_variables_if_in_df(df, [6, 7]) |
45 | | - assert str(record.value) == msg |
46 | 37 |
|
47 | | - with pytest.raises(ValueError) as record: |
| 38 | + with pytest.raises(ValueError, match=msg): |
48 | 39 | retain_variables_if_in_df(df, 7) |
49 | | - assert str(record.value) == msg |
0 commit comments