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 strict param to eager/lazy frame "rename" #19017

Merged
merged 1 commit into from
Sep 30, 2024

Conversation

alexander-beedie
Copy link
Collaborator

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

Closes #18954.

Useful; brings the same strict parameter behaviour already found in "drop" to "rename". Default behaviour remains unchanged (eg: strict=True).

Example

import polars as pl

df = pl.DataFrame({
    "siteid": ["xx", "yy", "zz"],
    "value1": [5, 4, 6],
    "value2": [7, 3, 3],
})

When strict=True (default) it is an error to reference a column that is not present.

df.rename({"siteid":"id", "other":"misc"})
# ColumnNotFoundError: 'rename' on column: 'other' is invalid

When strict=False (opt-in) the same rename will succeed for columns that are present, and ignore those that aren't.

df.rename({"siteid":"id", "other":"misc"}, strict=False)
# shape: (3, 3)
# ┌─────┬────────┬────────┐
# │ id  ┆ value1 ┆ value2 │
# │ --- ┆ ---    ┆ ---    │
# │ str ┆ i64    ┆ i64    │
# ╞═════╪════════╪════════╡
# │ xx  ┆ 5      ┆ 7      │
# │ yy  ┆ 4      ┆ 3      │
# │ zz  ┆ 6      ┆ 3      │
# └─────┴────────┴────────┘

@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 Sep 30, 2024
Copy link

codecov bot commented Sep 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.87%. Comparing base (e402e70) to head (b1c59d4).
Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #19017   +/-   ##
=======================================
  Coverage   79.87%   79.87%           
=======================================
  Files        1524     1524           
  Lines      207653   207729   +76     
  Branches     2905     2906    +1     
=======================================
+ Hits       165857   165925   +68     
- Misses      41248    41255    +7     
- Partials      548      549    +1     

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

@ritchie46 ritchie46 merged commit ab5200d into pola-rs:main Sep 30, 2024
27 checks passed
@alexander-beedie alexander-beedie deleted the optional-strict-rename branch September 30, 2024 11:08
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.

Allow lf/df rename() to have strict argument and ignore non existing columns
2 participants