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): Support Series init as struct from @dataclass and annotated NamedTuple #5057

Merged
merged 2 commits into from
Oct 1, 2022
Merged

feat(python): Support Series init as struct from @dataclass and annotated NamedTuple #5057

merged 2 commits into from
Oct 1, 2022

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Oct 1, 2022

Closes #5022


New functionality

  • Extends existing polars DataFrame support for @dataclass and NamedTuple init to Series .
  • Improves type-inference from annotations (specifically from ForwardRef and explicit Optional).

Setup (example)

from dataclasses import dataclass
from datetime import datetime

@dataclass
class Trade:
    timestamp: datetime
    ticker: str
    price: float
    size: int | None = None

trades = [
    Trade(datetime(2022, 10, 1, 14, 30, 45), "AAPL", 138.20, 250),
    Trade(datetime(2022, 10, 2, 10, 15, 12), "FSLY", 9.16, 2000),
    Trade(datetime(2022, 10, 3, 15, 30), "MU", 50.10, 600),
]

Initialise series

import polars as pl
s = pl.Series( trades )

Before (object)

# shape: (3,)
# Series: '' [o][object]
# [
#     Trade(timestamp=datetime.datetime(2022, 10, 1, 14, 30, 45), ticker='AAPL', price=138.2, size=250)
#     Trade(timestamp=datetime.datetime(2022, 10, 2, 10, 15, 12), ticker='FSLY', price=9.16, size=2000)
#     Trade(timestamp=datetime.datetime(2022, 10, 3, 15, 30), ticker='MU', price=50.1, size=600)
# ]

After (struct)

# shape: (3,)
# Series: '' [struct[4]]
# [
#     {2022-10-01 14:30:45,"AAPL",138.2,250}
#     {2022-10-02 10:15:12,"FSLY",9.16,2000}
#     {2022-10-03 15:30:00,"MU",50.1,600}
# ]

Suitable test coverage added for all the above.

@github-actions github-actions bot added the python Related to Python Polars label Oct 1, 2022
@ritchie46 ritchie46 merged commit 571687e into pola-rs:master Oct 1, 2022
@stinodego stinodego changed the title feat[python]: Support Series init as struct from @dataclass and annotated NamedTuple feat(python): Support Series init as struct from @dataclass and annotated NamedTuple Oct 1, 2022
@github-actions github-actions bot added the enhancement New feature or an improvement of an existing feature label Oct 1, 2022
@alexander-beedie alexander-beedie deleted the dataclass-series-struct branch October 2, 2022 06:48
zundertj pushed a commit to zundertj/polars that referenced this pull request Jan 7, 2023
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.

Create struct from namedtuple and dataclass
2 participants