polars_core/series/implementations/
null.rs1use std::any::Any;
2
3use polars_error::constants::LENGTH_LIMIT_MSG;
4
5use self::compare_inner::TotalOrdInner;
6use crate::prelude::compare_inner::{IntoTotalEqInner, TotalEqInner};
7use crate::prelude::*;
8use crate::series::private::{PrivateSeries, PrivateSeriesNumeric};
9use crate::series::*;
10
11impl Series {
12 pub fn new_null(name: PlSmallStr, len: usize) -> Series {
13 NullChunked::new(name, len).into_series()
14 }
15}
16
17#[derive(Clone)]
18pub struct NullChunked {
19 pub(crate) name: PlSmallStr,
20 length: IdxSize,
21 chunks: Vec<ArrayRef>,
24}
25
26impl NullChunked {
27 pub(crate) fn new(name: PlSmallStr, len: usize) -> Self {
28 Self {
29 name,
30 length: len as IdxSize,
31 chunks: vec![Box::new(arrow::array::NullArray::new(
32 ArrowDataType::Null,
33 len,
34 ))],
35 }
36 }
37}
38impl PrivateSeriesNumeric for NullChunked {
39 fn bit_repr(&self) -> Option<BitRepr> {
40 Some(BitRepr::Small(UInt32Chunked::full_null(
41 self.name.clone(),
42 self.len(),
43 )))
44 }
45}
46
47impl PrivateSeries for NullChunked {
48 fn compute_len(&mut self) {
49 fn inner(chunks: &[ArrayRef]) -> usize {
50 match chunks.len() {
51 1 => chunks[0].len(),
53 _ => chunks.iter().fold(0, |acc, arr| acc + arr.len()),
54 }
55 }
56 self.length = IdxSize::try_from(inner(&self.chunks)).expect(LENGTH_LIMIT_MSG);
57 }
58 fn _field(&self) -> Cow<Field> {
59 Cow::Owned(Field::new(self.name().clone(), DataType::Null))
60 }
61
62 #[allow(unused)]
63 fn _set_flags(&mut self, flags: StatisticsFlags) {}
64
65 fn _dtype(&self) -> &DataType {
66 &DataType::Null
67 }
68
69 #[cfg(feature = "zip_with")]
70 fn zip_with_same_type(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {
71 let len = match (self.len(), mask.len(), other.len()) {
72 (a, b, c) if a == b && b == c => a,
73 (1, a, b) | (a, 1, b) | (a, b, 1) if a == b => a,
74 (a, 1, 1) | (1, a, 1) | (1, 1, a) => a,
75 (_, 0, _) => 0,
76 _ => {
77 polars_bail!(ShapeMismatch: "shapes of `self`, `mask` and `other` are not suitable for `zip_with` operation")
78 },
79 };
80
81 Ok(Self::new(self.name().clone(), len).into_series())
82 }
83
84 fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {
85 IntoTotalEqInner::into_total_eq_inner(self)
86 }
87 fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {
88 invalid_operation_panic!(into_total_ord_inner, self)
89 }
90
91 fn subtract(&self, _rhs: &Series) -> PolarsResult<Series> {
92 null_arithmetic(self, _rhs, "subtract")
93 }
94
95 fn add_to(&self, _rhs: &Series) -> PolarsResult<Series> {
96 null_arithmetic(self, _rhs, "add_to")
97 }
98 fn multiply(&self, _rhs: &Series) -> PolarsResult<Series> {
99 null_arithmetic(self, _rhs, "multiply")
100 }
101 fn divide(&self, _rhs: &Series) -> PolarsResult<Series> {
102 null_arithmetic(self, _rhs, "divide")
103 }
104 fn remainder(&self, _rhs: &Series) -> PolarsResult<Series> {
105 null_arithmetic(self, _rhs, "remainder")
106 }
107
108 #[cfg(feature = "algorithm_group_by")]
109 fn group_tuples(&self, _multithreaded: bool, _sorted: bool) -> PolarsResult<GroupsType> {
110 Ok(if self.is_empty() {
111 GroupsType::default()
112 } else {
113 GroupsType::Slice {
114 groups: vec![[0, self.length]],
115 rolling: false,
116 }
117 })
118 }
119
120 #[cfg(feature = "algorithm_group_by")]
121 unsafe fn agg_list(&self, groups: &GroupsType) -> Series {
122 AggList::agg_list(self, groups)
123 }
124
125 fn _get_flags(&self) -> StatisticsFlags {
126 StatisticsFlags::empty()
127 }
128
129 fn vec_hash(&self, random_state: PlRandomState, buf: &mut Vec<u64>) -> PolarsResult<()> {
130 VecHash::vec_hash(self, random_state, buf)?;
131 Ok(())
132 }
133
134 fn vec_hash_combine(
135 &self,
136 build_hasher: PlRandomState,
137 hashes: &mut [u64],
138 ) -> PolarsResult<()> {
139 VecHash::vec_hash_combine(self, build_hasher, hashes)?;
140 Ok(())
141 }
142}
143
144fn null_arithmetic(lhs: &NullChunked, rhs: &Series, op: &str) -> PolarsResult<Series> {
145 let output_len = match (lhs.len(), rhs.len()) {
146 (1, len_r) => len_r,
147 (len_l, 1) => len_l,
148 (len_l, len_r) if len_l == len_r => len_l,
149 _ => polars_bail!(ComputeError: "Cannot {:?} two series of different lengths.", op),
150 };
151 Ok(NullChunked::new(lhs.name().clone(), output_len).into_series())
152}
153
154impl SeriesTrait for NullChunked {
155 fn name(&self) -> &PlSmallStr {
156 &self.name
157 }
158
159 fn rename(&mut self, name: PlSmallStr) {
160 self.name = name
161 }
162
163 fn chunks(&self) -> &Vec<ArrayRef> {
164 &self.chunks
165 }
166 unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
167 &mut self.chunks
168 }
169
170 fn chunk_lengths(&self) -> ChunkLenIter {
171 self.chunks.iter().map(|chunk| chunk.len())
172 }
173
174 fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {
175 Ok(NullChunked::new(self.name.clone(), indices.len()).into_series())
176 }
177
178 unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {
179 NullChunked::new(self.name.clone(), indices.len()).into_series()
180 }
181
182 fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {
183 Ok(NullChunked::new(self.name.clone(), indices.len()).into_series())
184 }
185
186 unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {
187 NullChunked::new(self.name.clone(), indices.len()).into_series()
188 }
189
190 fn len(&self) -> usize {
191 self.length as usize
192 }
193
194 fn has_nulls(&self) -> bool {
195 self.len() > 0
196 }
197
198 fn rechunk(&self) -> Series {
199 NullChunked::new(self.name.clone(), self.len()).into_series()
200 }
201
202 fn drop_nulls(&self) -> Series {
203 NullChunked::new(self.name.clone(), 0).into_series()
204 }
205
206 fn cast(&self, dtype: &DataType, _cast_options: CastOptions) -> PolarsResult<Series> {
207 Ok(Series::full_null(self.name.clone(), self.len(), dtype))
208 }
209
210 fn null_count(&self) -> usize {
211 self.len()
212 }
213
214 #[cfg(feature = "algorithm_group_by")]
215 fn unique(&self) -> PolarsResult<Series> {
216 let ca = NullChunked::new(self.name.clone(), self.n_unique().unwrap());
217 Ok(ca.into_series())
218 }
219
220 #[cfg(feature = "algorithm_group_by")]
221 fn n_unique(&self) -> PolarsResult<usize> {
222 let n = if self.is_empty() { 0 } else { 1 };
223 Ok(n)
224 }
225
226 #[cfg(feature = "algorithm_group_by")]
227 fn arg_unique(&self) -> PolarsResult<IdxCa> {
228 let idxs: Vec<IdxSize> = (0..self.n_unique().unwrap() as IdxSize).collect();
229 Ok(IdxCa::new(self.name().clone(), idxs))
230 }
231
232 fn new_from_index(&self, _index: usize, length: usize) -> Series {
233 NullChunked::new(self.name.clone(), length).into_series()
234 }
235
236 unsafe fn get_unchecked(&self, _index: usize) -> AnyValue {
237 AnyValue::Null
238 }
239
240 fn slice(&self, offset: i64, length: usize) -> Series {
241 let (chunks, len) = chunkops::slice(&self.chunks, offset, length, self.len());
242 NullChunked {
243 name: self.name.clone(),
244 length: len as IdxSize,
245 chunks,
246 }
247 .into_series()
248 }
249
250 fn split_at(&self, offset: i64) -> (Series, Series) {
251 let (l, r) = chunkops::split_at(self.chunks(), offset, self.len());
252 (
253 NullChunked {
254 name: self.name.clone(),
255 length: l.iter().map(|arr| arr.len() as IdxSize).sum(),
256 chunks: l,
257 }
258 .into_series(),
259 NullChunked {
260 name: self.name.clone(),
261 length: r.iter().map(|arr| arr.len() as IdxSize).sum(),
262 chunks: r,
263 }
264 .into_series(),
265 )
266 }
267
268 fn sort_with(&self, _options: SortOptions) -> PolarsResult<Series> {
269 Ok(self.clone().into_series())
270 }
271
272 fn arg_sort(&self, _options: SortOptions) -> IdxCa {
273 IdxCa::from_vec(self.name().clone(), (0..self.len() as IdxSize).collect())
274 }
275
276 fn is_null(&self) -> BooleanChunked {
277 BooleanChunked::full(self.name().clone(), true, self.len())
278 }
279
280 fn is_not_null(&self) -> BooleanChunked {
281 BooleanChunked::full(self.name().clone(), false, self.len())
282 }
283
284 fn reverse(&self) -> Series {
285 self.clone().into_series()
286 }
287
288 fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
289 let len = if self.is_empty() {
290 polars_ensure!(filter.len() <= 1, ShapeMismatch: "filter's length: {} differs from that of the series: 0", filter.len());
292 0
293 } else if filter.len() == 1 {
294 return match filter.get(0) {
295 Some(true) => Ok(self.clone().into_series()),
296 None | Some(false) => Ok(NullChunked::new(self.name.clone(), 0).into_series()),
297 };
298 } else {
299 polars_ensure!(filter.len() == self.len(), ShapeMismatch: "filter's length: {} differs from that of the series: {}", filter.len(), self.len());
300 filter.sum().unwrap_or(0) as usize
301 };
302 Ok(NullChunked::new(self.name.clone(), len).into_series())
303 }
304
305 fn shift(&self, _periods: i64) -> Series {
306 self.clone().into_series()
307 }
308
309 fn append(&mut self, other: &Series) -> PolarsResult<()> {
310 polars_ensure!(other.dtype() == &DataType::Null, ComputeError: "expected null dtype");
311 self.length += other.len() as IdxSize;
313 self.chunks.extend(other.chunks().iter().cloned());
314 Ok(())
315 }
316 fn append_owned(&mut self, mut other: Series) -> PolarsResult<()> {
317 polars_ensure!(other.dtype() == &DataType::Null, ComputeError: "expected null dtype");
318 let other: &mut NullChunked = other._get_inner_mut().as_any_mut().downcast_mut().unwrap();
320 self.length += other.len() as IdxSize;
321 self.chunks.extend(std::mem::take(&mut other.chunks));
322 Ok(())
323 }
324
325 fn extend(&mut self, other: &Series) -> PolarsResult<()> {
326 *self = NullChunked::new(self.name.clone(), self.len() + other.len());
327 Ok(())
328 }
329
330 fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
331 Arc::new(self.clone())
332 }
333
334 fn as_any(&self) -> &dyn Any {
335 self
336 }
337
338 fn as_any_mut(&mut self) -> &mut dyn Any {
339 self
340 }
341
342 fn as_phys_any(&self) -> &dyn Any {
343 self
344 }
345
346 fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
347 self as _
348 }
349}
350
351unsafe impl IntoSeries for NullChunked {
352 fn into_series(self) -> Series
353 where
354 Self: Sized,
355 {
356 Series(Arc::new(self))
357 }
358}