Skip to content

Commit

Permalink
moved logic to return original dataframe sooner for more efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
romanovacca authored and stinodego committed Oct 25, 2023
1 parent 824c28d commit a8dac99
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions crates/polars-core/src/frame/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ impl DataFrame {
}
})
.collect();
// if value_vars is still empty after attempting to populate it, return original dataframe
if value_vars.is_empty() {
return Ok(self.clone());
}
}

// values will all be placed in single column, so we must find their supertype
Expand All @@ -288,10 +292,6 @@ impl DataFrame {
.get(v)
.ok_or_else(|| polars_err!(ColumnNotFound: "{}", v))
});
// if there only is a single column, we return the original DataFrame
if value_vars.len() < 1 {
return Ok(self.clone());
}

let mut st = iter.next().unwrap()?.clone();
for dt in iter {
Expand Down
11 changes: 8 additions & 3 deletions py-polars/tests/unit/operations/test_melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ def test_melt_projection_pd_7747() -> None:
)
assert_frame_equal(result, expected)


def test_melt_single_column() -> None:
df = pl.DataFrame({'single_column': [1,2,3],})
result = df.melt('single_column')
assert_frame_equal(result, df)
df = pl.DataFrame(
{
"single_column": [1, 2, 3],
}
)
result = df.melt("single_column")
assert_frame_equal(result, df)

0 comments on commit a8dac99

Please sign in to comment.