Skip to content

Commit

Permalink
fix: Adding a struct field using a literal raises InvalidOperationErr…
Browse files Browse the repository at this point in the history
…or (#21254)
  • Loading branch information
lukemanley authored Feb 15, 2025
1 parent 3810e6b commit d2f9f0b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 1 addition & 2 deletions crates/polars-plan/src/dsl/function_expr/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ pub(super) fn with_fields(args: &[Column]) -> PolarsResult<Column> {
}

let new_fields = fields.into_values().cloned().collect::<Vec<_>>();
let mut out =
StructChunked::from_series(ca.name().clone(), new_fields[0].len(), new_fields.iter())?;
let mut out = StructChunked::from_series(ca.name().clone(), ca.len(), new_fields.iter())?;
out.zip_outer_validity(ca);
Ok(out.into_column())
}
12 changes: 12 additions & 0 deletions py-polars/tests/unit/datatypes/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,18 @@ def test_nested_object_raises_15237() -> None:
df.select(pl.struct("a"))


def test_empty_struct_with_fields_21095() -> None:
df = pl.DataFrame({"a": [{}, {}]})
assert_frame_equal(
df.select(pl.col("a").struct.with_fields(a=pl.lit(42, pl.Int64))),
pl.DataFrame({"a": [{"a": 42}, {"a": 42}]}),
)
assert_frame_equal(
df.select(pl.col("a").struct.with_fields(a=None)),
pl.DataFrame({"a": [{"a": None}, {"a": None}]}),
)


def test_cast_to_struct_needs_field_14083() -> None:
with pytest.raises(
InvalidOperationError, match="must specify one field in the struct"
Expand Down

0 comments on commit d2f9f0b

Please sign in to comment.