Skip to content

fix: Raise on missing struct fields when casting with strict=True - #28630

Closed
MatteoPossamai wants to merge 1 commit into
pola-rs:mainfrom
MatteoPossamai:mpossamai/raise_on_strict_true
Closed

fix: Raise on missing struct fields when casting with strict=True#28630
MatteoPossamai wants to merge 1 commit into
pola-rs:mainfrom
MatteoPossamai:mpossamai/raise_on_strict_true

Conversation

@MatteoPossamai

Copy link
Copy Markdown
Contributor

Closes #28587.

Problem

Struct casts map fields by name and unconditionally fill unmatched target
fields with nulls. Under strict=True this never raised:

s = pl.Series([{"a": 1, "b": 2}])
s.cast(pl.Struct({"x": pl.Int64, "y": pl.Int64}), strict=True)
# [{'x': None, 'y': None}]  <- no error, all data gone

Changes

  • StructChunked::cast_impl: when a target field is not found in the
    source struct, raise a SchemaError if the cast is strict. The error
    message names the missing field and lists the available source fields.
    Non-strict casts keep the existing null-filling behavior
  • test_nested_struct_cast_22744 now passes strict=False: it relies on
    null-filling missing fields, which remains supported for non-strict casts
    (per the expected behavior in cast(Struct{...}, strict=True) to renamed fields silently nulls every value #28587).
  • Added test_struct_cast_strict_missing_field_28587 covering: strict cast
    raises for both zero-overlap and partial-overlap field names, and
    non-strict cast still null-fills.

Local test

Screenshot From 2026-07-31 15-43-01

@github-actions github-actions Bot added A-dtype-struct Area: struct data type fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-dtype-struct Area: struct data type fix Bug fix python Related to Python Polars rust Related to Rust Polars

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cast(Struct{...}, strict=True) to renamed fields silently nulls every value

2 participants