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(python): Allow insert_column to take expressions #19024

Merged
merged 2 commits into from
Oct 1, 2024

Conversation

alexander-beedie
Copy link
Collaborator

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

Closes #18406.

Streamlines insertion of an Expr at an arbitrary index in a DataFrame; currently insert_column only supports this for Series, but it's straightforward to allow Expr too.

Without this affordance the user has to make two calls - one to add the new Expr (typically via with_columns), and then a second call to explicitly reorder all of the columns.

More ergonomic to allow this in a single call (this PR resolves the operation as a single select), and it makes the method consistent with with_columns, which also supports both Expr and Series input.

Example

import polars as pl

df = pl.DataFrame({
    "id": ["xx", "yy", "zz"],
    "v1": [5, 4, 6],
    "v2": [7, 3, 3],
})

df.insert_column(3, (pl.col("v1") * pl.col("v2")).alias("v3"))
df.insert_column(1, (pl.col("v2") - pl.col("v1")).alias("v0"))

# shape: (3, 5)
# ┌─────┬─────┬─────┬─────┬─────┐
# │ id  ┆ v0  ┆ v1  ┆ v2  ┆ v3  │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
# │ str ┆ i64 ┆ i64 ┆ i64 ┆ i64 │
# ╞═════╪═════╪═════╪═════╪═════╡
# │ xx  ┆ 2   ┆ 5   ┆ 7   ┆ 35  │
# │ yy  ┆ -1  ┆ 4   ┆ 3   ┆ 12  │
# │ zz  ┆ -3  ┆ 6   ┆ 3   ┆ 18  │
# └─────┴─────┴─────┴─────┴─────┘

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Sep 30, 2024
Copy link

codecov bot commented Sep 30, 2024

Codecov Report

Attention: Patch coverage is 72.22222% with 5 lines in your changes missing coverage. Please review.

Project coverage is 79.87%. Comparing base (ab5200d) to head (885247b).
Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
py-polars/polars/dataframe/frame.py 72.22% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #19024      +/-   ##
==========================================
+ Coverage   79.85%   79.87%   +0.02%     
==========================================
  Files        1524     1524              
  Lines      207734   207762      +28     
  Branches     2908     2913       +5     
==========================================
+ Hits       165891   165956      +65     
+ Misses      41294    41255      -39     
- Partials      549      551       +2     

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

@ritchie46 ritchie46 merged commit 2e2823a into pola-rs:main Oct 1, 2024
13 checks passed
@alexander-beedie alexander-beedie deleted the insert-expression branch October 4, 2024 08:58
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow expressions in insert_column
3 participants