polars_python/series/
export.rs

1use polars_core::prelude::*;
2use polars_ffi::version_0::SeriesExport;
3use pyo3::IntoPyObjectExt;
4use pyo3::prelude::*;
5use pyo3::types::{PyCapsule, PyList};
6
7use super::PySeries;
8use crate::error::PyPolarsErr;
9use crate::interop;
10use crate::interop::arrow::to_py::series_to_stream;
11use crate::prelude::*;
12
13#[pymethods]
14impl PySeries {
15    /// Convert this Series to a Python list.
16    /// This operation copies data.
17    pub fn to_list<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
18        let series = &self.series;
19
20        fn to_list_recursive<'py>(py: Python<'py>, series: &Series) -> PyResult<Bound<'py, PyAny>> {
21            let pylist = match series.dtype() {
22                DataType::Boolean => PyList::new(py, series.bool().map_err(PyPolarsErr::from)?)?,
23                DataType::UInt8 => PyList::new(py, series.u8().map_err(PyPolarsErr::from)?)?,
24                DataType::UInt16 => PyList::new(py, series.u16().map_err(PyPolarsErr::from)?)?,
25                DataType::UInt32 => PyList::new(py, series.u32().map_err(PyPolarsErr::from)?)?,
26                DataType::UInt64 => PyList::new(py, series.u64().map_err(PyPolarsErr::from)?)?,
27                DataType::Int8 => PyList::new(py, series.i8().map_err(PyPolarsErr::from)?)?,
28                DataType::Int16 => PyList::new(py, series.i16().map_err(PyPolarsErr::from)?)?,
29                DataType::Int32 => PyList::new(py, series.i32().map_err(PyPolarsErr::from)?)?,
30                DataType::Int64 => PyList::new(py, series.i64().map_err(PyPolarsErr::from)?)?,
31                DataType::Int128 => PyList::new(py, series.i128().map_err(PyPolarsErr::from)?)?,
32                DataType::Float32 => PyList::new(py, series.f32().map_err(PyPolarsErr::from)?)?,
33                DataType::Float64 => PyList::new(py, series.f64().map_err(PyPolarsErr::from)?)?,
34                DataType::Categorical(_, _) | DataType::Enum(_, _) => PyList::new(
35                    py,
36                    series.categorical().map_err(PyPolarsErr::from)?.iter_str(),
37                )?,
38                #[cfg(feature = "object")]
39                DataType::Object(_) => {
40                    let v = PyList::empty(py);
41                    for i in 0..series.len() {
42                        let obj: Option<&ObjectValue> = series.get_object(i).map(|any| any.into());
43                        v.append(obj)?;
44                    }
45                    v
46                },
47                DataType::List(_) => {
48                    let v = PyList::empty(py);
49                    let ca = series.list().map_err(PyPolarsErr::from)?;
50                    for opt_s in ca.amortized_iter() {
51                        match opt_s {
52                            None => {
53                                v.append(py.None())?;
54                            },
55                            Some(s) => {
56                                let pylst = to_list_recursive(py, s.as_ref())?;
57                                v.append(pylst)?;
58                            },
59                        }
60                    }
61                    v
62                },
63                DataType::Array(_, _) => {
64                    let v = PyList::empty(py);
65                    let ca = series.array().map_err(PyPolarsErr::from)?;
66                    for opt_s in ca.amortized_iter() {
67                        match opt_s {
68                            None => {
69                                v.append(py.None())?;
70                            },
71                            Some(s) => {
72                                let pylst = to_list_recursive(py, s.as_ref())?;
73                                v.append(pylst)?;
74                            },
75                        }
76                    }
77                    v
78                },
79                DataType::Date => {
80                    let ca = series.date().map_err(PyPolarsErr::from)?;
81                    return Wrap(ca).into_bound_py_any(py);
82                },
83                DataType::Time => {
84                    let ca = series.time().map_err(PyPolarsErr::from)?;
85                    return Wrap(ca).into_bound_py_any(py);
86                },
87                DataType::Datetime(_, _) => {
88                    let ca = series.datetime().map_err(PyPolarsErr::from)?;
89                    return Wrap(ca).into_bound_py_any(py);
90                },
91                DataType::Decimal(_, _) => {
92                    let ca = series.decimal().map_err(PyPolarsErr::from)?;
93                    return Wrap(ca).into_bound_py_any(py);
94                },
95                DataType::String => {
96                    let ca = series.str().map_err(PyPolarsErr::from)?;
97                    return Wrap(ca).into_bound_py_any(py);
98                },
99                DataType::Struct(_) => {
100                    let ca = series.struct_().map_err(PyPolarsErr::from)?;
101                    return Wrap(ca).into_bound_py_any(py);
102                },
103                DataType::Duration(_) => {
104                    let ca = series.duration().map_err(PyPolarsErr::from)?;
105                    return Wrap(ca).into_bound_py_any(py);
106                },
107                DataType::Binary => {
108                    let ca = series.binary().map_err(PyPolarsErr::from)?;
109                    return Wrap(ca).into_bound_py_any(py);
110                },
111                DataType::Null => {
112                    let null: Option<u8> = None;
113                    let n = series.len();
114                    let iter = std::iter::repeat_n(null, n);
115                    use std::iter::RepeatN;
116                    struct NullIter {
117                        iter: RepeatN<Option<u8>>,
118                        n: usize,
119                    }
120                    impl Iterator for NullIter {
121                        type Item = Option<u8>;
122
123                        fn next(&mut self) -> Option<Self::Item> {
124                            self.iter.next()
125                        }
126                        fn size_hint(&self) -> (usize, Option<usize>) {
127                            (self.n, Some(self.n))
128                        }
129                    }
130                    impl ExactSizeIterator for NullIter {}
131
132                    PyList::new(py, NullIter { iter, n })?
133                },
134                DataType::Unknown(_) => {
135                    panic!("to_list not implemented for unknown")
136                },
137                DataType::BinaryOffset => {
138                    unreachable!()
139                },
140            };
141            Ok(pylist.into_any())
142        }
143
144        to_list_recursive(py, series)
145    }
146
147    /// Return the underlying Arrow array.
148    #[allow(clippy::wrong_self_convention)]
149    fn to_arrow(&mut self, py: Python<'_>, compat_level: PyCompatLevel) -> PyResult<PyObject> {
150        self.rechunk(py, true)?;
151        let pyarrow = py.import("pyarrow")?;
152
153        interop::arrow::to_py::to_py_array(
154            self.series.to_arrow(0, compat_level.0),
155            &self.series.field().to_arrow(compat_level.0),
156            &pyarrow,
157        )
158    }
159
160    #[allow(unused_variables)]
161    #[pyo3(signature = (requested_schema=None))]
162    fn __arrow_c_stream__<'py>(
163        &self,
164        py: Python<'py>,
165        requested_schema: Option<PyObject>,
166    ) -> PyResult<Bound<'py, PyCapsule>> {
167        series_to_stream(&self.series, py)
168    }
169
170    pub fn _export(&mut self, _py: Python<'_>, location: usize) {
171        let export = polars_ffi::version_0::export_series(&self.series);
172        unsafe {
173            (location as *mut SeriesExport).write(export);
174        }
175    }
176}