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 from generator #5411

Merged
merged 1 commit into from
Nov 3, 2022
Merged

feat(python): support Series init from generator #5411

merged 1 commit into from
Nov 3, 2022

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Nov 3, 2022

Closes #5395.


Currently Series init requires full materialisation of generators/iterables, up-front. As we don't have the same costs relating to chunked-append as pandas, we can take the more memory-friendly approach of incremental materialisation followed by a final call to rechunk.

Before: (don't actually do this - much more efficient to init 'range' directly)

import polars as pl

def gen(n):
    yield from range(n)

pl.Series("s", gen(5), dtype=pl.Int8)

# ValueError: Series constructor not called properly. 
#  Got <generator object gen at 0x17d2fc200>.

After:

pl.Series("s", gen(5), dtype=pl.Int8)

# shape: (5,)
# Series: 's' [i8]
# [
#     0
#     1
#     2
#     3
#     4
# ]

Also:

Ensure Series generated from python range object still respect the given integer dtype, if provided (was being ignored; always i64).

Follow-up:

Offer generator support for DataFrame init; will take care of that later, in a separate PR.

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Nov 3, 2022
@ritchie46 ritchie46 merged commit 39e061c into pola-rs:master Nov 3, 2022
@alexander-beedie alexander-beedie deleted the series-generator-init branch November 3, 2022 17:24
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 DataFrame with Python Generator (ValueError: Series constructor not called properly.)
2 participants