Path: blob/main/crates/polars-plan/src/plans/python/utils.rs
6940 views
use polars_core::error::{PolarsResult, polars_err};1use polars_core::frame::DataFrame;2use polars_core::schema::SchemaRef;3use polars_ffi::version_0::SeriesExport;4use pyo3::intern;5use pyo3::prelude::*;67pub fn python_df_to_rust(py: Python, df: Bound<PyAny>) -> PolarsResult<DataFrame> {8let err = |_| polars_err!(ComputeError: "expected a polars.DataFrame; got {}", df);9let pydf = df.getattr(intern!(py, "_df")).map_err(err)?;1011let width = pydf.call_method0(intern!(py, "width")).unwrap();12let width = width.extract::<usize>().unwrap();1314// Don't resize the Vec<> so that the drop of the SeriesExport will not be caleld.15let mut export: Vec<SeriesExport> = Vec::with_capacity(width);16let location = export.as_mut_ptr();1718let _ = pydf19.call_method1(intern!(py, "_export_columns"), (location as usize,))20.unwrap();2122unsafe { polars_ffi::version_0::import_df(location, width) }23}2425pub(crate) fn python_schema_to_rust(py: Python, schema: Bound<PyAny>) -> PolarsResult<SchemaRef> {26let err = |_| polars_err!(ComputeError: "expected a polars.Schema; got {}", schema);27let df = schema.call_method0("to_frame").map_err(err)?;28python_df_to_rust(py, df).map(|df| df.schema().clone())29}303132