From 59a2936408a4b1dff8bef85478284757adf5e2b5 Mon Sep 17 00:00:00 2001 From: "samuel.oranyeli" Date: Tue, 9 Jan 2024 18:56:59 +1100 Subject: [PATCH] ensure get_index_level is called early in code --- janitor/functions/coalesce.py | 8 +++++--- tests/functions/test_coalesce.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/janitor/functions/coalesce.py b/janitor/functions/coalesce.py index c6ba8ac09..874885a2e 100644 --- a/janitor/functions/coalesce.py +++ b/janitor/functions/coalesce.py @@ -113,11 +113,13 @@ def coalesce( if default_value: check("default_value", default_value, [int, float, str]) - if target_column_name is None: - target_column_name = column_names[0] - outcome = df.loc(axis=1)[column_names].bfill(axis="columns").iloc[:, 0] if outcome.hasnans and (default_value is not None): outcome = outcome.fillna(default_value) + if target_column_name is None: + df = df.copy() + df.iloc[:, 0] = outcome + return df + return df.assign(**{target_column_name: outcome}) diff --git a/tests/functions/test_coalesce.py b/tests/functions/test_coalesce.py index f17a5f686..e2b0d26d2 100644 --- a/tests/functions/test_coalesce.py +++ b/tests/functions/test_coalesce.py @@ -63,3 +63,22 @@ def test_coalesce_without_delete(): expected = df.assign(s3=df.s1.combine_first(df.s2).fillna(0)) result = df.coalesce("s1", "s2", target_column_name="s3", default_value=0) assert_frame_equal(result, expected) + + +def test_coalesce_duplicate_columns(): + """ + Test output on duplicate columns. + """ + df = pd.DataFrame( + np.array([[1.0, 2.0, 2.0], [np.nan, 3.0, np.nan], [3.0, 1.0, 9.0]]), + columns=["a", "a", "c"], + ) + + expected = pd.DataFrame( + np.array([[1.0, 2.0, 2.0], [3, 3.0, np.nan], [3.0, 1.0, 9.0]]), + columns=["a", "a", "c"], + ) + + actual = df.coalesce("a") + + assert_frame_equal(expected, actual)