Skip to content

Commit

Permalink
python; allow null columns in FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 22, 2021
1 parent 7b9bf9e commit 1b890e0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
14 changes: 13 additions & 1 deletion py-polars/src/arrow_interop/to_rust.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::error::PyPolarsEr;
use crate::prelude::ArrowDataType;
use polars_core::prelude::Arc;
use polars_core::utils::arrow::{
array::ArrayRef,
datatypes::{Field, Schema},
ffi,
record_batch::RecordBatch,
};
use polars_core::utils::arrow::{array::PrimitiveArray, bitmap::MutableBitmap};
use pyo3::ffi::Py_uintptr_t;
use pyo3::prelude::*;

Expand All @@ -26,7 +28,17 @@ pub fn array_to_rust(obj: &PyAny) -> PyResult<ArrayRef> {

unsafe {
let field = ffi::import_field_from_c(schema.as_ref()).map_err(PyPolarsEr::from)?;
let array = ffi::import_array_from_c(array, &field).map_err(PyPolarsEr::from)?;
let array = if field.data_type == ArrowDataType::Null {
let mut validity = MutableBitmap::new();
validity.extend_constant(1, false);
Box::new(PrimitiveArray::from_data(
ArrowDataType::Float64,
vec![1.0f64].into(),
Some(validity.into()),
))
} else {
ffi::import_array_from_c(array, &field).map_err(PyPolarsEr::from)?
};
Ok(array.into())
}
}
Expand Down
2 changes: 1 addition & 1 deletion py-polars/src/lazy/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub fn map_mul(

// we return an error, because that will become a null value polars lazy apply list
if apply_groups && out.is_none(py) {
return Err(PolarsError::NoData("".into()))
return Err(PolarsError::NoData("".into()));
}

// unpack the wrapper in a PySeries
Expand Down
2 changes: 1 addition & 1 deletion py-polars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub fn map_mul(
output_type: &PyAny,
apply_groups: bool,
) -> PyExpr {
lazy::map_mul(&pyexpr, py, lambda, output_type, apply_groups)
lazy::map_mul(&pyexpr, py, lambda, output_type, apply_groups)
}

#[pymodule]
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ def test_arrow_list_chunked_array():
ca = pa.chunked_array([a, a, a])
s = pl.from_arrow(ca)
assert s.dtype == pl.List


def test_from_pandas_null():
test_df = pd.DataFrame([{0: None}, {0: None}])
out = pl.DataFrame(test_df)
assert out.dtypes == [pl.Float64]
assert out["0"][0] is None

0 comments on commit 1b890e0

Please sign in to comment.