Skip to content

Commit

Permalink
fix rename same name (#3364)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed May 11, 2022
1 parent 2d94240 commit 2a91a81
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion polars/polars-lazy/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,17 @@ impl LazyFrame {
self.select_local(vec![col("*").reverse()])
}

fn rename_impl_swapping(self, existing: Vec<String>, new: Vec<String>) -> Self {
fn rename_impl_swapping(self, mut existing: Vec<String>, mut new: Vec<String>) -> Self {
assert_eq!(new.len(), existing.len());
for idx in 0..existing.len() {
// remove "name" -> "name"
// these are no ops.
if existing[idx] == new[idx] {
existing.swap_remove(idx);
new.swap_remove(idx);
}
}

// schema after renaming
let mut new_schema = (&*self.schema()).clone();

Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,18 @@ def test_rename_swap() -> None:
assert out.frame_equal(expected)


def test_rename_same_name() -> None:
df = pl.DataFrame(
{
"nrs": [1, 2, 3, 4, 5],
"groups": ["A", "A", "B", "C", "B"],
}
).lazy()
df = df.rename({"groups": "groups"})
df = df.select(["groups"])
assert df.collect().to_dict(False) == {"groups": ["A", "A", "B", "C", "B"]}


def test_fill_null() -> None:
df = pl.DataFrame({"a": [1, 2], "b": [3, None]})
assert df.fill_null(4).frame_equal(pl.DataFrame({"a": [1, 2], "b": [3, 4]}))
Expand Down

0 comments on commit 2a91a81

Please sign in to comment.