Skip to content

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

Open
MatteoPossamai wants to merge 1 commit into
pola-rs:mainfrom
MatteoPossamai:fix/struct-cast-strict-missing-fields
Open

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

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
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.46%. Comparing base (ae588a9) to head (57db45b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #28631      +/-   ##
==========================================
- Coverage   81.33%   76.46%   -4.88%     
==========================================
  Files        1870     1870              
  Lines      263034   263039       +5     
  Branches     3217     3217              
==========================================
- Hits       213950   201144   -12806     
- Misses      48253    61064   +12811     
  Partials      831      831              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

1 participant