1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::prelude::*;
#[cfg(any(
    feature = "dtype-datetime",
    feature = "dtype-date",
    feature = "dtype-duration",
    feature = "dtype-time"
))]
use polars_arrow::compute::cast::cast;

impl Series {
    /// Convert a chunk in the Series to the correct Arrow type.
    /// This conversion is needed because polars doesn't use a
    /// 1 on 1 mapping for logical/ categoricals, etc.
    pub fn to_arrow(&self, chunk_idx: usize) -> ArrayRef {
        match self.dtype() {
            #[cfg(feature = "dtype-categorical")]
            DataType::Categorical(_) => {
                let ca = self.categorical().unwrap();
                let arr = ca.logical().chunks()[chunk_idx].clone();
                let cats = UInt32Chunked::from_chunks("", vec![arr]);

                let new = CategoricalChunked::from_cats_and_rev_map(cats, ca.get_rev_map().clone());

                let arr: DictionaryArray<u32> = (&new).into();
                Arc::new(arr) as ArrayRef
            }
            #[cfg(feature = "dtype-date")]
            DataType::Date => {
                let arr = cast(&*self.chunks()[chunk_idx], &DataType::Date.to_arrow()).unwrap();
                Arc::from(arr)
            }
            #[cfg(feature = "dtype-datetime")]
            DataType::Datetime(_, _) => {
                let arr = cast(&*self.chunks()[chunk_idx], &self.dtype().to_arrow()).unwrap();
                Arc::from(arr)
            }
            #[cfg(feature = "dtype-duration")]
            DataType::Duration(_) => {
                let arr = cast(&*self.chunks()[chunk_idx], &self.dtype().to_arrow()).unwrap();
                Arc::from(arr)
            }
            #[cfg(feature = "dtype-time")]
            DataType::Time => {
                let arr = cast(&*self.chunks()[chunk_idx], &DataType::Time.to_arrow()).unwrap();
                Arc::from(arr)
            }
            #[cfg(feature = "dtype-struct")]
            DataType::Struct(_) => {
                let ca = self.struct_().unwrap();
                ca.arrow_array().clone()
            }
            _ => self.chunks()[chunk_idx].clone(),
        }
    }
}