Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-core/src/series/implementations/object.rs
6940 views
1
use std::any::Any;
2
use std::borrow::Cow;
3
4
use self::compare_inner::TotalOrdInner;
5
use super::{BitRepr, StatisticsFlags, *};
6
use crate::chunked_array::cast::CastOptions;
7
use crate::chunked_array::object::PolarsObjectSafe;
8
use crate::chunked_array::ops::compare_inner::{IntoTotalEqInner, TotalEqInner};
9
use crate::prelude::*;
10
use crate::series::implementations::SeriesWrap;
11
use crate::series::private::{PrivateSeries, PrivateSeriesNumeric};
12
13
impl<T: PolarsObject> PrivateSeriesNumeric for SeriesWrap<ObjectChunked<T>> {
14
fn bit_repr(&self) -> Option<BitRepr> {
15
None
16
}
17
}
18
19
impl<T> PrivateSeries for SeriesWrap<ObjectChunked<T>>
20
where
21
T: PolarsObject,
22
{
23
fn get_list_builder(
24
&self,
25
_name: PlSmallStr,
26
_values_capacity: usize,
27
_list_capacity: usize,
28
) -> Box<dyn ListBuilderTrait> {
29
ObjectChunked::<T>::get_list_builder(_name, _values_capacity, _list_capacity)
30
}
31
32
fn compute_len(&mut self) {
33
self.0.compute_len()
34
}
35
36
fn _field(&self) -> Cow<'_, Field> {
37
Cow::Borrowed(self.0.ref_field())
38
}
39
40
fn _dtype(&self) -> &DataType {
41
self.0.dtype()
42
}
43
44
fn _set_flags(&mut self, flags: StatisticsFlags) {
45
self.0.set_flags(flags)
46
}
47
fn _get_flags(&self) -> StatisticsFlags {
48
self.0.get_flags()
49
}
50
unsafe fn agg_list(&self, groups: &GroupsType) -> Series {
51
self.0.agg_list(groups)
52
}
53
54
fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {
55
(&self.0).into_total_eq_inner()
56
}
57
fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {
58
invalid_operation_panic!(into_total_ord_inner, self)
59
}
60
61
fn vec_hash(
62
&self,
63
random_state: PlSeedableRandomStateQuality,
64
buf: &mut Vec<u64>,
65
) -> PolarsResult<()> {
66
self.0.vec_hash(random_state, buf)?;
67
Ok(())
68
}
69
70
fn vec_hash_combine(
71
&self,
72
build_hasher: PlSeedableRandomStateQuality,
73
hashes: &mut [u64],
74
) -> PolarsResult<()> {
75
self.0.vec_hash_combine(build_hasher, hashes)?;
76
Ok(())
77
}
78
79
#[cfg(feature = "algorithm_group_by")]
80
fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {
81
IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)
82
}
83
#[cfg(feature = "zip_with")]
84
fn zip_with_same_type(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {
85
self.0
86
.zip_with(mask, other.as_ref().as_ref())
87
.map(|ca| ca.into_series())
88
}
89
}
90
impl<T> SeriesTrait for SeriesWrap<ObjectChunked<T>>
91
where
92
T: PolarsObject,
93
{
94
fn rename(&mut self, name: PlSmallStr) {
95
ObjectChunked::rename(&mut self.0, name)
96
}
97
98
fn chunk_lengths(&self) -> ChunkLenIter<'_> {
99
ObjectChunked::chunk_lengths(&self.0)
100
}
101
102
fn name(&self) -> &PlSmallStr {
103
ObjectChunked::name(&self.0)
104
}
105
106
fn dtype(&self) -> &DataType {
107
ObjectChunked::dtype(&self.0)
108
}
109
110
fn chunks(&self) -> &Vec<ArrayRef> {
111
ObjectChunked::chunks(&self.0)
112
}
113
unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
114
self.0.chunks_mut()
115
}
116
117
fn slice(&self, offset: i64, length: usize) -> Series {
118
ObjectChunked::slice(&self.0, offset, length).into_series()
119
}
120
121
fn split_at(&self, offset: i64) -> (Series, Series) {
122
let (a, b) = ObjectChunked::split_at(&self.0, offset);
123
(a.into_series(), b.into_series())
124
}
125
126
fn append(&mut self, other: &Series) -> PolarsResult<()> {
127
polars_ensure!(self.dtype() == other.dtype(), append);
128
ObjectChunked::append(&mut self.0, other.as_ref().as_ref())
129
}
130
fn append_owned(&mut self, other: Series) -> PolarsResult<()> {
131
polars_ensure!(self.dtype() == other.dtype(), append);
132
ObjectChunked::append_owned(&mut self.0, other.take_inner())
133
}
134
135
fn extend(&mut self, _other: &Series) -> PolarsResult<()> {
136
polars_bail!(opq = extend, self.dtype());
137
}
138
139
fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
140
ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())
141
}
142
143
fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {
144
let ca = self.rechunk_object();
145
Ok(ca.take(indices)?.into_series())
146
}
147
148
unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {
149
let ca = self.rechunk_object();
150
ca.take_unchecked(indices).into_series()
151
}
152
153
fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {
154
Ok(self.0.take(indices)?.into_series())
155
}
156
157
unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {
158
self.0.take_unchecked(indices).into_series()
159
}
160
161
fn len(&self) -> usize {
162
ObjectChunked::len(&self.0)
163
}
164
165
fn rechunk(&self) -> Series {
166
// do not call normal rechunk
167
self.rechunk_object().into_series()
168
}
169
170
fn new_from_index(&self, index: usize, length: usize) -> Series {
171
ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()
172
}
173
174
fn cast(&self, dtype: &DataType, _cast_options: CastOptions) -> PolarsResult<Series> {
175
if matches!(dtype, DataType::Object(_)) {
176
Ok(self.0.clone().into_series())
177
} else {
178
Err(PolarsError::ComputeError(
179
"cannot cast 'Object' type".into(),
180
))
181
}
182
}
183
184
fn get(&self, index: usize) -> PolarsResult<AnyValue<'_>> {
185
ObjectChunked::get_any_value(&self.0, index)
186
}
187
unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {
188
ObjectChunked::get_any_value_unchecked(&self.0, index)
189
}
190
fn null_count(&self) -> usize {
191
ObjectChunked::null_count(&self.0)
192
}
193
194
fn has_nulls(&self) -> bool {
195
ObjectChunked::has_nulls(&self.0)
196
}
197
198
fn unique(&self) -> PolarsResult<Series> {
199
ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
200
}
201
202
fn n_unique(&self) -> PolarsResult<usize> {
203
ChunkUnique::n_unique(&self.0)
204
}
205
206
fn arg_unique(&self) -> PolarsResult<IdxCa> {
207
ChunkUnique::arg_unique(&self.0)
208
}
209
210
fn is_null(&self) -> BooleanChunked {
211
ObjectChunked::is_null(&self.0)
212
}
213
214
fn is_not_null(&self) -> BooleanChunked {
215
ObjectChunked::is_not_null(&self.0)
216
}
217
218
fn reverse(&self) -> Series {
219
ChunkReverse::reverse(&self.0).into_series()
220
}
221
222
fn shift(&self, periods: i64) -> Series {
223
ChunkShift::shift(&self.0, periods).into_series()
224
}
225
226
fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
227
Arc::new(SeriesWrap(Clone::clone(&self.0)))
228
}
229
230
fn get_object(&self, index: usize) -> Option<&dyn PolarsObjectSafe> {
231
ObjectChunked::<T>::get_object(&self.0, index)
232
}
233
234
unsafe fn get_object_chunked_unchecked(
235
&self,
236
chunk: usize,
237
index: usize,
238
) -> Option<&dyn PolarsObjectSafe> {
239
ObjectChunked::<T>::get_object_chunked_unchecked(&self.0, chunk, index)
240
}
241
242
fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {
243
self.0.find_validity_mismatch(other, idxs)
244
}
245
246
fn as_any(&self) -> &dyn Any {
247
&self.0
248
}
249
250
fn as_any_mut(&mut self) -> &mut dyn Any {
251
&mut self.0
252
}
253
254
fn as_phys_any(&self) -> &dyn Any {
255
self
256
}
257
258
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
259
self as _
260
}
261
}
262
263
#[cfg(test)]
264
mod test {
265
use super::*;
266
267
#[test]
268
fn test_downcast_object() -> PolarsResult<()> {
269
#[allow(non_local_definitions)]
270
impl PolarsObject for i32 {
271
fn type_name() -> &'static str {
272
"i32"
273
}
274
}
275
276
let ca = ObjectChunked::new_from_vec("a".into(), vec![0i32, 1, 2]);
277
let s = ca.into_series();
278
279
let ca = s.as_any().downcast_ref::<ObjectChunked<i32>>().unwrap();
280
assert_eq!(*ca.get(0).unwrap(), 0);
281
assert_eq!(*ca.get(1).unwrap(), 1);
282
assert_eq!(*ca.get(2).unwrap(), 2);
283
284
Ok(())
285
}
286
}
287
288