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 list/tuple lit values #7879

Merged
merged 1 commit into from
Mar 30, 2023

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Mar 30, 2023

Closes: #6608 and #7862.
Also references: #6596.

This has come up a few times, and it appears to be a straightforward "quality of life" addition; allows for 1D/2D list/tuple literals to initialise lit like any other literals:

  • lit( [1,2,3] ) → lit( Series([1,2,3]) )
  • lit( [[1,2],[2,3]] ) → lit( Series([[1,2],[2,3]]) )

Example:

df = pl.DataFrame( {'a': [3,4]} )
df.select(
    result = pl.lit( [1,2] ).append( pl.col('a') )
)
# shape: (4, 1)
# ┌────────┐
# │ result │
# │ ---    │
# │ i64    │
# ╞════════╡
# │ 1      │
# │ 2      │
# │ 3      │
# │ 4      │
# └────────┘

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Mar 30, 2023
@ritchie46 ritchie46 merged commit 6cb68c1 into pola-rs:master Mar 30, 2023
@alexander-beedie alexander-beedie deleted the list-tuple-literal branch March 30, 2023 13:16
@josh josh mentioned this pull request Apr 10, 2023
2 tasks
@m-legrand
Copy link

Given that list[...] is a valid column type, this breaks the intuitive consistency of the api:

df = pl.DataFrame({x: [1, 2], y=[[1], [2]]})
df.filter(pl.col("x") == pl.lit(1))  # works
df.filter(pl.col("y") == pl.lit([1]))  # doesn't work

Not sure this straightforward quality of life was worth it.
The consistent, intuitive api is really one of the core value-add of Polars vs e.g. Pandas (along with perf, of course), I'm not sure changes like this are making it any favour :/

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.

Add pl.lit(list)
3 participants