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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use crate::prelude::*;

impl Series {
    /// Unpack to ChunkedArray of dtype i8
    pub fn i8(&self) -> PolarsResult<&Int8Chunked> {
        match self.dtype() {
            DataType::Int8 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const Int8Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Int8").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray i16
    pub fn i16(&self) -> PolarsResult<&Int16Chunked> {
        match self.dtype() {
            DataType::Int16 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const Int16Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Int16").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray
    /// ```
    /// # use polars_core::prelude::*;
    /// let s = Series::new("foo", [1i32 ,2, 3]);
    /// let s_squared: Series = s.i32()
    ///     .unwrap()
    ///     .into_iter()
    ///     .map(|opt_v| {
    ///         match opt_v {
    ///             Some(v) => Some(v * v),
    ///             None => None, // null value
    ///         }
    /// }).collect();
    /// ```
    pub fn i32(&self) -> PolarsResult<&Int32Chunked> {
        match self.dtype() {
            DataType::Int32 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const Int32Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Int32").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype i64
    pub fn i64(&self) -> PolarsResult<&Int64Chunked> {
        match self.dtype() {
            DataType::Int64 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const Int64Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Int64").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype f32
    pub fn f32(&self) -> PolarsResult<&Float32Chunked> {
        match self.dtype() {
            DataType::Float32 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const Float32Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Float32").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype f64
    pub fn f64(&self) -> PolarsResult<&Float64Chunked> {
        match self.dtype() {
            DataType::Float64 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const Float64Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Float64").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype u8
    pub fn u8(&self) -> PolarsResult<&UInt8Chunked> {
        match self.dtype() {
            DataType::UInt8 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const UInt8Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != UInt8").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype u16
    pub fn u16(&self) -> PolarsResult<&UInt16Chunked> {
        match self.dtype() {
            DataType::UInt16 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const UInt16Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != UInt16").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype u32
    pub fn u32(&self) -> PolarsResult<&UInt32Chunked> {
        match self.dtype() {
            DataType::UInt32 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const UInt32Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != UInt32").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype u64
    pub fn u64(&self) -> PolarsResult<&UInt64Chunked> {
        match self.dtype() {
            DataType::UInt64 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const UInt64Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != UInt64").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype bool
    pub fn bool(&self) -> PolarsResult<&BooleanChunked> {
        match self.dtype() {
            DataType::Boolean => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const BooleanChunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Boolean").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype utf8
    pub fn utf8(&self) -> PolarsResult<&Utf8Chunked> {
        match self.dtype() {
            DataType::Utf8 => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const Utf8Chunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Utf8").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype binary
    #[cfg(feature = "dtype-binary")]
    pub fn binary(&self) -> PolarsResult<&BinaryChunked> {
        match self.dtype() {
            DataType::Binary => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const BinaryChunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != binary").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype Time
    #[cfg(feature = "dtype-time")]
    pub fn time(&self) -> PolarsResult<&TimeChunked> {
        match self.dtype() {
            DataType::Time => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const TimeChunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Time").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype Date
    #[cfg(feature = "dtype-date")]
    pub fn date(&self) -> PolarsResult<&DateChunked> {
        match self.dtype() {
            DataType::Date => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const DateChunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Date").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype datetime
    #[cfg(feature = "dtype-datetime")]
    pub fn datetime(&self) -> PolarsResult<&DatetimeChunked> {
        match self.dtype() {
            DataType::Datetime(_, _) => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const DatetimeChunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Datetime").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype duration
    #[cfg(feature = "dtype-duration")]
    pub fn duration(&self) -> PolarsResult<&DurationChunked> {
        match self.dtype() {
            DataType::Duration(_) => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const DurationChunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Duration").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype list
    pub fn list(&self) -> PolarsResult<&ListChunked> {
        match self.dtype() {
            DataType::List(_) => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const ListChunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != List").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype categorical
    #[cfg(feature = "dtype-categorical")]
    pub fn categorical(&self) -> PolarsResult<&CategoricalChunked> {
        match self.dtype() {
            DataType::Categorical(_) => unsafe {
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const CategoricalChunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Categorical").into(),
            )),
        }
    }

    /// Unpack to ChunkedArray of dtype struct
    #[cfg(feature = "dtype-struct")]
    pub fn struct_(&self) -> PolarsResult<&StructChunked> {
        match self.dtype() {
            DataType::Struct(_) => unsafe {
                #[cfg(debug_assertions)]
                {
                    let any = self.as_any();
                    assert!(any.is::<StructChunked>());
                }
                // Safety
                // We just checked type
                Ok(&*(self.as_ref() as *const dyn SeriesTrait as *const StructChunked))
            },
            dt => Err(PolarsError::SchemaMisMatch(
                format!("Series of dtype: {dt:?} != Struct").into(),
            )),
        }
    }
}