Skip to content

Commit

Permalink
fix[rust]: fix rename edge case caused by redundant mappings (#4689)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 2, 2022
1 parent d247d02 commit ac4b9a9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
26 changes: 19 additions & 7 deletions polars/polars-lazy/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,26 @@ impl LazyFrame {
.into_iter()
.map(|a| a.as_ref().to_string())
.collect::<Vec<_>>();
// todo! make delayed
let schema = &*self.schema().unwrap();
// a column gets swapped
if new.iter().any(|name| schema.get(name).is_some()) {
self.rename_impl_swapping(existing, new)
} else {
self.rename_impl(existing, new)

fn inner(lf: LazyFrame, existing: Vec<String>, new: Vec<String>) -> LazyFrame {
// remove mappings that map to themselves.
let (existing, new): (Vec<_>, Vec<_>) = existing
.into_iter()
.zip(new)
.flat_map(|(a, b)| if a == b { None } else { Some((a, b)) })
.unzip();

// todo! make delayed
let schema = &*lf.schema().unwrap();
// a column gets swapped
if new.iter().any(|name| schema.get(name).is_some()) {
lf.rename_impl_swapping(existing, new)
} else {
lf.rename_impl(existing, new)
}
}

inner(self, existing, new)
}

/// Removes columns from the DataFrame.
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,10 @@ def test_unknown_apply() -> None:
"Flour": [10.0, 100.0, 100.0, 20.0],
}
assert q.dtypes == [pl.Int64, pl.Unknown]


def test_remove_redundant_mapping_4668() -> None:
df = pl.DataFrame([["a"]] * 2, ["A", "B "]).lazy()
clean_name_dict = {x: " ".join(x.split()) for x in df.columns}
df = df.rename(clean_name_dict)
assert df.columns == ["A", "B"]

0 comments on commit ac4b9a9

Please sign in to comment.