polars_core/series/ops/
downcast.rs1#![allow(unsafe_op_in_unsafe_fn)]
2use crate::prelude::*;
3use crate::series::implementations::null::NullChunked;
4
5macro_rules! unpack_chunked_err {
6 ($series:expr => $name:expr) => {
7 polars_err!(SchemaMismatch: "invalid series dtype: expected `{}`, got `{}` for series with name `{}`", $name, $series.dtype(), $series.name())
8 };
9}
10
11macro_rules! try_unpack_chunked {
12 ($series:expr, $expected:pat $(if $guard: expr)? => $ca:ty) => {
13 match $series.dtype() {
14 $expected $(if $guard)? => {
15 #[cfg(debug_assertions)]
17 {
18 Some($series.as_ref().as_any().downcast_ref::<$ca>().unwrap())
19 }
20 #[cfg(not(debug_assertions))]
21 unsafe {
22 Some(&*($series.as_ref() as *const dyn SeriesTrait as *const $ca))
23 }
24 },
25 _ => None,
26 }
27 };
28}
29
30impl Series {
31 pub fn try_i8(&self) -> Option<&Int8Chunked> {
33 try_unpack_chunked!(self, DataType::Int8 => Int8Chunked)
34 }
35
36 pub fn try_i16(&self) -> Option<&Int16Chunked> {
38 try_unpack_chunked!(self, DataType::Int16 => Int16Chunked)
39 }
40
41 pub fn try_i32(&self) -> Option<&Int32Chunked> {
57 try_unpack_chunked!(self, DataType::Int32 => Int32Chunked)
58 }
59
60 pub fn try_i64(&self) -> Option<&Int64Chunked> {
62 try_unpack_chunked!(self, DataType::Int64 => Int64Chunked)
63 }
64
65 #[cfg(feature = "dtype-i128")]
67 pub fn try_i128(&self) -> Option<&Int128Chunked> {
68 try_unpack_chunked!(self, DataType::Int128 => Int128Chunked)
69 }
70
71 pub fn try_f32(&self) -> Option<&Float32Chunked> {
73 try_unpack_chunked!(self, DataType::Float32 => Float32Chunked)
74 }
75
76 pub fn try_f64(&self) -> Option<&Float64Chunked> {
78 try_unpack_chunked!(self, DataType::Float64 => Float64Chunked)
79 }
80
81 pub fn try_u8(&self) -> Option<&UInt8Chunked> {
83 try_unpack_chunked!(self, DataType::UInt8 => UInt8Chunked)
84 }
85
86 pub fn try_u16(&self) -> Option<&UInt16Chunked> {
88 try_unpack_chunked!(self, DataType::UInt16 => UInt16Chunked)
89 }
90
91 pub fn try_u32(&self) -> Option<&UInt32Chunked> {
93 try_unpack_chunked!(self, DataType::UInt32 => UInt32Chunked)
94 }
95
96 pub fn try_u64(&self) -> Option<&UInt64Chunked> {
98 try_unpack_chunked!(self, DataType::UInt64 => UInt64Chunked)
99 }
100
101 pub fn try_bool(&self) -> Option<&BooleanChunked> {
103 try_unpack_chunked!(self, DataType::Boolean => BooleanChunked)
104 }
105
106 pub fn try_str(&self) -> Option<&StringChunked> {
108 try_unpack_chunked!(self, DataType::String => StringChunked)
109 }
110
111 pub fn try_binary(&self) -> Option<&BinaryChunked> {
113 try_unpack_chunked!(self, DataType::Binary => BinaryChunked)
114 }
115
116 pub fn try_binary_offset(&self) -> Option<&BinaryOffsetChunked> {
118 try_unpack_chunked!(self, DataType::BinaryOffset => BinaryOffsetChunked)
119 }
120
121 #[cfg(feature = "dtype-time")]
123 pub fn try_time(&self) -> Option<&TimeChunked> {
124 try_unpack_chunked!(self, DataType::Time => TimeChunked)
125 }
126
127 #[cfg(feature = "dtype-date")]
129 pub fn try_date(&self) -> Option<&DateChunked> {
130 try_unpack_chunked!(self, DataType::Date => DateChunked)
131 }
132
133 #[cfg(feature = "dtype-datetime")]
135 pub fn try_datetime(&self) -> Option<&DatetimeChunked> {
136 try_unpack_chunked!(self, DataType::Datetime(_, _) => DatetimeChunked)
137 }
138
139 #[cfg(feature = "dtype-duration")]
141 pub fn try_duration(&self) -> Option<&DurationChunked> {
142 try_unpack_chunked!(self, DataType::Duration(_) => DurationChunked)
143 }
144
145 #[cfg(feature = "dtype-decimal")]
147 pub fn try_decimal(&self) -> Option<&DecimalChunked> {
148 try_unpack_chunked!(self, DataType::Decimal(_, _) => DecimalChunked)
149 }
150
151 pub fn try_list(&self) -> Option<&ListChunked> {
153 try_unpack_chunked!(self, DataType::List(_) => ListChunked)
154 }
155
156 #[cfg(feature = "dtype-array")]
158 pub fn try_array(&self) -> Option<&ArrayChunked> {
159 try_unpack_chunked!(self, DataType::Array(_, _) => ArrayChunked)
160 }
161
162 #[cfg(feature = "dtype-categorical")]
163 pub fn try_cat<T: PolarsCategoricalType>(&self) -> Option<&CategoricalChunked<T>> {
164 try_unpack_chunked!(self, dt @ DataType::Enum(_, _) | dt @ DataType::Categorical(_, _) if dt.cat_physical().unwrap() == T::physical() => CategoricalChunked<T>)
165 }
166
167 #[cfg(feature = "dtype-categorical")]
169 pub fn try_cat8(&self) -> Option<&Categorical8Chunked> {
170 self.try_cat::<Categorical8Type>()
171 }
172
173 #[cfg(feature = "dtype-categorical")]
174 pub fn try_cat16(&self) -> Option<&Categorical16Chunked> {
175 self.try_cat::<Categorical16Type>()
176 }
177
178 #[cfg(feature = "dtype-categorical")]
179 pub fn try_cat32(&self) -> Option<&Categorical32Chunked> {
180 self.try_cat::<Categorical32Type>()
181 }
182
183 #[cfg(feature = "dtype-struct")]
185 pub fn try_struct(&self) -> Option<&StructChunked> {
186 #[cfg(debug_assertions)]
187 {
188 if let DataType::Struct(_) = self.dtype() {
189 let any = self.as_any();
190 assert!(any.is::<StructChunked>());
191 }
192 }
193 try_unpack_chunked!(self, DataType::Struct(_) => StructChunked)
194 }
195
196 pub fn try_null(&self) -> Option<&NullChunked> {
198 try_unpack_chunked!(self, DataType::Null => NullChunked)
199 }
200 pub fn i8(&self) -> PolarsResult<&Int8Chunked> {
202 self.try_i8()
203 .ok_or_else(|| unpack_chunked_err!(self => "Int8"))
204 }
205
206 pub fn i16(&self) -> PolarsResult<&Int16Chunked> {
208 self.try_i16()
209 .ok_or_else(|| unpack_chunked_err!(self => "Int16"))
210 }
211
212 pub fn i32(&self) -> PolarsResult<&Int32Chunked> {
228 self.try_i32()
229 .ok_or_else(|| unpack_chunked_err!(self => "Int32"))
230 }
231
232 pub fn i64(&self) -> PolarsResult<&Int64Chunked> {
234 self.try_i64()
235 .ok_or_else(|| unpack_chunked_err!(self => "Int64"))
236 }
237
238 #[cfg(feature = "dtype-i128")]
240 pub fn i128(&self) -> PolarsResult<&Int128Chunked> {
241 self.try_i128()
242 .ok_or_else(|| unpack_chunked_err!(self => "Int128"))
243 }
244
245 pub fn f32(&self) -> PolarsResult<&Float32Chunked> {
247 self.try_f32()
248 .ok_or_else(|| unpack_chunked_err!(self => "Float32"))
249 }
250
251 pub fn f64(&self) -> PolarsResult<&Float64Chunked> {
253 self.try_f64()
254 .ok_or_else(|| unpack_chunked_err!(self => "Float64"))
255 }
256
257 pub fn u8(&self) -> PolarsResult<&UInt8Chunked> {
259 self.try_u8()
260 .ok_or_else(|| unpack_chunked_err!(self => "UInt8"))
261 }
262
263 pub fn u16(&self) -> PolarsResult<&UInt16Chunked> {
265 self.try_u16()
266 .ok_or_else(|| unpack_chunked_err!(self => "UInt16"))
267 }
268
269 pub fn u32(&self) -> PolarsResult<&UInt32Chunked> {
271 self.try_u32()
272 .ok_or_else(|| unpack_chunked_err!(self => "UInt32"))
273 }
274
275 pub fn u64(&self) -> PolarsResult<&UInt64Chunked> {
277 self.try_u64()
278 .ok_or_else(|| unpack_chunked_err!(self => "UInt64"))
279 }
280
281 pub fn bool(&self) -> PolarsResult<&BooleanChunked> {
283 self.try_bool()
284 .ok_or_else(|| unpack_chunked_err!(self => "Boolean"))
285 }
286
287 pub fn str(&self) -> PolarsResult<&StringChunked> {
289 self.try_str()
290 .ok_or_else(|| unpack_chunked_err!(self => "String"))
291 }
292
293 pub fn binary(&self) -> PolarsResult<&BinaryChunked> {
295 self.try_binary()
296 .ok_or_else(|| unpack_chunked_err!(self => "Binary"))
297 }
298
299 pub fn binary_offset(&self) -> PolarsResult<&BinaryOffsetChunked> {
301 self.try_binary_offset()
302 .ok_or_else(|| unpack_chunked_err!(self => "BinaryOffset"))
303 }
304
305 #[cfg(feature = "dtype-time")]
307 pub fn time(&self) -> PolarsResult<&TimeChunked> {
308 self.try_time()
309 .ok_or_else(|| unpack_chunked_err!(self => "Time"))
310 }
311
312 #[cfg(feature = "dtype-date")]
314 pub fn date(&self) -> PolarsResult<&DateChunked> {
315 self.try_date()
316 .ok_or_else(|| unpack_chunked_err!(self => "Date"))
317 }
318
319 #[cfg(feature = "dtype-datetime")]
321 pub fn datetime(&self) -> PolarsResult<&DatetimeChunked> {
322 self.try_datetime()
323 .ok_or_else(|| unpack_chunked_err!(self => "Datetime"))
324 }
325
326 #[cfg(feature = "dtype-duration")]
328 pub fn duration(&self) -> PolarsResult<&DurationChunked> {
329 self.try_duration()
330 .ok_or_else(|| unpack_chunked_err!(self => "Duration"))
331 }
332
333 #[cfg(feature = "dtype-decimal")]
335 pub fn decimal(&self) -> PolarsResult<&DecimalChunked> {
336 self.try_decimal()
337 .ok_or_else(|| unpack_chunked_err!(self => "Decimal"))
338 }
339
340 pub fn list(&self) -> PolarsResult<&ListChunked> {
342 self.try_list()
343 .ok_or_else(|| unpack_chunked_err!(self => "List"))
344 }
345
346 #[cfg(feature = "dtype-array")]
348 pub fn array(&self) -> PolarsResult<&ArrayChunked> {
349 self.try_array()
350 .ok_or_else(|| unpack_chunked_err!(self => "Array"))
351 }
352
353 #[cfg(feature = "dtype-categorical")]
355 pub fn cat<T: PolarsCategoricalType>(&self) -> PolarsResult<&CategoricalChunked<T>> {
356 self.try_cat::<T>()
357 .ok_or_else(|| unpack_chunked_err!(self => "Enum | Categorical"))
358 }
359
360 #[cfg(feature = "dtype-categorical")]
362 pub fn cat8(&self) -> PolarsResult<&CategoricalChunked<Categorical8Type>> {
363 self.try_cat8()
364 .ok_or_else(|| unpack_chunked_err!(self => "Enum8 | Categorical8"))
365 }
366
367 #[cfg(feature = "dtype-categorical")]
369 pub fn cat16(&self) -> PolarsResult<&CategoricalChunked<Categorical16Type>> {
370 self.try_cat16()
371 .ok_or_else(|| unpack_chunked_err!(self => "Enum16 | Categorical16"))
372 }
373
374 #[cfg(feature = "dtype-categorical")]
376 pub fn cat32(&self) -> PolarsResult<&CategoricalChunked<Categorical32Type>> {
377 self.try_cat32()
378 .ok_or_else(|| unpack_chunked_err!(self => "Enum32 | Categorical32"))
379 }
380
381 #[cfg(feature = "dtype-struct")]
383 pub fn struct_(&self) -> PolarsResult<&StructChunked> {
384 #[cfg(debug_assertions)]
385 {
386 if let DataType::Struct(_) = self.dtype() {
387 let any = self.as_any();
388 assert!(any.is::<StructChunked>());
389 }
390 }
391
392 self.try_struct()
393 .ok_or_else(|| unpack_chunked_err!(self => "Struct"))
394 }
395
396 pub fn null(&self) -> PolarsResult<&NullChunked> {
398 self.try_null()
399 .ok_or_else(|| unpack_chunked_err!(self => "Null"))
400 }
401}