Skip to content
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

feat: Add name.replace to the set of column rename options #17942

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jul 30, 2024

Closes #17920.

Handy addition to the set of name methods.

Example

import polars.selectors as cs
import polars as pl

df = pl.DataFrame({
    "n_foo": [1, 2, 3],
    "n_bar": [4, 5, 6],
    "denom": [7, 8, 9],
}).with_columns(
    cs.starts_with("n_").truediv("denom").name.replace("^n_", "frac_"),
)
# shape: (3, 5)
# ┌───────┬───────┬───────┬──────────┬──────────┐
# │ n_foo ┆ n_bar ┆ denom ┆ frac_foo ┆ frac_bar │
# │ ---   ┆ ---   ┆ ---   ┆ ---      ┆ ---      │
# │ i64   ┆ i64   ┆ i64   ┆ f64      ┆ f64      │
# ╞═══════╪═══════╪═══════╪══════════╪══════════╡
# │ 1     ┆ 4     ┆ 7     ┆ 0.142857 ┆ 0.571429 │
# │ 2     ┆ 5     ┆ 8     ┆ 0.25     ┆ 0.625    │
# │ 3     ┆ 6     ┆ 9     ┆ 0.333333 ┆ 0.666667 │
# └───────┴───────┴───────┴──────────┴──────────┘

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Jul 30, 2024
@alexander-beedie alexander-beedie changed the title feat: Add name.replace expression to support additional column rename options feat: Add name.replace expression to the set of column rename options Jul 30, 2024
@alexander-beedie alexander-beedie changed the title feat: Add name.replace expression to the set of column rename options feat: Add name.replace to the set of column rename options Jul 30, 2024
Copy link

codecov bot commented Jul 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.13%. Comparing base (676f10d) to head (fe03f7a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #17942   +/-   ##
=======================================
  Coverage   79.13%   79.13%           
=======================================
  Files        1572     1572           
  Lines      219839   219859   +20     
  Branches     2462     2462           
=======================================
+ Hits       173961   173986   +25     
+ Misses      45310    45305    -5     
  Partials      568      568           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

pub fn replace(self, pattern: &str, value: &str, literal: bool) -> Expr {
let value = value.to_string();
let pattern = pattern.to_string();
if literal {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to move all map functions to map_private so that the DSL can be serialized and has all the information. So this requires a map_private where we do the expansion upon ir conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a replace method to the expr.name attribute
2 participants