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

fix(python): Simultaneous usage of named_expr and schema in pl.struct #17768

Merged
merged 2 commits into from
Jul 22, 2024
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
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")))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res and expect seem to be the exact same query. Can you check the valid outputs via a assert res.to_dict(as_series=False) == expected

and a res.schema == expected_schema?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I made that part more direct. Thanks @ritchie46 .

assert res.to_dict(as_series=False) == {"b": [{"b": 1}]}
assert res.schema["b"] == pl.Struct(schema)