|
| 1 | +""" |
| 2 | +This file is part of CLIMADA. |
| 3 | +
|
| 4 | +Copyright (C) 2017 ETH Zurich, CLIMADA contributors listed in AUTHORS. |
| 5 | +
|
| 6 | +CLIMADA is free software: you can redistribute it and/or modify it under the |
| 7 | +terms of the GNU General Public License as published by the Free |
| 8 | +Software Foundation, version 3. |
| 9 | +
|
| 10 | +CLIMADA is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 | +PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License along |
| 15 | +with CLIMADA. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +
|
| 17 | +--- |
| 18 | +
|
| 19 | +Test coordinates module. |
| 20 | +""" |
| 21 | + |
| 22 | +import pandas as pd |
| 23 | + |
| 24 | +from climada.util.dataframe_handling import reorder_dataframe_columns |
| 25 | + |
| 26 | + |
| 27 | +def test_reorder_dataframe_columns(): |
| 28 | + # Setup: Create a sample DataFrame |
| 29 | + df = pd.DataFrame( |
| 30 | + {"A": [1, 2], "B": [3, 4], "C": [5, 6], "D": [7, 8], "E": [9, 10]} |
| 31 | + ) |
| 32 | + |
| 33 | + # Test Case 1: Standard reordering with keep_remaining=True |
| 34 | + # Priority: C, A (should move C and A to front, keep B, D, E in original order) |
| 35 | + priority = ["C", "A", "Z"] # 'Z' is not in df, should be ignored |
| 36 | + result = reorder_dataframe_columns(df, priority, keep_remaining=True) |
| 37 | + |
| 38 | + expected_cols = ["C", "A", "B", "D", "E"] |
| 39 | + assert ( |
| 40 | + list(result.columns) == expected_cols |
| 41 | + ), f"Expected {expected_cols}, got {list(result.columns)}" |
| 42 | + |
| 43 | + # Test Case 2: Dropping remaining columns (keep_remaining=False) |
| 44 | + # Priority: B, D (should only keep B and D) |
| 45 | + priority = ["B", "D"] |
| 46 | + result = reorder_dataframe_columns(df, priority, keep_remaining=False) |
| 47 | + |
| 48 | + expected_cols = ["B", "D"] |
| 49 | + assert ( |
| 50 | + list(result.columns) == expected_cols |
| 51 | + ), f"Expected {expected_cols}, got {list(result.columns)}" |
| 52 | + |
| 53 | + # Test Case 3: All priority columns missing |
| 54 | + priority = ["X", "Y"] |
| 55 | + result = reorder_dataframe_columns(df, priority, keep_remaining=True) |
| 56 | + |
| 57 | + # Should return original order since no priority matches |
| 58 | + expected_cols = ["A", "B", "C", "D", "E"] |
| 59 | + assert list(result.columns) == expected_cols |
| 60 | + |
| 61 | + # Test Case 4: Empty priority list |
| 62 | + priority = [] |
| 63 | + result = reorder_dataframe_columns(df, priority, keep_remaining=True) |
| 64 | + |
| 65 | + expected_cols = ["A", "B", "C", "D", "E"] |
| 66 | + assert list(result.columns) == expected_cols |
0 commit comments