polars-df 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -0
- data/Cargo.lock +447 -410
- data/Cargo.toml +0 -1
- data/README.md +6 -5
- data/ext/polars/Cargo.toml +10 -5
- data/ext/polars/src/apply/dataframe.rs +2 -2
- data/ext/polars/src/{lazy/apply.rs → apply/lazy.rs} +1 -2
- data/ext/polars/src/apply/mod.rs +8 -3
- data/ext/polars/src/batched_csv.rs +7 -5
- data/ext/polars/src/conversion.rs +269 -59
- data/ext/polars/src/dataframe.rs +38 -40
- data/ext/polars/src/error.rs +6 -2
- data/ext/polars/src/expr/array.rs +15 -0
- data/ext/polars/src/expr/binary.rs +69 -0
- data/ext/polars/src/expr/categorical.rs +10 -0
- data/ext/polars/src/expr/datetime.rs +223 -0
- data/ext/polars/src/expr/general.rs +963 -0
- data/ext/polars/src/expr/list.rs +151 -0
- data/ext/polars/src/{lazy → expr}/meta.rs +16 -6
- data/ext/polars/src/expr/string.rs +314 -0
- data/ext/polars/src/expr/struct.rs +15 -0
- data/ext/polars/src/expr.rs +34 -0
- data/ext/polars/src/functions/eager.rs +93 -0
- data/ext/polars/src/functions/io.rs +34 -0
- data/ext/polars/src/functions/lazy.rs +249 -0
- data/ext/polars/src/functions/meta.rs +8 -0
- data/ext/polars/src/functions/mod.rs +5 -0
- data/ext/polars/src/functions/whenthen.rs +43 -0
- data/ext/polars/src/{lazy/dataframe.rs → lazyframe.rs} +26 -35
- data/ext/polars/src/lazygroupby.rs +29 -0
- data/ext/polars/src/lib.rs +223 -316
- data/ext/polars/src/object.rs +1 -1
- data/ext/polars/src/rb_modules.rs +12 -0
- data/ext/polars/src/series/aggregation.rs +83 -0
- data/ext/polars/src/series/arithmetic.rs +88 -0
- data/ext/polars/src/series/comparison.rs +251 -0
- data/ext/polars/src/series/construction.rs +190 -0
- data/ext/polars/src/series.rs +151 -551
- data/lib/polars/array_expr.rb +84 -0
- data/lib/polars/array_name_space.rb +77 -0
- data/lib/polars/batched_csv_reader.rb +1 -1
- data/lib/polars/convert.rb +2 -2
- data/lib/polars/data_frame.rb +289 -96
- data/lib/polars/data_types.rb +169 -33
- data/lib/polars/date_time_expr.rb +142 -2
- data/lib/polars/date_time_name_space.rb +17 -3
- data/lib/polars/expr.rb +145 -78
- data/lib/polars/functions.rb +0 -1
- data/lib/polars/group_by.rb +1 -22
- data/lib/polars/lazy_frame.rb +84 -31
- data/lib/polars/lazy_functions.rb +71 -32
- data/lib/polars/list_expr.rb +94 -45
- data/lib/polars/list_name_space.rb +13 -13
- data/lib/polars/rolling_group_by.rb +4 -2
- data/lib/polars/series.rb +249 -87
- data/lib/polars/string_expr.rb +277 -45
- data/lib/polars/string_name_space.rb +137 -22
- data/lib/polars/struct_name_space.rb +32 -0
- data/lib/polars/utils.rb +138 -54
- data/lib/polars/version.rb +1 -1
- data/lib/polars.rb +5 -2
- metadata +29 -11
- data/ext/polars/src/lazy/dsl.rs +0 -1775
- data/ext/polars/src/lazy/mod.rs +0 -5
- data/ext/polars/src/lazy/utils.rs +0 -13
- data/ext/polars/src/list_construction.rs +0 -100
- /data/ext/polars/src/{numo.rs → series/export.rs} +0 -0
- /data/ext/polars/src/{set.rs → series/set_at_idx.rs} +0 -0
data/ext/polars/src/series.rs
CHANGED
@@ -1,4 +1,11 @@
|
|
1
|
-
|
1
|
+
mod aggregation;
|
2
|
+
mod arithmetic;
|
3
|
+
mod comparison;
|
4
|
+
mod construction;
|
5
|
+
mod export;
|
6
|
+
mod set_at_idx;
|
7
|
+
|
8
|
+
use magnus::{exception, Error, IntoValue, RArray, Value, QNIL};
|
2
9
|
use polars::prelude::*;
|
3
10
|
use polars::series::IsSorted;
|
4
11
|
use std::cell::RefCell;
|
@@ -6,9 +13,8 @@ use std::cell::RefCell;
|
|
6
13
|
use crate::apply::series::{call_lambda_and_extract, ApplyLambda};
|
7
14
|
use crate::apply_method_all_arrow_series2;
|
8
15
|
use crate::conversion::*;
|
9
|
-
use crate::
|
10
|
-
use crate::
|
11
|
-
use crate::{RbDataFrame, RbPolarsErr, RbResult, RbValueError};
|
16
|
+
use crate::series::set_at_idx::set_at_idx;
|
17
|
+
use crate::{RbDataFrame, RbPolarsErr, RbResult};
|
12
18
|
|
13
19
|
#[magnus::wrap(class = "Polars::RbSeries")]
|
14
20
|
pub struct RbSeries {
|
@@ -27,121 +33,46 @@ impl RbSeries {
|
|
27
33
|
series: RefCell::new(series),
|
28
34
|
}
|
29
35
|
}
|
30
|
-
|
31
|
-
pub fn is_sorted_flag(&self) -> bool {
|
32
|
-
matches!(self.series.borrow().is_sorted_flag(), IsSorted::Ascending)
|
33
|
-
}
|
34
|
-
|
35
|
-
pub fn is_sorted_reverse_flag(&self) -> bool {
|
36
|
-
matches!(self.series.borrow().is_sorted_flag(), IsSorted::Descending)
|
37
|
-
}
|
38
|
-
|
39
|
-
pub fn new_opt_bool(name: String, obj: RArray, strict: bool) -> RbResult<RbSeries> {
|
40
|
-
let len = obj.len();
|
41
|
-
let mut builder = BooleanChunkedBuilder::new(&name, len);
|
42
|
-
|
43
|
-
unsafe {
|
44
|
-
for item in obj.as_slice().iter() {
|
45
|
-
if item.is_nil() {
|
46
|
-
builder.append_null()
|
47
|
-
} else {
|
48
|
-
match item.try_convert::<bool>() {
|
49
|
-
Ok(val) => builder.append_value(val),
|
50
|
-
Err(e) => {
|
51
|
-
if strict {
|
52
|
-
return Err(e);
|
53
|
-
}
|
54
|
-
builder.append_null()
|
55
|
-
}
|
56
|
-
}
|
57
|
-
}
|
58
|
-
}
|
59
|
-
}
|
60
|
-
let ca = builder.finish();
|
61
|
-
|
62
|
-
let s = ca.into_series();
|
63
|
-
Ok(RbSeries::new(s))
|
64
|
-
}
|
65
36
|
}
|
66
37
|
|
67
|
-
fn
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
T::Native: magnus::TryConvert,
|
72
|
-
{
|
73
|
-
let len = obj.len();
|
74
|
-
let mut builder = PrimitiveChunkedBuilder::<T>::new(name, len);
|
75
|
-
|
76
|
-
unsafe {
|
77
|
-
for item in obj.as_slice().iter() {
|
78
|
-
if item.is_nil() {
|
79
|
-
builder.append_null()
|
80
|
-
} else {
|
81
|
-
match item.try_convert::<T::Native>() {
|
82
|
-
Ok(val) => builder.append_value(val),
|
83
|
-
Err(e) => {
|
84
|
-
if strict {
|
85
|
-
return Err(e);
|
86
|
-
}
|
87
|
-
builder.append_null()
|
88
|
-
}
|
89
|
-
}
|
90
|
-
}
|
91
|
-
}
|
38
|
+
pub fn to_series_collection(rs: RArray) -> RbResult<Vec<Series>> {
|
39
|
+
let mut series = Vec::new();
|
40
|
+
for item in rs.each() {
|
41
|
+
series.push(item?.try_convert::<&RbSeries>()?.series.borrow().clone());
|
92
42
|
}
|
93
|
-
|
94
|
-
|
95
|
-
let s = ca.into_series();
|
96
|
-
Ok(RbSeries::new(s))
|
43
|
+
Ok(series)
|
97
44
|
}
|
98
45
|
|
99
|
-
|
100
|
-
|
101
|
-
($name:ident, $type:ty, $native: ty) => {
|
102
|
-
impl RbSeries {
|
103
|
-
pub fn $name(name: String, obj: RArray, strict: bool) -> RbResult<Self> {
|
104
|
-
new_primitive::<$type>(&name, obj, strict)
|
105
|
-
}
|
106
|
-
}
|
107
|
-
};
|
46
|
+
pub fn to_rbseries_collection(s: Vec<Series>) -> RArray {
|
47
|
+
RArray::from_iter(s.into_iter().map(RbSeries::new))
|
108
48
|
}
|
109
49
|
|
110
|
-
init_method_opt!(new_opt_u8, UInt8Type, u8);
|
111
|
-
init_method_opt!(new_opt_u16, UInt16Type, u16);
|
112
|
-
init_method_opt!(new_opt_u32, UInt32Type, u32);
|
113
|
-
init_method_opt!(new_opt_u64, UInt64Type, u64);
|
114
|
-
init_method_opt!(new_opt_i8, Int8Type, i8);
|
115
|
-
init_method_opt!(new_opt_i16, Int16Type, i16);
|
116
|
-
init_method_opt!(new_opt_i32, Int32Type, i32);
|
117
|
-
init_method_opt!(new_opt_i64, Int64Type, i64);
|
118
|
-
init_method_opt!(new_opt_f32, Float32Type, f32);
|
119
|
-
init_method_opt!(new_opt_f64, Float64Type, f64);
|
120
|
-
|
121
50
|
impl RbSeries {
|
122
|
-
pub fn
|
123
|
-
let
|
124
|
-
|
125
|
-
|
51
|
+
pub fn struct_unnest(&self) -> RbResult<RbDataFrame> {
|
52
|
+
let binding = self.series.borrow();
|
53
|
+
let ca = binding.struct_().map_err(RbPolarsErr::from)?;
|
54
|
+
let df: DataFrame = ca.clone().into();
|
55
|
+
Ok(df.into())
|
126
56
|
}
|
127
57
|
|
128
|
-
pub fn
|
129
|
-
|
130
|
-
|
131
|
-
|
58
|
+
// pub fn struct_fields(&self) -> RbResult<Vec<&str>> {
|
59
|
+
// let ca = self.series.borrow().struct_().map_err(RbPolarsErr::from)?;
|
60
|
+
// Ok(ca.fields().iter().map(|s| s.name()).collect())
|
61
|
+
// }
|
62
|
+
|
63
|
+
pub fn is_sorted_ascending_flag(&self) -> bool {
|
64
|
+
matches!(self.series.borrow().is_sorted_flag(), IsSorted::Ascending)
|
132
65
|
}
|
133
66
|
|
134
|
-
pub fn
|
135
|
-
|
136
|
-
.each()
|
137
|
-
.map(|v| v.map(ObjectValue::from))
|
138
|
-
.collect::<RbResult<Vec<ObjectValue>>>()?;
|
139
|
-
let s = ObjectChunked::<ObjectValue>::new_from_vec(&name, val).into_series();
|
140
|
-
Ok(s.into())
|
67
|
+
pub fn is_sorted_descending_flag(&self) -> bool {
|
68
|
+
matches!(self.series.borrow().is_sorted_flag(), IsSorted::Descending)
|
141
69
|
}
|
142
70
|
|
143
|
-
pub fn
|
144
|
-
|
71
|
+
pub fn can_fast_explode_flag(&self) -> bool {
|
72
|
+
match self.series.borrow().list() {
|
73
|
+
Err(_) => false,
|
74
|
+
Ok(list) => list._can_fast_explode(),
|
75
|
+
}
|
145
76
|
}
|
146
77
|
|
147
78
|
pub fn estimated_size(&self) -> usize {
|
@@ -232,9 +163,9 @@ impl RbSeries {
|
|
232
163
|
.map(|dt| Wrap(dt.clone()).into_value())
|
233
164
|
}
|
234
165
|
|
235
|
-
pub fn
|
166
|
+
pub fn set_sorted_flag(&self, descending: bool) -> Self {
|
236
167
|
let mut out = self.series.borrow().clone();
|
237
|
-
if
|
168
|
+
if descending {
|
238
169
|
out.set_sorted_flag(IsSorted::Descending);
|
239
170
|
} else {
|
240
171
|
out.set_sorted_flag(IsSorted::Ascending)
|
@@ -242,49 +173,6 @@ impl RbSeries {
|
|
242
173
|
out.into()
|
243
174
|
}
|
244
175
|
|
245
|
-
pub fn mean(&self) -> Option<f64> {
|
246
|
-
match self.series.borrow().dtype() {
|
247
|
-
DataType::Boolean => {
|
248
|
-
let s = self.series.borrow().cast(&DataType::UInt8).unwrap();
|
249
|
-
s.mean()
|
250
|
-
}
|
251
|
-
_ => self.series.borrow().mean(),
|
252
|
-
}
|
253
|
-
}
|
254
|
-
|
255
|
-
pub fn max(&self) -> RbResult<Value> {
|
256
|
-
Ok(Wrap(
|
257
|
-
self.series
|
258
|
-
.borrow()
|
259
|
-
.max_as_series()
|
260
|
-
.get(0)
|
261
|
-
.map_err(RbPolarsErr::from)?,
|
262
|
-
)
|
263
|
-
.into_value())
|
264
|
-
}
|
265
|
-
|
266
|
-
pub fn min(&self) -> RbResult<Value> {
|
267
|
-
Ok(Wrap(
|
268
|
-
self.series
|
269
|
-
.borrow()
|
270
|
-
.min_as_series()
|
271
|
-
.get(0)
|
272
|
-
.map_err(RbPolarsErr::from)?,
|
273
|
-
)
|
274
|
-
.into_value())
|
275
|
-
}
|
276
|
-
|
277
|
-
pub fn sum(&self) -> RbResult<Value> {
|
278
|
-
Ok(Wrap(
|
279
|
-
self.series
|
280
|
-
.borrow()
|
281
|
-
.sum_as_series()
|
282
|
-
.get(0)
|
283
|
-
.map_err(RbPolarsErr::from)?,
|
284
|
-
)
|
285
|
-
.into_value())
|
286
|
-
}
|
287
|
-
|
288
176
|
pub fn n_chunks(&self) -> usize {
|
289
177
|
self.series.borrow().n_chunks()
|
290
178
|
}
|
@@ -328,26 +216,6 @@ impl RbSeries {
|
|
328
216
|
}
|
329
217
|
}
|
330
218
|
|
331
|
-
pub fn add(&self, other: &RbSeries) -> Self {
|
332
|
-
(&*self.series.borrow() + &*other.series.borrow()).into()
|
333
|
-
}
|
334
|
-
|
335
|
-
pub fn sub(&self, other: &RbSeries) -> Self {
|
336
|
-
(&*self.series.borrow() - &*other.series.borrow()).into()
|
337
|
-
}
|
338
|
-
|
339
|
-
pub fn mul(&self, other: &RbSeries) -> Self {
|
340
|
-
(&*self.series.borrow() * &*other.series.borrow()).into()
|
341
|
-
}
|
342
|
-
|
343
|
-
pub fn div(&self, other: &RbSeries) -> Self {
|
344
|
-
(&*self.series.borrow() / &*other.series.borrow()).into()
|
345
|
-
}
|
346
|
-
|
347
|
-
pub fn rem(&self, other: &RbSeries) -> Self {
|
348
|
-
(&*self.series.borrow() % &*other.series.borrow()).into()
|
349
|
-
}
|
350
|
-
|
351
219
|
pub fn sort(&self, reverse: bool) -> Self {
|
352
220
|
(self.series.borrow_mut().sort(reverse)).into()
|
353
221
|
}
|
@@ -361,14 +229,6 @@ impl RbSeries {
|
|
361
229
|
Ok(df.into())
|
362
230
|
}
|
363
231
|
|
364
|
-
pub fn arg_min(&self) -> Option<usize> {
|
365
|
-
self.series.borrow().arg_min()
|
366
|
-
}
|
367
|
-
|
368
|
-
pub fn arg_max(&self) -> Option<usize> {
|
369
|
-
self.series.borrow().arg_max()
|
370
|
-
}
|
371
|
-
|
372
232
|
pub fn take_with_series(&self, indices: &RbSeries) -> RbResult<Self> {
|
373
233
|
let binding = indices.series.borrow();
|
374
234
|
let idx = binding.idx().map_err(RbPolarsErr::from)?;
|
@@ -426,60 +286,6 @@ impl RbSeries {
|
|
426
286
|
}
|
427
287
|
}
|
428
288
|
|
429
|
-
pub fn eq(&self, rhs: &RbSeries) -> RbResult<Self> {
|
430
|
-
let s = self
|
431
|
-
.series
|
432
|
-
.borrow()
|
433
|
-
.equal(&*rhs.series.borrow())
|
434
|
-
.map_err(RbPolarsErr::from)?;
|
435
|
-
Ok(Self::new(s.into_series()))
|
436
|
-
}
|
437
|
-
|
438
|
-
pub fn neq(&self, rhs: &RbSeries) -> RbResult<Self> {
|
439
|
-
let s = self
|
440
|
-
.series
|
441
|
-
.borrow()
|
442
|
-
.not_equal(&*rhs.series.borrow())
|
443
|
-
.map_err(RbPolarsErr::from)?;
|
444
|
-
Ok(Self::new(s.into_series()))
|
445
|
-
}
|
446
|
-
|
447
|
-
pub fn gt(&self, rhs: &RbSeries) -> RbResult<Self> {
|
448
|
-
let s = self
|
449
|
-
.series
|
450
|
-
.borrow()
|
451
|
-
.gt(&*rhs.series.borrow())
|
452
|
-
.map_err(RbPolarsErr::from)?;
|
453
|
-
Ok(Self::new(s.into_series()))
|
454
|
-
}
|
455
|
-
|
456
|
-
pub fn gt_eq(&self, rhs: &RbSeries) -> RbResult<Self> {
|
457
|
-
let s = self
|
458
|
-
.series
|
459
|
-
.borrow()
|
460
|
-
.gt_eq(&*rhs.series.borrow())
|
461
|
-
.map_err(RbPolarsErr::from)?;
|
462
|
-
Ok(Self::new(s.into_series()))
|
463
|
-
}
|
464
|
-
|
465
|
-
pub fn lt(&self, rhs: &RbSeries) -> RbResult<Self> {
|
466
|
-
let s = self
|
467
|
-
.series
|
468
|
-
.borrow()
|
469
|
-
.lt(&*rhs.series.borrow())
|
470
|
-
.map_err(RbPolarsErr::from)?;
|
471
|
-
Ok(Self::new(s.into_series()))
|
472
|
-
}
|
473
|
-
|
474
|
-
pub fn lt_eq(&self, rhs: &RbSeries) -> RbResult<Self> {
|
475
|
-
let s = self
|
476
|
-
.series
|
477
|
-
.borrow()
|
478
|
-
.lt_eq(&*rhs.series.borrow())
|
479
|
-
.map_err(RbPolarsErr::from)?;
|
480
|
-
Ok(Self::new(s.into_series()))
|
481
|
-
}
|
482
|
-
|
483
289
|
pub fn not(&self) -> RbResult<Self> {
|
484
290
|
let binding = self.series.borrow();
|
485
291
|
let bool = binding.bool().map_err(RbPolarsErr::from)?;
|
@@ -494,85 +300,131 @@ impl RbSeries {
|
|
494
300
|
self.series.borrow().len()
|
495
301
|
}
|
496
302
|
|
497
|
-
pub fn to_a(&self) ->
|
303
|
+
pub fn to_a(&self) -> Value {
|
498
304
|
let series = &self.series.borrow();
|
499
305
|
|
500
|
-
fn
|
306
|
+
fn to_a_recursive(series: &Series) -> Value {
|
501
307
|
let rblist = match series.dtype() {
|
502
|
-
DataType::Boolean => RArray::from_iter(series.bool().unwrap()),
|
503
|
-
DataType::UInt8 => RArray::from_iter(series.u8().unwrap()),
|
504
|
-
DataType::UInt16 => RArray::from_iter(series.u16().unwrap()),
|
505
|
-
DataType::UInt32 => RArray::from_iter(series.u32().unwrap()),
|
506
|
-
DataType::UInt64 => RArray::from_iter(series.u64().unwrap()),
|
507
|
-
DataType::Int8 => RArray::from_iter(series.i8().unwrap()),
|
508
|
-
DataType::Int16 => RArray::from_iter(series.i16().unwrap()),
|
509
|
-
DataType::Int32 => RArray::from_iter(series.i32().unwrap()),
|
510
|
-
DataType::Int64 => RArray::from_iter(series.i64().unwrap()),
|
511
|
-
DataType::Float32 => RArray::from_iter(series.f32().unwrap()),
|
512
|
-
DataType::Float64 => RArray::from_iter(series.f64().unwrap()),
|
308
|
+
DataType::Boolean => RArray::from_iter(series.bool().unwrap()).into_value(),
|
309
|
+
DataType::UInt8 => RArray::from_iter(series.u8().unwrap()).into_value(),
|
310
|
+
DataType::UInt16 => RArray::from_iter(series.u16().unwrap()).into_value(),
|
311
|
+
DataType::UInt32 => RArray::from_iter(series.u32().unwrap()).into_value(),
|
312
|
+
DataType::UInt64 => RArray::from_iter(series.u64().unwrap()).into_value(),
|
313
|
+
DataType::Int8 => RArray::from_iter(series.i8().unwrap()).into_value(),
|
314
|
+
DataType::Int16 => RArray::from_iter(series.i16().unwrap()).into_value(),
|
315
|
+
DataType::Int32 => RArray::from_iter(series.i32().unwrap()).into_value(),
|
316
|
+
DataType::Int64 => RArray::from_iter(series.i64().unwrap()).into_value(),
|
317
|
+
DataType::Float32 => RArray::from_iter(series.f32().unwrap()).into_value(),
|
318
|
+
DataType::Float64 => RArray::from_iter(series.f64().unwrap()).into_value(),
|
513
319
|
DataType::Categorical(_) => {
|
514
|
-
RArray::from_iter(series.categorical().unwrap().iter_str())
|
320
|
+
RArray::from_iter(series.categorical().unwrap().iter_str()).into_value()
|
515
321
|
}
|
516
|
-
DataType::
|
517
|
-
let
|
518
|
-
for
|
519
|
-
|
322
|
+
DataType::Object(_) => {
|
323
|
+
let v = RArray::with_capacity(series.len());
|
324
|
+
for i in 0..series.len() {
|
325
|
+
let obj: Option<&ObjectValue> = series.get_object(i).map(|any| any.into());
|
326
|
+
match obj {
|
327
|
+
Some(val) => v.push(val.to_object()).unwrap(),
|
328
|
+
None => v.push(QNIL).unwrap(),
|
329
|
+
};
|
520
330
|
}
|
521
|
-
|
331
|
+
v.into_value()
|
522
332
|
}
|
523
|
-
DataType::
|
524
|
-
let
|
525
|
-
|
526
|
-
|
333
|
+
DataType::List(_) => {
|
334
|
+
let v = RArray::new();
|
335
|
+
let ca = series.list().unwrap();
|
336
|
+
for opt_s in ca.amortized_iter() {
|
337
|
+
match opt_s {
|
338
|
+
None => {
|
339
|
+
v.push(QNIL).unwrap();
|
340
|
+
}
|
341
|
+
Some(s) => {
|
342
|
+
let rblst = to_a_recursive(s.as_ref());
|
343
|
+
v.push(rblst).unwrap();
|
344
|
+
}
|
345
|
+
}
|
346
|
+
}
|
347
|
+
v.into_value()
|
348
|
+
}
|
349
|
+
DataType::Array(_, _) => {
|
350
|
+
let v = RArray::new();
|
351
|
+
let ca = series.array().unwrap();
|
352
|
+
for opt_s in ca.amortized_iter() {
|
353
|
+
match opt_s {
|
354
|
+
None => {
|
355
|
+
v.push(QNIL).unwrap();
|
356
|
+
}
|
357
|
+
Some(s) => {
|
358
|
+
let rblst = to_a_recursive(s.as_ref());
|
359
|
+
v.push(rblst).unwrap();
|
360
|
+
}
|
361
|
+
}
|
527
362
|
}
|
528
|
-
|
363
|
+
v.into_value()
|
364
|
+
}
|
365
|
+
DataType::Date => {
|
366
|
+
let ca = series.date().unwrap();
|
367
|
+
return Wrap(ca).into_value();
|
368
|
+
}
|
369
|
+
DataType::Time => {
|
370
|
+
let ca = series.time().unwrap();
|
371
|
+
return Wrap(ca).into_value();
|
372
|
+
}
|
373
|
+
DataType::Datetime(_, _) => {
|
374
|
+
let ca = series.datetime().unwrap();
|
375
|
+
return Wrap(ca).into_value();
|
376
|
+
}
|
377
|
+
DataType::Decimal(_, _) => {
|
378
|
+
let ca = series.decimal().unwrap();
|
379
|
+
return Wrap(ca).into_value();
|
529
380
|
}
|
530
381
|
DataType::Utf8 => {
|
531
382
|
let ca = series.utf8().unwrap();
|
532
|
-
return
|
383
|
+
return Wrap(ca).into_value();
|
384
|
+
}
|
385
|
+
DataType::Struct(_) => {
|
386
|
+
let ca = series.struct_().unwrap();
|
387
|
+
return Wrap(ca).into_value();
|
388
|
+
}
|
389
|
+
DataType::Duration(_) => {
|
390
|
+
let ca = series.duration().unwrap();
|
391
|
+
return Wrap(ca).into_value();
|
533
392
|
}
|
534
393
|
DataType::Binary => {
|
535
|
-
let
|
536
|
-
|
537
|
-
|
394
|
+
let ca = series.binary().unwrap();
|
395
|
+
return Wrap(ca).into_value();
|
396
|
+
}
|
397
|
+
DataType::Null => {
|
398
|
+
let null: Option<u8> = None;
|
399
|
+
let n = series.len();
|
400
|
+
let iter = std::iter::repeat(null).take(n);
|
401
|
+
use std::iter::{Repeat, Take};
|
402
|
+
struct NullIter {
|
403
|
+
iter: Take<Repeat<Option<u8>>>,
|
404
|
+
n: usize,
|
405
|
+
}
|
406
|
+
impl Iterator for NullIter {
|
407
|
+
type Item = Option<u8>;
|
408
|
+
|
409
|
+
fn next(&mut self) -> Option<Self::Item> {
|
410
|
+
self.iter.next()
|
411
|
+
}
|
412
|
+
fn size_hint(&self) -> (usize, Option<usize>) {
|
413
|
+
(self.n, Some(self.n))
|
414
|
+
}
|
538
415
|
}
|
539
|
-
|
416
|
+
impl ExactSizeIterator for NullIter {}
|
417
|
+
|
418
|
+
RArray::from_iter(NullIter { iter, n }).into_value()
|
540
419
|
}
|
541
|
-
DataType::
|
420
|
+
DataType::Unknown => {
|
542
421
|
panic!("to_a not implemented for null/unknown")
|
543
422
|
}
|
544
|
-
_ => todo!(),
|
545
423
|
};
|
546
424
|
rblist
|
547
425
|
}
|
548
426
|
|
549
|
-
|
550
|
-
}
|
551
|
-
|
552
|
-
pub fn median(&self) -> Option<f64> {
|
553
|
-
match self.series.borrow().dtype() {
|
554
|
-
DataType::Boolean => {
|
555
|
-
let s = self.series.borrow().cast(&DataType::UInt8).unwrap();
|
556
|
-
s.median()
|
557
|
-
}
|
558
|
-
_ => self.series.borrow().median(),
|
559
|
-
}
|
560
|
-
}
|
561
|
-
|
562
|
-
pub fn quantile(
|
563
|
-
&self,
|
564
|
-
quantile: f64,
|
565
|
-
interpolation: Wrap<QuantileInterpolOptions>,
|
566
|
-
) -> RbResult<Value> {
|
567
|
-
Ok(Wrap(
|
568
|
-
self.series
|
569
|
-
.borrow()
|
570
|
-
.quantile_as_series(quantile, interpolation.0)
|
571
|
-
.map_err(|_| RbValueError::new_err("invalid quantile".into()))?
|
572
|
-
.get(0)
|
573
|
-
.unwrap_or(AnyValue::Null),
|
574
|
-
)
|
575
|
-
.into_value())
|
427
|
+
to_a_recursive(series)
|
576
428
|
}
|
577
429
|
|
578
430
|
pub fn clone(&self) -> Self {
|
@@ -782,11 +634,11 @@ impl RbSeries {
|
|
782
634
|
Ok(RbSeries::new(s))
|
783
635
|
}
|
784
636
|
|
785
|
-
pub fn to_dummies(&self, sep: Option<String
|
637
|
+
pub fn to_dummies(&self, sep: Option<String>, drop_first: bool) -> RbResult<RbDataFrame> {
|
786
638
|
let df = self
|
787
639
|
.series
|
788
640
|
.borrow()
|
789
|
-
.to_dummies(sep.as_deref())
|
641
|
+
.to_dummies(sep.as_deref(), drop_first)
|
790
642
|
.map_err(RbPolarsErr::from)?;
|
791
643
|
Ok(df.into())
|
792
644
|
}
|
@@ -906,266 +758,14 @@ impl_set_with_mask!(set_with_mask_i32, i32, i32, Int32);
|
|
906
758
|
impl_set_with_mask!(set_with_mask_i64, i64, i64, Int64);
|
907
759
|
impl_set_with_mask!(set_with_mask_bool, bool, bool, Boolean);
|
908
760
|
|
909
|
-
macro_rules! impl_arithmetic {
|
910
|
-
($name:ident, $type:ty, $operand:tt) => {
|
911
|
-
impl RbSeries {
|
912
|
-
pub fn $name(&self, other: $type) -> RbResult<Self> {
|
913
|
-
Ok(RbSeries::new(&*self.series.borrow() $operand other))
|
914
|
-
}
|
915
|
-
}
|
916
|
-
};
|
917
|
-
}
|
918
|
-
|
919
|
-
impl_arithmetic!(add_u8, u8, +);
|
920
|
-
impl_arithmetic!(add_u16, u16, +);
|
921
|
-
impl_arithmetic!(add_u32, u32, +);
|
922
|
-
impl_arithmetic!(add_u64, u64, +);
|
923
|
-
impl_arithmetic!(add_i8, i8, +);
|
924
|
-
impl_arithmetic!(add_i16, i16, +);
|
925
|
-
impl_arithmetic!(add_i32, i32, +);
|
926
|
-
impl_arithmetic!(add_i64, i64, +);
|
927
|
-
impl_arithmetic!(add_datetime, i64, +);
|
928
|
-
impl_arithmetic!(add_duration, i64, +);
|
929
|
-
impl_arithmetic!(add_f32, f32, +);
|
930
|
-
impl_arithmetic!(add_f64, f64, +);
|
931
|
-
impl_arithmetic!(sub_u8, u8, -);
|
932
|
-
impl_arithmetic!(sub_u16, u16, -);
|
933
|
-
impl_arithmetic!(sub_u32, u32, -);
|
934
|
-
impl_arithmetic!(sub_u64, u64, -);
|
935
|
-
impl_arithmetic!(sub_i8, i8, -);
|
936
|
-
impl_arithmetic!(sub_i16, i16, -);
|
937
|
-
impl_arithmetic!(sub_i32, i32, -);
|
938
|
-
impl_arithmetic!(sub_i64, i64, -);
|
939
|
-
impl_arithmetic!(sub_datetime, i64, -);
|
940
|
-
impl_arithmetic!(sub_duration, i64, -);
|
941
|
-
impl_arithmetic!(sub_f32, f32, -);
|
942
|
-
impl_arithmetic!(sub_f64, f64, -);
|
943
|
-
impl_arithmetic!(div_u8, u8, /);
|
944
|
-
impl_arithmetic!(div_u16, u16, /);
|
945
|
-
impl_arithmetic!(div_u32, u32, /);
|
946
|
-
impl_arithmetic!(div_u64, u64, /);
|
947
|
-
impl_arithmetic!(div_i8, i8, /);
|
948
|
-
impl_arithmetic!(div_i16, i16, /);
|
949
|
-
impl_arithmetic!(div_i32, i32, /);
|
950
|
-
impl_arithmetic!(div_i64, i64, /);
|
951
|
-
impl_arithmetic!(div_f32, f32, /);
|
952
|
-
impl_arithmetic!(div_f64, f64, /);
|
953
|
-
impl_arithmetic!(mul_u8, u8, *);
|
954
|
-
impl_arithmetic!(mul_u16, u16, *);
|
955
|
-
impl_arithmetic!(mul_u32, u32, *);
|
956
|
-
impl_arithmetic!(mul_u64, u64, *);
|
957
|
-
impl_arithmetic!(mul_i8, i8, *);
|
958
|
-
impl_arithmetic!(mul_i16, i16, *);
|
959
|
-
impl_arithmetic!(mul_i32, i32, *);
|
960
|
-
impl_arithmetic!(mul_i64, i64, *);
|
961
|
-
impl_arithmetic!(mul_f32, f32, *);
|
962
|
-
impl_arithmetic!(mul_f64, f64, *);
|
963
|
-
impl_arithmetic!(rem_u8, u8, %);
|
964
|
-
impl_arithmetic!(rem_u16, u16, %);
|
965
|
-
impl_arithmetic!(rem_u32, u32, %);
|
966
|
-
impl_arithmetic!(rem_u64, u64, %);
|
967
|
-
impl_arithmetic!(rem_i8, i8, %);
|
968
|
-
impl_arithmetic!(rem_i16, i16, %);
|
969
|
-
impl_arithmetic!(rem_i32, i32, %);
|
970
|
-
impl_arithmetic!(rem_i64, i64, %);
|
971
|
-
impl_arithmetic!(rem_f32, f32, %);
|
972
|
-
impl_arithmetic!(rem_f64, f64, %);
|
973
|
-
|
974
|
-
macro_rules! impl_eq_num {
|
975
|
-
($name:ident, $type:ty) => {
|
976
|
-
impl RbSeries {
|
977
|
-
pub fn $name(&self, rhs: $type) -> RbResult<Self> {
|
978
|
-
let s = self.series.borrow().equal(rhs).map_err(RbPolarsErr::from)?;
|
979
|
-
Ok(RbSeries::new(s.into_series()))
|
980
|
-
}
|
981
|
-
}
|
982
|
-
};
|
983
|
-
}
|
984
|
-
|
985
|
-
impl_eq_num!(eq_u8, u8);
|
986
|
-
impl_eq_num!(eq_u16, u16);
|
987
|
-
impl_eq_num!(eq_u32, u32);
|
988
|
-
impl_eq_num!(eq_u64, u64);
|
989
|
-
impl_eq_num!(eq_i8, i8);
|
990
|
-
impl_eq_num!(eq_i16, i16);
|
991
|
-
impl_eq_num!(eq_i32, i32);
|
992
|
-
impl_eq_num!(eq_i64, i64);
|
993
|
-
impl_eq_num!(eq_f32, f32);
|
994
|
-
impl_eq_num!(eq_f64, f64);
|
995
|
-
// impl_eq_num!(eq_str, &str);
|
996
|
-
|
997
|
-
macro_rules! impl_neq_num {
|
998
|
-
($name:ident, $type:ty) => {
|
999
|
-
impl RbSeries {
|
1000
|
-
pub fn $name(&self, rhs: $type) -> RbResult<Self> {
|
1001
|
-
let s = self
|
1002
|
-
.series
|
1003
|
-
.borrow()
|
1004
|
-
.not_equal(rhs)
|
1005
|
-
.map_err(RbPolarsErr::from)?;
|
1006
|
-
Ok(RbSeries::new(s.into_series()))
|
1007
|
-
}
|
1008
|
-
}
|
1009
|
-
};
|
1010
|
-
}
|
1011
|
-
|
1012
|
-
impl_neq_num!(neq_u8, u8);
|
1013
|
-
impl_neq_num!(neq_u16, u16);
|
1014
|
-
impl_neq_num!(neq_u32, u32);
|
1015
|
-
impl_neq_num!(neq_u64, u64);
|
1016
|
-
impl_neq_num!(neq_i8, i8);
|
1017
|
-
impl_neq_num!(neq_i16, i16);
|
1018
|
-
impl_neq_num!(neq_i32, i32);
|
1019
|
-
impl_neq_num!(neq_i64, i64);
|
1020
|
-
impl_neq_num!(neq_f32, f32);
|
1021
|
-
impl_neq_num!(neq_f64, f64);
|
1022
|
-
// impl_neq_num!(neq_str, &str);
|
1023
|
-
|
1024
|
-
macro_rules! impl_gt_num {
|
1025
|
-
($name:ident, $type:ty) => {
|
1026
|
-
impl RbSeries {
|
1027
|
-
pub fn $name(&self, rhs: $type) -> RbResult<Self> {
|
1028
|
-
let s = self.series.borrow().gt(rhs).map_err(RbPolarsErr::from)?;
|
1029
|
-
Ok(RbSeries::new(s.into_series()))
|
1030
|
-
}
|
1031
|
-
}
|
1032
|
-
};
|
1033
|
-
}
|
1034
|
-
|
1035
|
-
impl_gt_num!(gt_u8, u8);
|
1036
|
-
impl_gt_num!(gt_u16, u16);
|
1037
|
-
impl_gt_num!(gt_u32, u32);
|
1038
|
-
impl_gt_num!(gt_u64, u64);
|
1039
|
-
impl_gt_num!(gt_i8, i8);
|
1040
|
-
impl_gt_num!(gt_i16, i16);
|
1041
|
-
impl_gt_num!(gt_i32, i32);
|
1042
|
-
impl_gt_num!(gt_i64, i64);
|
1043
|
-
impl_gt_num!(gt_f32, f32);
|
1044
|
-
impl_gt_num!(gt_f64, f64);
|
1045
|
-
// impl_gt_num!(gt_str, &str);
|
1046
|
-
|
1047
|
-
macro_rules! impl_gt_eq_num {
|
1048
|
-
($name:ident, $type:ty) => {
|
1049
|
-
impl RbSeries {
|
1050
|
-
pub fn $name(&self, rhs: $type) -> RbResult<Self> {
|
1051
|
-
let s = self.series.borrow().gt_eq(rhs).map_err(RbPolarsErr::from)?;
|
1052
|
-
Ok(RbSeries::new(s.into_series()))
|
1053
|
-
}
|
1054
|
-
}
|
1055
|
-
};
|
1056
|
-
}
|
1057
|
-
|
1058
|
-
impl_gt_eq_num!(gt_eq_u8, u8);
|
1059
|
-
impl_gt_eq_num!(gt_eq_u16, u16);
|
1060
|
-
impl_gt_eq_num!(gt_eq_u32, u32);
|
1061
|
-
impl_gt_eq_num!(gt_eq_u64, u64);
|
1062
|
-
impl_gt_eq_num!(gt_eq_i8, i8);
|
1063
|
-
impl_gt_eq_num!(gt_eq_i16, i16);
|
1064
|
-
impl_gt_eq_num!(gt_eq_i32, i32);
|
1065
|
-
impl_gt_eq_num!(gt_eq_i64, i64);
|
1066
|
-
impl_gt_eq_num!(gt_eq_f32, f32);
|
1067
|
-
impl_gt_eq_num!(gt_eq_f64, f64);
|
1068
|
-
// impl_gt_eq_num!(gt_eq_str, &str);
|
1069
|
-
|
1070
|
-
macro_rules! impl_lt_num {
|
1071
|
-
($name:ident, $type:ty) => {
|
1072
|
-
impl RbSeries {
|
1073
|
-
pub fn $name(&self, rhs: $type) -> RbResult<RbSeries> {
|
1074
|
-
let s = self.series.borrow().lt(rhs).map_err(RbPolarsErr::from)?;
|
1075
|
-
Ok(RbSeries::new(s.into_series()))
|
1076
|
-
}
|
1077
|
-
}
|
1078
|
-
};
|
1079
|
-
}
|
1080
|
-
|
1081
|
-
impl_lt_num!(lt_u8, u8);
|
1082
|
-
impl_lt_num!(lt_u16, u16);
|
1083
|
-
impl_lt_num!(lt_u32, u32);
|
1084
|
-
impl_lt_num!(lt_u64, u64);
|
1085
|
-
impl_lt_num!(lt_i8, i8);
|
1086
|
-
impl_lt_num!(lt_i16, i16);
|
1087
|
-
impl_lt_num!(lt_i32, i32);
|
1088
|
-
impl_lt_num!(lt_i64, i64);
|
1089
|
-
impl_lt_num!(lt_f32, f32);
|
1090
|
-
impl_lt_num!(lt_f64, f64);
|
1091
|
-
// impl_lt_num!(lt_str, &str);
|
1092
|
-
|
1093
|
-
macro_rules! impl_lt_eq_num {
|
1094
|
-
($name:ident, $type:ty) => {
|
1095
|
-
impl RbSeries {
|
1096
|
-
pub fn $name(&self, rhs: $type) -> RbResult<Self> {
|
1097
|
-
let s = self.series.borrow().lt_eq(rhs).map_err(RbPolarsErr::from)?;
|
1098
|
-
Ok(RbSeries::new(s.into_series()))
|
1099
|
-
}
|
1100
|
-
}
|
1101
|
-
};
|
1102
|
-
}
|
1103
|
-
|
1104
|
-
impl_lt_eq_num!(lt_eq_u8, u8);
|
1105
|
-
impl_lt_eq_num!(lt_eq_u16, u16);
|
1106
|
-
impl_lt_eq_num!(lt_eq_u32, u32);
|
1107
|
-
impl_lt_eq_num!(lt_eq_u64, u64);
|
1108
|
-
impl_lt_eq_num!(lt_eq_i8, i8);
|
1109
|
-
impl_lt_eq_num!(lt_eq_i16, i16);
|
1110
|
-
impl_lt_eq_num!(lt_eq_i32, i32);
|
1111
|
-
impl_lt_eq_num!(lt_eq_i64, i64);
|
1112
|
-
impl_lt_eq_num!(lt_eq_f32, f32);
|
1113
|
-
impl_lt_eq_num!(lt_eq_f64, f64);
|
1114
|
-
// impl_lt_eq_num!(lt_eq_str, &str);
|
1115
|
-
|
1116
|
-
pub fn to_series_collection(rs: RArray) -> RbResult<Vec<Series>> {
|
1117
|
-
let mut series = Vec::new();
|
1118
|
-
for item in rs.each() {
|
1119
|
-
series.push(item?.try_convert::<&RbSeries>()?.series.borrow().clone());
|
1120
|
-
}
|
1121
|
-
Ok(series)
|
1122
|
-
}
|
1123
|
-
|
1124
|
-
pub fn to_rbseries_collection(s: Vec<Series>) -> RArray {
|
1125
|
-
RArray::from_iter(s.into_iter().map(RbSeries::new))
|
1126
|
-
}
|
1127
|
-
|
1128
761
|
impl RbSeries {
|
1129
|
-
pub fn
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
} else {
|
1137
|
-
// convert to DateTime for UTC
|
1138
|
-
let v = v
|
1139
|
-
.funcall::<_, _, Value>("to_datetime", ())?
|
1140
|
-
.funcall::<_, _, Value>("to_time", ())?
|
1141
|
-
.funcall::<_, _, i64>("to_i", ())?;
|
1142
|
-
|
1143
|
-
// TODO use strict
|
1144
|
-
builder.append_value((v / 86400) as i32);
|
1145
|
-
}
|
1146
|
-
}
|
1147
|
-
let ca: ChunkedArray<Int32Type> = builder.finish();
|
1148
|
-
Ok(ca.into_date().into_series().into())
|
1149
|
-
}
|
1150
|
-
|
1151
|
-
pub fn new_opt_datetime(name: String, values: RArray, _strict: Option<bool>) -> RbResult<Self> {
|
1152
|
-
let len = values.len();
|
1153
|
-
let mut builder = PrimitiveChunkedBuilder::<Int64Type>::new(&name, len);
|
1154
|
-
for item in values.each() {
|
1155
|
-
let v = item?;
|
1156
|
-
if v.is_nil() {
|
1157
|
-
builder.append_null();
|
1158
|
-
} else {
|
1159
|
-
let sec: i64 = v.funcall("to_i", ())?;
|
1160
|
-
let nsec: i64 = v.funcall("nsec", ())?;
|
1161
|
-
// TODO use strict
|
1162
|
-
builder.append_value(sec * 1_000_000_000 + nsec);
|
1163
|
-
}
|
1164
|
-
}
|
1165
|
-
let ca: ChunkedArray<Int64Type> = builder.finish();
|
1166
|
-
Ok(ca
|
1167
|
-
.into_datetime(TimeUnit::Nanoseconds, None)
|
1168
|
-
.into_series()
|
762
|
+
pub fn extend_constant(&self, value: Wrap<AnyValue>, n: usize) -> RbResult<Self> {
|
763
|
+
Ok(self
|
764
|
+
.series
|
765
|
+
.borrow()
|
766
|
+
.clone()
|
767
|
+
.extend_constant(value.0, n)
|
768
|
+
.map_err(RbPolarsErr::from)?
|
1169
769
|
.into())
|
1170
770
|
}
|
1171
771
|
}
|