Skip to content

Commit

Permalink
ensure get_index_level is called early in code
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel.oranyeli committed Jan 9, 2024
1 parent 74293be commit 59a2936
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions janitor/functions/coalesce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
19 changes: 19 additions & 0 deletions tests/functions/test_coalesce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 59a2936

Please sign in to comment.