Skip to content

Commit

Permalink
fix(python): Simultaneous usage of named_expr and schema in `pl.s…
Browse files Browse the repository at this point in the history
…truct` (#17768)

Co-authored-by: Casagrande, Sandro <[email protected]>
  • Loading branch information
SandroCasagrande and Casagrande, Sandro authored Jul 22, 2024
1 parent 62fe50d commit 26346cf
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")))
assert res.to_dict(as_series=False) == {"b": [{"b": 1}]}
assert res.schema["b"] == pl.Struct(schema)

0 comments on commit 26346cf

Please sign in to comment.