-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rename()
for DataFrame and LazyFrame
#239
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Looks nice.
Actually I definitely prefer using a named list, it's much easier to see what is replaced by what when we want to rename a lot of variables: library(polars)
pl$
DataFrame(mtcars)$
rename(
list(
mpg = "miles_per_gallon",
hp = "horsepower",
gear = "Number of forward gears",
carb = "Number of carburators"
)
)
#> shape: (32, 11)
#> ┌────────────────────┬─────┬───────┬────────────┬───┬─────┬─────┬────────────────────┬─────────────┐
#> │ miles_per_gallon ┆ cyl ┆ disp ┆ horsepower ┆ … ┆ vs ┆ am ┆ Number of forward ┆ Number of │
#> │ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ gears ┆ carburators │
#> │ f64 ┆ f64 ┆ f64 ┆ f64 ┆ ┆ f64 ┆ f64 ┆ --- ┆ --- │
#> │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ f64 ┆ f64 │
#> ╞════════════════════╪═════╪═══════╪════════════╪═══╪═════╪═════╪════════════════════╪═════════════╡
#> │ 21.0 ┆ 6.0 ┆ 160.0 ┆ 110.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 4.0 ┆ 4.0 │
#> │ 21.0 ┆ 6.0 ┆ 160.0 ┆ 110.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 4.0 ┆ 4.0 │
#> │ 22.8 ┆ 4.0 ┆ 108.0 ┆ 93.0 ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0 ┆ 1.0 │
#> │ 21.4 ┆ 6.0 ┆ 258.0 ┆ 110.0 ┆ … ┆ 1.0 ┆ 0.0 ┆ 3.0 ┆ 1.0 │
#> │ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │
#> │ 15.8 ┆ 8.0 ┆ 351.0 ┆ 264.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0 ┆ 4.0 │
#> │ 19.7 ┆ 6.0 ┆ 145.0 ┆ 175.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0 ┆ 6.0 │
#> │ 15.0 ┆ 8.0 ┆ 301.0 ┆ 335.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0 ┆ 8.0 │
#> │ 21.4 ┆ 4.0 ┆ 121.0 ┆ 109.0 ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0 ┆ 2.0 │
#> └────────────────────┴─────┴───────┴────────────┴───┴─────┴─────┴────────────────────┴─────────────┘ Created on 2023-06-10 with reprex v2.0.2 |
I agree with that, but I think the |
I prefer I'll change it |
This comment was marked as outdated.
This comment was marked as outdated.
library(polars)
pl$
DataFrame(mtcars)$
rename(
miles_per_gallon = "mpg",
horsepower = "hp",
`Number of forward gears` = "gear",
`Number of carburators` = "carb"
)
#> shape: (32, 11)
#> ┌────────────────────┬─────┬───────┬────────────┬───┬─────┬─────┬────────────────────┬─────────────┐
#> │ miles_per_gallon ┆ cyl ┆ disp ┆ horsepower ┆ … ┆ vs ┆ am ┆ Number of forward ┆ Number of │
#> │ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ gears ┆ carburators │
#> │ f64 ┆ f64 ┆ f64 ┆ f64 ┆ ┆ f64 ┆ f64 ┆ --- ┆ --- │
#> │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ f64 ┆ f64 │
#> ╞════════════════════╪═════╪═══════╪════════════╪═══╪═════╪═════╪════════════════════╪═════════════╡
#> │ 21.0 ┆ 6.0 ┆ 160.0 ┆ 110.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 4.0 ┆ 4.0 │
#> │ 21.0 ┆ 6.0 ┆ 160.0 ┆ 110.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 4.0 ┆ 4.0 │
#> │ 22.8 ┆ 4.0 ┆ 108.0 ┆ 93.0 ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0 ┆ 1.0 │
#> │ 21.4 ┆ 6.0 ┆ 258.0 ┆ 110.0 ┆ … ┆ 1.0 ┆ 0.0 ┆ 3.0 ┆ 1.0 │
#> │ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │
#> │ 15.8 ┆ 8.0 ┆ 351.0 ┆ 264.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0 ┆ 4.0 │
#> │ 19.7 ┆ 6.0 ┆ 145.0 ┆ 175.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0 ┆ 6.0 │
#> │ 15.0 ┆ 8.0 ┆ 301.0 ┆ 335.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0 ┆ 8.0 │
#> │ 21.4 ┆ 4.0 ┆ 121.0 ┆ 109.0 ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0 ┆ 2.0 │
#> └────────────────────┴─────┴───────┴────────────┴───┴─────┴─────┴────────────────────┴─────────────┘ Created on 2023-06-10 with reprex v2.0.2 |
@eitsupi I'm just re-requesting your review since there were changes since you approved |
r-polars has in general a syntax deviation from py-polars . Wherever in py-polars where first input is a list or a dict e.g. |
so @sorhawell are you fine with the syntax in the last post? |
Oh yes I missed that :) sry! Looks great :) I will take a quick look at code also |
forgot to add something |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Thanks!
Not sure about which args we should have here. I used
existing
andnew
as in the Rust implementation but the Python implementation uses a named list likelist(old = "new")
that I also like.What do you think?
Created on 2023-06-10 with reprex v2.0.2