Skip to content

Commit

Permalink
fix(python): allow from pandas null structs (#6430)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Jan 25, 2023
1 parent 3adef12 commit 7e5ac26
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 8 additions & 3 deletions polars/polars-core/src/series/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,14 @@ impl Series {
let new_values = struct_arr
.values()
.iter()
.map(|arr| match arr.validity() {
None => arr.with_validity(Some(validity.clone())),
Some(arr_validity) => arr.with_validity(Some(arr_validity & validity)),
.map(|arr| match arr.data_type() {
ArrowDataType::Null => arr.clone(),
_ => match arr.validity() {
None => arr.with_validity(Some(validity.clone())),
Some(arr_validity) => {
arr.with_validity(Some(arr_validity & validity))
}
},
})
.collect();

Expand Down
13 changes: 13 additions & 0 deletions py-polars/tests/unit/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,16 @@ def test_arrow_list_null_5697() -> None:
assert pl.from_arrow(pa_table,).schema == { # type: ignore[union-attr]
"mycol": pl.List(pl.Null)
}


def test_from_pandas_null_struct_6412() -> None:
data = [
{
"a": {
"b": None,
},
},
{"a": None},
]
df_pandas = pd.DataFrame(data)
assert pl.from_pandas(df_pandas).to_dict(False) == {"a": [{"b": None}, {"b": None}]}

0 comments on commit 7e5ac26

Please sign in to comment.