Skip to content

Commit

Permalink
fix named_expr with schema in struct and added corresponding test
Browse files Browse the repository at this point in the history
  • Loading branch information
Casagrande, Sandro committed Jul 21, 2024
1 parent 7888d3b commit 50f7f0e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion py-polars/polars/functions/as_datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def struct(
pyexprs = parse_into_list_of_expressions(*exprs, **named_exprs)

if schema:
if not exprs:
if not exprs and not named_exprs:
# no columns or expressions provided; create one from schema keys
expr = wrap_expr(
plr.as_struct(parse_into_list_of_expressions(list(schema.keys())))
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/datatypes/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,3 +970,11 @@ def test_struct_out_nullability_from_arrow() -> None:
def test_empty_struct_raise() -> None:
with pytest.raises(ValueError):
pl.struct()


def test_named_exprs() -> None:
df = pl.DataFrame({"a": 1})
schema = {"b": pl.Int64}
res = df.select(pl.struct(schema=schema, b=pl.col("a")))
expected = df.select(pl.struct(pl.col("a").alias("b"), schema=schema))
assert_frame_equal(res, expected)

0 comments on commit 50f7f0e

Please sign in to comment.