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: Fix out nullability for structs coming from arrow #17738

Merged
merged 2 commits into from
Jul 19, 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 crates/polars-core/src/chunked_array/struct_/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl StructChunked {
}

/// Set the outer nulls into the inner arrays, and clear the outer validity.
fn propagate_nulls(&mut self) {
pub(crate) fn propagate_nulls(&mut self) {
if self.null_count > 0 {
// SAFETY:
// We keep length and dtypes the same.
Expand Down
15 changes: 9 additions & 6 deletions crates/polars-core/src/series/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ impl Series {
BinaryOffset => BinaryOffsetChunked::from_chunks(name, chunks).into_series(),
#[cfg(feature = "dtype-struct")]
Struct(_) => {
StructChunked::from_chunks_and_dtype_unchecked(name, chunks, dtype.clone())
.into_series()
let mut ca =
StructChunked::from_chunks_and_dtype_unchecked(name, chunks, dtype.clone());
ca.propagate_nulls();
ca.into_series()
},
#[cfg(feature = "object")]
Object(_, _) => {
Expand Down Expand Up @@ -400,11 +402,12 @@ impl Series {
#[cfg(feature = "dtype-struct")]
ArrowDataType::Struct(_) => {
let (chunks, dtype) = to_physical_and_dtype(chunks, md);

unsafe {
Ok(
StructChunked::from_chunks_and_dtype_unchecked(name, chunks, dtype)
.into_series(),
)
let mut ca =
StructChunked::from_chunks_and_dtype_unchecked(name, chunks, dtype);
ca.propagate_nulls();
Ok(ca.into_series())
}
},
ArrowDataType::FixedSizeBinary(_) => {
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/datatypes/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,3 +959,9 @@ def test_struct_chunked_gather_17603() -> None:
{"a": 3},
]
}


def test_struct_out_nullability_from_arrow() -> None:
df = pl.DataFrame(pd.DataFrame({"abc": [{"a": 1.0, "b": pd.NA}, pd.NA]}))
res = df.select(a=pl.col("abc").struct.field("a"))
assert res.to_dicts() == [{"a": 1.0}, {"a": None}]