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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions py-polars/polars/functions/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,10 +1239,12 @@ def lit(
return e
return e.alias(name)

if _check_for_numpy(value) and isinstance(value, np.ndarray):
elif (_check_for_numpy(value) and isinstance(value, np.ndarray)) or isinstance(
value, (list, tuple)
):
return lit(pli.Series("", value))

if dtype:
elif dtype:
return wrap_expr(pylit(value, allow_object)).cast(dtype)

try:
Expand Down
29 changes: 26 additions & 3 deletions py-polars/tests/unit/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,15 @@ def test_count_expr() -> None:


def test_shuffle() -> None:
# Setting random.seed should lead to reproducible results
# setting 'random.seed' should lead to reproducible results
s = pl.Series("a", range(20))
s_list = s.to_list()

random.seed(1)
result1 = pl.select(pl.lit(s).shuffle()).to_series()

random.seed(1)
result2 = pl.select(pl.lit(s).shuffle()).to_series()
result2 = pl.select(a=pl.lit(s_list).shuffle()).to_series()
assert_series_equal(result1, result2)


Expand Down Expand Up @@ -843,6 +846,8 @@ def lit_series(value: Any, dtype: pl.PolarsDataType | None) -> pl.Series:
"f32": lit_series(0, pl.Float32),
"u16": lit_series(0, pl.UInt16),
"i16": lit_series(0, pl.Int16),
"i64": lit_series([8], None),
"list_i64": lit_series([[1, 2, 3]], None),
}
)
assert df.dtypes == [
Expand All @@ -859,8 +864,26 @@ def lit_series(value: Any, dtype: pl.PolarsDataType | None) -> pl.Series:
pl.Float32,
pl.UInt16,
pl.Int16,
pl.Int64,
pl.List(pl.Int64),
]
assert df.row(0) == (d_ms, d, d, d_tz, d_tz, d_tz, d_tz, td_ms, td, td, 0, 0, 0)
assert df.row(0) == (
d_ms,
d,
d,
d_tz,
d_tz,
d_tz,
d_tz,
td_ms,
td,
td,
0,
0,
0,
8,
[1, 2, 3],
)


def test_incompatible_lit_dtype() -> None:
Expand Down