Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-core/src/series/ops/null.rs
6940 views
1
use arrow::bitmap::Bitmap;
2
use arrow::buffer::Buffer;
3
use arrow::offset::OffsetsBuffer;
4
5
#[cfg(feature = "object")]
6
use crate::chunked_array::object::registry::get_object_builder;
7
use crate::prelude::*;
8
9
impl 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.unwrap_or(0))
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
if !fields.is_empty() {
60
ca.with_outer_validity(Some(Bitmap::new_zeroed(size)))
61
.into_series()
62
} else {
63
ca.into_series()
64
}
65
},
66
DataType::BinaryOffset => {
67
let length = size;
68
69
let offsets = vec![0; size + 1];
70
let array = BinaryArray::<i64>::new(
71
dtype.to_arrow(CompatLevel::oldest()),
72
unsafe { OffsetsBuffer::new_unchecked(Buffer::from(offsets)) },
73
Buffer::default(),
74
Some(Bitmap::new_zeroed(size)),
75
);
76
77
unsafe {
78
BinaryOffsetChunked::new_with_dims(
79
Arc::new(Field::new(name, dtype.clone())),
80
vec![Box::new(array)],
81
length,
82
length,
83
)
84
}
85
.into_series()
86
},
87
DataType::Null => Series::new_null(name, size),
88
DataType::Unknown(kind) => {
89
let dtype = kind.materialize().unwrap_or(DataType::Null);
90
Series::full_null(name, size, &dtype)
91
},
92
#[cfg(feature = "object")]
93
DataType::Object(_) => {
94
let mut builder = get_object_builder(name, size);
95
for _ in 0..size {
96
builder.append_null();
97
}
98
builder.to_series()
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
}
118
119