pandrs/optimized/split_dataframe/
column_view.rs1use 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 pub fn column_type(&self) -> ColumnType {
11 self.column.column_type()
12 }
13
14 pub fn len(&self) -> usize {
16 self.column.len()
17 }
18
19 pub fn is_empty(&self) -> bool {
21 self.column.is_empty()
22 }
23
24 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 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 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 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 pub fn column(&self) -> &Column {
62 &self.column
63 }
64
65 pub fn into_column(self) -> Column {
67 self.column
68 }
69
70 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 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 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 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}