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 struct.with_fields #16305

Merged
merged 1 commit into from
May 18, 2024
Merged

feat: Add struct.with_fields #16305

merged 1 commit into from
May 18, 2024

Conversation

ritchie46
Copy link
Member

@ritchie46 ritchie46 commented May 18, 2024

Akin to DataFrame.with_columns.

Will follow up with a select_fields as well.

>>> df = pl.DataFrame(
...     {
...         "coords": [{"x": 1, "y": 4}, {"x": 4, "y": 9}, {"x": 9, "y": 16}],
...         "multiply": [10, 2, 3],
...     }
... )
>>> df
shape: (3, 2)
┌───────────┬──────────┐
│ coordsmultiply │
│ ------      │
│ struct[2] ┆ i64      │
╞═══════════╪══════════╡
│ {1,4}     ┆ 10       │
│ {4,9}     ┆ 2        │
│ {9,16}    ┆ 3        │
└───────────┴──────────┘
>>> df = df.with_columns(
...     pl.col("coords").struct.with_fields(
...         pl.col("coords").struct.field("x").sqrt(),
...         y_mul=pl.col("coords").struct.field("y") * pl.col("multiply"),
...     ),
... )
>>> df
shape: (3, 2)
┌─────────────┬──────────┐
│ coordsmultiply │
│ ------      │
│ struct[3]   ┆ i64      │
╞═════════════╪══════════╡
│ {1.0,4,40}  ┆ 10       │
│ {2.0,9,18}  ┆ 2        │
│ {3.0,16,48} ┆ 3        │
└─────────────┴──────────┘
>>> df.unnest("coords")
shape: (3, 4)
┌─────┬─────┬───────┬──────────┐
│ xyy_mulmultiply │
│ ------------      │
│ f64i64i64i64      │
╞═════╪═════╪═══════╪══════════╡
│ 1.044010       │
│ 2.09182        │
│ 3.016483        │
└─────┴─────┴───────┴──────────┘

@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 May 18, 2024
@ritchie46 ritchie46 merged commit e214e93 into main May 18, 2024
29 checks passed
@ritchie46 ritchie46 deleted the with_fields branch May 18, 2024 07:56
@ritchie46 ritchie46 mentioned this pull request May 18, 2024
@alexander-beedie
Copy link
Collaborator

Love it! Also, a select_fields addition would probably close this one: #3859 🤔

@ritchie46
Copy link
Member Author

Ah, that seems like a special case of expansion. I think that should be fairly easy. Will take a look. And ping me if I forget.

@c-peters c-peters added the accepted Ready for implementation label May 21, 2024
Wouittone pushed a commit to Wouittone/polars that referenced this pull request Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants