pandrs/optimized/split_dataframe/
column_view.rs

1//! Implementation of ColumnView
2
3use super::core::ColumnView;
4use crate::column::{BooleanColumn, Column, ColumnType, Float64Column, Int64Column, StringColumn};
5use crate::error::{Error, Result};
6use std::any::Any;
7
8impl ColumnView {
9    /// Get column type
10    pub fn column_type(&self) -> ColumnType {
11        self.column.column_type()
12    }
13
14    /// Get column length
15    pub fn len(&self) -> usize {
16        self.column.len()
17    }
18
19    /// Check if column is empty
20    pub fn is_empty(&self) -> bool {
21        self.column.is_empty()
22    }
23
24    /// Access as integer column
25    pub fn as_int64(&self) -> Option<&crate::column::Int64Column> {
26        if let Column::Int64(ref col) = self.column {
27            Some(col)
28        } else {
29            None
30        }
31    }
32
33    /// Access as floating point column
34    pub fn as_float64(&self) -> Option<&crate::column::Float64Column> {
35        if let Column::Float64(ref col) = self.column {
36            Some(col)
37        } else {
38            None
39        }
40    }
41
42    /// Access as string column
43    pub fn as_string(&self) -> Option<&crate::column::StringColumn> {
44        if let Column::String(ref col) = self.column {
45            Some(col)
46        } else {
47            None
48        }
49    }
50
51    /// Access as boolean column
52    pub fn as_boolean(&self) -> Option<&crate::column::BooleanColumn> {
53        if let Column::Boolean(ref col) = self.column {
54            Some(col)
55        } else {
56            None
57        }
58    }
59
60    /// Get reference to internal Column
61    pub fn column(&self) -> &Column {
62        &self.column
63    }
64
65    /// Get internal Column (consuming)
66    pub fn into_column(self) -> Column {
67        self.column
68    }
69
70    /// Get float64 value at specific index
71    pub fn get_f64(&self, index: usize) -> Result<Option<f64>> {
72        match &self.column {
73            Column::Float64(col) => col.get(index),
74            _ => Err(Error::ColumnTypeMismatch {
75                name: self.column.name().unwrap_or("").to_string(),
76                expected: ColumnType::Float64,
77                found: self.column.column_type(),
78            }),
79        }
80    }
81
82    /// Get int64 value at specific index
83    pub fn get_i64(&self, index: usize) -> Result<Option<i64>> {
84        match &self.column {
85            Column::Int64(col) => col.get(index),
86            _ => Err(Error::ColumnTypeMismatch {
87                name: self.column.name().unwrap_or("").to_string(),
88                expected: ColumnType::Int64,
89                found: self.column.column_type(),
90            }),
91        }
92    }
93
94    /// Get string value at specific index
95    pub fn get_string(&self, index: usize) -> Result<Option<String>> {
96        match &self.column {
97            Column::String(col) => match col.get(index)? {
98                Some(s) => Ok(Some(s.to_string())),
99                None => Ok(None),
100            },
101            _ => Err(Error::ColumnTypeMismatch {
102                name: self.column.name().unwrap_or("").to_string(),
103                expected: ColumnType::String,
104                found: self.column.column_type(),
105            }),
106        }
107    }
108
109    /// Get boolean value at specific index
110    pub fn get_bool(&self, index: usize) -> Result<Option<bool>> {
111        match &self.column {
112            Column::Boolean(col) => col.get(index),
113            _ => Err(Error::ColumnTypeMismatch {
114                name: self.column.name().unwrap_or("").to_string(),
115                expected: ColumnType::Boolean,
116                found: self.column.column_type(),
117            }),
118        }
119    }
120}