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): improve Series/DataFrame init from existing Series/DataFrame objects #13344

Merged
merged 2 commits into from
Jan 2, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Dec 31, 2023

Closes #13338.

Various init enhancements. Can already init Series and DataFrame objects directly from an existing Series; now we can additionally init both DataFrame and Series from an existing DataFrame (improving cross-class constructor consistency).

Also now preserves Series name on Series(Series) init, and adds some (marginal) fast-paths if neither "schema" nor "schema_overrides" params are passed to the DataFrame constructor when initialising from either type (if these parameters are passed, they are handled appropriately).

Examples

import polars as pl

df1 = pl.DataFrame( {"id": [0, 1], "misc": ["a", "b"], "val": [-10, 10]} )
# ┌─────┬──────┬─────┐
# │ id  ┆ misc ┆ val │
# │ --- ┆ ---  ┆ --- │
# │ i64 ┆ str  ┆ i64 │
# ╞═════╪══════╪═════╡
# │ 0   ┆ a    ┆ -10 │
# │ 1   ┆ b    ┆ 10  │
# └─────┴──────┴─────┘

df2 = pl.DataFrame( df1, schema=["a", "b", "c"], schema_overrides={"val": pl.Int8} )
# ┌─────┬─────┬─────┐
# │ a   ┆ b   ┆ c   │
# │ --- ┆ --- ┆ --- │
# │ i64 ┆ str ┆ i8  │
# ╞═════╪═════╪═════╡
# │ 0   ┆ a   ┆ -10 │
# │ 1   ┆ b   ┆ 10  │
# └─────┴─────┴─────┘

s1 = pl.Series( "s", df2 )
# Series: 's' [struct[3]]
# [
#   {0,"a",-10}
#   {1,"b",10}
# ]

s2 = pl.Series( s1 )
# Series: 's' [struct[3]]
# [
#   {0,"a",-10}
#   {1,"b",10}
# ]

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Dec 31, 2023
Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

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

I like the new functionality! Just one minor nitpick and this can be merged 👍

py-polars/polars/series/series.py Outdated Show resolved Hide resolved
Co-authored-by: Stijn de Gooijer <stijn@degooijer.io>
@stinodego stinodego merged commit 4d95c18 into pola-rs:main Jan 2, 2024
13 of 14 checks passed
@alexander-beedie alexander-beedie deleted the updated-frame-series-init branch January 2, 2024 11:37
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.

Preserve name when passing a Series to pl.Series(), and allow passing a DataFrame to pl.DataFrame()
2 participants