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