Skip to main content

polars_core/series/ops/
null.rs

1use arrow::bitmap::Bitmap;
2use arrow::offset::OffsetsBuffer;
3use polars_buffer::Buffer;
4
5#[cfg(feature = "object")]
6use crate::chunked_array::object::registry::get_object_builder;
7use crate::prelude::*;
8
9impl Series {
10    pub fn full_null(name: PlSmallStr, size: usize, dtype: &DataType) -> Self {
11        // match the logical types and create them
12        match dtype {
13            DataType::List(inner_dtype) => {
14                ListChunked::full_null_with_dtype(name, size, inner_dtype).into_series()
15            },
16            #[cfg(feature = "dtype-array")]
17            DataType::Array(inner_dtype, width) => {
18                ArrayChunked::full_null_with_dtype(name, size, inner_dtype, *width).into_series()
19            },
20            #[cfg(feature = "dtype-categorical")]
21            dt @ (DataType::Categorical(_, _) | DataType::Enum(_, _)) => {
22                with_match_categorical_physical_type!(dt.cat_physical().unwrap(), |$C| {
23                    CategoricalChunked::<$C>::full_null_with_dtype(
24                        name,
25                        size,
26                        dtype.clone()
27                    )
28                        .into_series()
29                })
30            },
31            #[cfg(feature = "dtype-date")]
32            DataType::Date => Int32Chunked::full_null(name, size)
33                .into_date()
34                .into_series(),
35            #[cfg(feature = "dtype-datetime")]
36            DataType::Datetime(tu, tz) => Int64Chunked::full_null(name, size)
37                .into_datetime(*tu, tz.clone())
38                .into_series(),
39            #[cfg(feature = "dtype-duration")]
40            DataType::Duration(tu) => Int64Chunked::full_null(name, size)
41                .into_duration(*tu)
42                .into_series(),
43            #[cfg(feature = "dtype-time")]
44            DataType::Time => Int64Chunked::full_null(name, size)
45                .into_time()
46                .into_series(),
47            #[cfg(feature = "dtype-decimal")]
48            DataType::Decimal(precision, scale) => Int128Chunked::full_null(name, size)
49                .into_decimal_unchecked(*precision, *scale)
50                .into_series(),
51            #[cfg(feature = "dtype-struct")]
52            DataType::Struct(fields) => {
53                let fields = fields
54                    .iter()
55                    .map(|fld| Series::full_null(fld.name().clone(), size, fld.dtype()))
56                    .collect::<Vec<_>>();
57                let ca = StructChunked::from_series(name, size, fields.iter()).unwrap();
58
59                ca.with_outer_validity(Some(Bitmap::new_zeroed(size)))
60                    .into_series()
61            },
62            DataType::BinaryOffset => {
63                let length = size;
64
65                let offsets = vec![0; size + 1];
66                let array = BinaryArray::<i64>::new(
67                    dtype.to_arrow(CompatLevel::oldest()),
68                    unsafe { OffsetsBuffer::new_unchecked(Buffer::from(offsets)) },
69                    Buffer::default(),
70                    Some(Bitmap::new_zeroed(size)),
71                );
72
73                unsafe {
74                    BinaryOffsetChunked::new_with_dims(
75                        Arc::new(Field::new(name, dtype.clone())),
76                        vec![Box::new(array)],
77                        length,
78                        length,
79                    )
80                }
81                .into_series()
82            },
83            DataType::Null => Series::new_null(name, size),
84            DataType::Unknown(kind) => {
85                let dtype = kind.materialize().unwrap_or(DataType::Null);
86                Series::full_null(name, size, &dtype)
87            },
88            #[cfg(feature = "object")]
89            DataType::Object(_) => {
90                let mut builder = get_object_builder(name, size);
91                for _ in 0..size {
92                    builder.append_null();
93                }
94                builder.to_series()
95            },
96            #[cfg(feature = "dtype-extension")]
97            DataType::Extension(typ, storage_dtype) => {
98                Series::full_null(name, size, storage_dtype).into_extension(typ.clone())
99            },
100            _ => {
101                macro_rules! primitive {
102                    ($type:ty) => {{ ChunkedArray::<$type>::full_null(name, size).into_series() }};
103                }
104                macro_rules! bool {
105                    () => {{ ChunkedArray::<BooleanType>::full_null(name, size).into_series() }};
106                }
107                macro_rules! string {
108                    () => {{ ChunkedArray::<StringType>::full_null(name, size).into_series() }};
109                }
110                macro_rules! binary {
111                    () => {{ ChunkedArray::<BinaryType>::full_null(name, size).into_series() }};
112                }
113                match_dtype_to_logical_apply_macro!(dtype, primitive, string, binary, bool)
114            },
115        }
116    }
117}