polars-df 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -1
  3. data/Cargo.lock +486 -380
  4. data/Cargo.toml +0 -2
  5. data/README.md +31 -2
  6. data/ext/polars/Cargo.toml +10 -4
  7. data/ext/polars/src/apply/dataframe.rs +2 -2
  8. data/ext/polars/src/{lazy/apply.rs → apply/lazy.rs} +1 -2
  9. data/ext/polars/src/apply/mod.rs +1 -0
  10. data/ext/polars/src/batched_csv.rs +36 -19
  11. data/ext/polars/src/conversion.rs +159 -16
  12. data/ext/polars/src/dataframe.rs +51 -52
  13. data/ext/polars/src/error.rs +0 -4
  14. data/ext/polars/src/expr/binary.rs +69 -0
  15. data/ext/polars/src/expr/categorical.rs +10 -0
  16. data/ext/polars/src/expr/datetime.rs +223 -0
  17. data/ext/polars/src/{lazy/dsl.rs → expr/general.rs} +22 -799
  18. data/ext/polars/src/expr/list.rs +146 -0
  19. data/ext/polars/src/{lazy → expr}/meta.rs +16 -6
  20. data/ext/polars/src/expr/string.rs +313 -0
  21. data/ext/polars/src/expr/struct.rs +15 -0
  22. data/ext/polars/src/expr.rs +33 -0
  23. data/ext/polars/src/functions/eager.rs +93 -0
  24. data/ext/polars/src/functions/io.rs +34 -0
  25. data/ext/polars/src/functions/lazy.rs +209 -0
  26. data/ext/polars/src/functions/meta.rs +8 -0
  27. data/ext/polars/src/functions/mod.rs +5 -0
  28. data/ext/polars/src/functions/whenthen.rs +43 -0
  29. data/ext/polars/src/{lazy/dataframe.rs → lazyframe.rs} +58 -45
  30. data/ext/polars/src/lazygroupby.rs +29 -0
  31. data/ext/polars/src/lib.rs +216 -300
  32. data/ext/polars/src/rb_modules.rs +8 -0
  33. data/ext/polars/src/series/aggregation.rs +83 -0
  34. data/ext/polars/src/series/arithmetic.rs +88 -0
  35. data/ext/polars/src/series/comparison.rs +251 -0
  36. data/ext/polars/src/series/construction.rs +164 -0
  37. data/ext/polars/src/series.rs +103 -531
  38. data/lib/polars/batched_csv_reader.rb +1 -1
  39. data/lib/polars/binary_expr.rb +77 -0
  40. data/lib/polars/binary_name_space.rb +66 -0
  41. data/lib/polars/convert.rb +2 -2
  42. data/lib/polars/data_frame.rb +263 -87
  43. data/lib/polars/data_types.rb +6 -4
  44. data/lib/polars/date_time_expr.rb +148 -8
  45. data/lib/polars/expr.rb +78 -11
  46. data/lib/polars/io.rb +73 -62
  47. data/lib/polars/lazy_frame.rb +107 -10
  48. data/lib/polars/lazy_functions.rb +7 -3
  49. data/lib/polars/list_expr.rb +70 -21
  50. data/lib/polars/list_name_space.rb +2 -2
  51. data/lib/polars/series.rb +190 -74
  52. data/lib/polars/string_expr.rb +150 -44
  53. data/lib/polars/string_name_space.rb +4 -4
  54. data/lib/polars/struct_name_space.rb +32 -0
  55. data/lib/polars/utils.rb +51 -9
  56. data/lib/polars/version.rb +1 -1
  57. data/lib/polars.rb +4 -2
  58. metadata +29 -12
  59. data/ext/polars/src/lazy/mod.rs +0 -5
  60. data/ext/polars/src/lazy/utils.rs +0 -13
  61. data/ext/polars/src/list_construction.rs +0 -100
  62. /data/ext/polars/src/{numo.rs → series/export.rs} +0 -0
  63. /data/ext/polars/src/{set.rs → series/set_at_idx.rs} +0 -0
@@ -1,4 +1,11 @@
1
- use magnus::{exception, Error, IntoValue, RArray, Value};
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::list_construction::rb_seq_to_list;
10
- use crate::set::set_at_idx;
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,115 +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 new_primitive<T>(name: &str, obj: RArray, strict: bool) -> RbResult<RbSeries>
68
- where
69
- T: PolarsNumericType,
70
- ChunkedArray<T>: IntoSeries,
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
- let ca = builder.finish();
94
-
95
- let s = ca.into_series();
96
- Ok(RbSeries::new(s))
43
+ Ok(series)
97
44
  }
98
45
 
99
- // Init with lists that can contain Nones
100
- macro_rules! init_method_opt {
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 new_str(name: String, val: Wrap<Utf8Chunked>, _strict: bool) -> Self {
123
- let mut s = val.0.into_series();
124
- s.rename(&name);
125
- RbSeries::new(s)
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 new_object(name: String, val: RArray, _strict: bool) -> RbResult<Self> {
129
- let val = val
130
- .each()
131
- .map(|v| v.map(ObjectValue::from))
132
- .collect::<RbResult<Vec<ObjectValue>>>()?;
133
- let s = ObjectChunked::<ObjectValue>::new_from_vec(&name, val).into_series();
134
- Ok(s.into())
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)
65
+ }
66
+
67
+ pub fn is_sorted_descending_flag(&self) -> bool {
68
+ matches!(self.series.borrow().is_sorted_flag(), IsSorted::Descending)
135
69
  }
136
70
 
137
- pub fn new_list(name: String, seq: Value, dtype: Wrap<DataType>) -> RbResult<Self> {
138
- rb_seq_to_list(&name, seq, &dtype.0).map(|s| s.into())
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
+ }
139
76
  }
140
77
 
141
78
  pub fn estimated_size(&self) -> usize {
@@ -226,9 +163,9 @@ impl RbSeries {
226
163
  .map(|dt| Wrap(dt.clone()).into_value())
227
164
  }
228
165
 
229
- pub fn set_sorted(&self, reverse: bool) -> Self {
166
+ pub fn set_sorted_flag(&self, descending: bool) -> Self {
230
167
  let mut out = self.series.borrow().clone();
231
- if reverse {
168
+ if descending {
232
169
  out.set_sorted_flag(IsSorted::Descending);
233
170
  } else {
234
171
  out.set_sorted_flag(IsSorted::Ascending)
@@ -236,49 +173,6 @@ impl RbSeries {
236
173
  out.into()
237
174
  }
238
175
 
239
- pub fn mean(&self) -> Option<f64> {
240
- match self.series.borrow().dtype() {
241
- DataType::Boolean => {
242
- let s = self.series.borrow().cast(&DataType::UInt8).unwrap();
243
- s.mean()
244
- }
245
- _ => self.series.borrow().mean(),
246
- }
247
- }
248
-
249
- pub fn max(&self) -> RbResult<Value> {
250
- Ok(Wrap(
251
- self.series
252
- .borrow()
253
- .max_as_series()
254
- .get(0)
255
- .map_err(RbPolarsErr::from)?,
256
- )
257
- .into_value())
258
- }
259
-
260
- pub fn min(&self) -> RbResult<Value> {
261
- Ok(Wrap(
262
- self.series
263
- .borrow()
264
- .min_as_series()
265
- .get(0)
266
- .map_err(RbPolarsErr::from)?,
267
- )
268
- .into_value())
269
- }
270
-
271
- pub fn sum(&self) -> RbResult<Value> {
272
- Ok(Wrap(
273
- self.series
274
- .borrow()
275
- .sum_as_series()
276
- .get(0)
277
- .map_err(RbPolarsErr::from)?,
278
- )
279
- .into_value())
280
- }
281
-
282
176
  pub fn n_chunks(&self) -> usize {
283
177
  self.series.borrow().n_chunks()
284
178
  }
@@ -322,26 +216,6 @@ impl RbSeries {
322
216
  }
323
217
  }
324
218
 
325
- pub fn add(&self, other: &RbSeries) -> Self {
326
- (&*self.series.borrow() + &*other.series.borrow()).into()
327
- }
328
-
329
- pub fn sub(&self, other: &RbSeries) -> Self {
330
- (&*self.series.borrow() - &*other.series.borrow()).into()
331
- }
332
-
333
- pub fn mul(&self, other: &RbSeries) -> Self {
334
- (&*self.series.borrow() * &*other.series.borrow()).into()
335
- }
336
-
337
- pub fn div(&self, other: &RbSeries) -> Self {
338
- (&*self.series.borrow() / &*other.series.borrow()).into()
339
- }
340
-
341
- pub fn rem(&self, other: &RbSeries) -> Self {
342
- (&*self.series.borrow() % &*other.series.borrow()).into()
343
- }
344
-
345
219
  pub fn sort(&self, reverse: bool) -> Self {
346
220
  (self.series.borrow_mut().sort(reverse)).into()
347
221
  }
@@ -355,14 +229,6 @@ impl RbSeries {
355
229
  Ok(df.into())
356
230
  }
357
231
 
358
- pub fn arg_min(&self) -> Option<usize> {
359
- self.series.borrow().arg_min()
360
- }
361
-
362
- pub fn arg_max(&self) -> Option<usize> {
363
- self.series.borrow().arg_max()
364
- }
365
-
366
232
  pub fn take_with_series(&self, indices: &RbSeries) -> RbResult<Self> {
367
233
  let binding = indices.series.borrow();
368
234
  let idx = binding.idx().map_err(RbPolarsErr::from)?;
@@ -420,60 +286,6 @@ impl RbSeries {
420
286
  }
421
287
  }
422
288
 
423
- pub fn eq(&self, rhs: &RbSeries) -> RbResult<Self> {
424
- let s = self
425
- .series
426
- .borrow()
427
- .equal(&*rhs.series.borrow())
428
- .map_err(RbPolarsErr::from)?;
429
- Ok(Self::new(s.into_series()))
430
- }
431
-
432
- pub fn neq(&self, rhs: &RbSeries) -> RbResult<Self> {
433
- let s = self
434
- .series
435
- .borrow()
436
- .not_equal(&*rhs.series.borrow())
437
- .map_err(RbPolarsErr::from)?;
438
- Ok(Self::new(s.into_series()))
439
- }
440
-
441
- pub fn gt(&self, rhs: &RbSeries) -> RbResult<Self> {
442
- let s = self
443
- .series
444
- .borrow()
445
- .gt(&*rhs.series.borrow())
446
- .map_err(RbPolarsErr::from)?;
447
- Ok(Self::new(s.into_series()))
448
- }
449
-
450
- pub fn gt_eq(&self, rhs: &RbSeries) -> RbResult<Self> {
451
- let s = self
452
- .series
453
- .borrow()
454
- .gt_eq(&*rhs.series.borrow())
455
- .map_err(RbPolarsErr::from)?;
456
- Ok(Self::new(s.into_series()))
457
- }
458
-
459
- pub fn lt(&self, rhs: &RbSeries) -> RbResult<Self> {
460
- let s = self
461
- .series
462
- .borrow()
463
- .lt(&*rhs.series.borrow())
464
- .map_err(RbPolarsErr::from)?;
465
- Ok(Self::new(s.into_series()))
466
- }
467
-
468
- pub fn lt_eq(&self, rhs: &RbSeries) -> RbResult<Self> {
469
- let s = self
470
- .series
471
- .borrow()
472
- .lt_eq(&*rhs.series.borrow())
473
- .map_err(RbPolarsErr::from)?;
474
- Ok(Self::new(s.into_series()))
475
- }
476
-
477
289
  pub fn not(&self) -> RbResult<Self> {
478
290
  let binding = self.series.borrow();
479
291
  let bool = binding.bool().map_err(RbPolarsErr::from)?;
@@ -488,43 +300,81 @@ impl RbSeries {
488
300
  self.series.borrow().len()
489
301
  }
490
302
 
491
- pub fn to_a(&self) -> RArray {
303
+ pub fn to_a(&self) -> Value {
492
304
  let series = &self.series.borrow();
493
305
 
494
- fn to_list_recursive(series: &Series) -> RArray {
306
+ fn to_list_recursive(series: &Series) -> Value {
495
307
  let rblist = match series.dtype() {
496
- DataType::Boolean => RArray::from_iter(series.bool().unwrap()),
497
- DataType::UInt8 => RArray::from_iter(series.u8().unwrap()),
498
- DataType::UInt16 => RArray::from_iter(series.u16().unwrap()),
499
- DataType::UInt32 => RArray::from_iter(series.u32().unwrap()),
500
- DataType::UInt64 => RArray::from_iter(series.u64().unwrap()),
501
- DataType::Int8 => RArray::from_iter(series.i8().unwrap()),
502
- DataType::Int16 => RArray::from_iter(series.i16().unwrap()),
503
- DataType::Int32 => RArray::from_iter(series.i32().unwrap()),
504
- DataType::Int64 => RArray::from_iter(series.i64().unwrap()),
505
- DataType::Float32 => RArray::from_iter(series.f32().unwrap()),
506
- DataType::Float64 => RArray::from_iter(series.f64().unwrap()),
507
- DataType::Decimal128(_) => todo!(),
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(),
508
319
  DataType::Categorical(_) => {
509
- RArray::from_iter(series.categorical().unwrap().iter_str())
320
+ RArray::from_iter(series.categorical().unwrap().iter_str()).into_value()
321
+ }
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
+ };
330
+ }
331
+ v.into_value()
332
+ }
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_list_recursive(s.as_ref());
343
+ v.push(rblst).unwrap();
344
+ }
345
+ }
346
+ }
347
+ v.into_value()
510
348
  }
511
349
  DataType::Date => {
512
350
  let a = RArray::with_capacity(series.len());
513
351
  for v in series.iter() {
514
352
  a.push::<Value>(Wrap(v).into_value()).unwrap();
515
353
  }
516
- return a;
354
+ return a.into_value();
517
355
  }
518
356
  DataType::Datetime(_, _) => {
519
357
  let a = RArray::with_capacity(series.len());
520
358
  for v in series.iter() {
521
359
  a.push::<Value>(Wrap(v).into_value()).unwrap();
522
360
  }
523
- return a;
361
+ return a.into_value();
524
362
  }
525
363
  DataType::Utf8 => {
526
364
  let ca = series.utf8().unwrap();
527
- return RArray::from_iter(ca);
365
+ return Wrap(ca).into_value();
366
+ }
367
+ DataType::Struct(_) => {
368
+ let ca = series.struct_().unwrap();
369
+ return Wrap(ca).into_value();
370
+ }
371
+ DataType::Duration(_) => {
372
+ let ca = series.duration().unwrap();
373
+ return Wrap(ca).into_value();
374
+ }
375
+ DataType::Binary => {
376
+ let ca = series.binary().unwrap();
377
+ return Wrap(ca).into_value();
528
378
  }
529
379
  DataType::Null | DataType::Unknown => {
530
380
  panic!("to_a not implemented for null/unknown")
@@ -537,32 +387,6 @@ impl RbSeries {
537
387
  to_list_recursive(series)
538
388
  }
539
389
 
540
- pub fn median(&self) -> Option<f64> {
541
- match self.series.borrow().dtype() {
542
- DataType::Boolean => {
543
- let s = self.series.borrow().cast(&DataType::UInt8).unwrap();
544
- s.median()
545
- }
546
- _ => self.series.borrow().median(),
547
- }
548
- }
549
-
550
- pub fn quantile(
551
- &self,
552
- quantile: f64,
553
- interpolation: Wrap<QuantileInterpolOptions>,
554
- ) -> RbResult<Value> {
555
- Ok(Wrap(
556
- self.series
557
- .borrow()
558
- .quantile_as_series(quantile, interpolation.0)
559
- .map_err(|_| RbValueError::new_err("invalid quantile".into()))?
560
- .get(0)
561
- .unwrap_or(AnyValue::Null),
562
- )
563
- .into_value())
564
- }
565
-
566
390
  pub fn clone(&self) -> Self {
567
391
  RbSeries::new(self.series.borrow().clone())
568
392
  }
@@ -894,266 +718,14 @@ impl_set_with_mask!(set_with_mask_i32, i32, i32, Int32);
894
718
  impl_set_with_mask!(set_with_mask_i64, i64, i64, Int64);
895
719
  impl_set_with_mask!(set_with_mask_bool, bool, bool, Boolean);
896
720
 
897
- macro_rules! impl_arithmetic {
898
- ($name:ident, $type:ty, $operand:tt) => {
899
- impl RbSeries {
900
- pub fn $name(&self, other: $type) -> RbResult<Self> {
901
- Ok(RbSeries::new(&*self.series.borrow() $operand other))
902
- }
903
- }
904
- };
905
- }
906
-
907
- impl_arithmetic!(add_u8, u8, +);
908
- impl_arithmetic!(add_u16, u16, +);
909
- impl_arithmetic!(add_u32, u32, +);
910
- impl_arithmetic!(add_u64, u64, +);
911
- impl_arithmetic!(add_i8, i8, +);
912
- impl_arithmetic!(add_i16, i16, +);
913
- impl_arithmetic!(add_i32, i32, +);
914
- impl_arithmetic!(add_i64, i64, +);
915
- impl_arithmetic!(add_datetime, i64, +);
916
- impl_arithmetic!(add_duration, i64, +);
917
- impl_arithmetic!(add_f32, f32, +);
918
- impl_arithmetic!(add_f64, f64, +);
919
- impl_arithmetic!(sub_u8, u8, -);
920
- impl_arithmetic!(sub_u16, u16, -);
921
- impl_arithmetic!(sub_u32, u32, -);
922
- impl_arithmetic!(sub_u64, u64, -);
923
- impl_arithmetic!(sub_i8, i8, -);
924
- impl_arithmetic!(sub_i16, i16, -);
925
- impl_arithmetic!(sub_i32, i32, -);
926
- impl_arithmetic!(sub_i64, i64, -);
927
- impl_arithmetic!(sub_datetime, i64, -);
928
- impl_arithmetic!(sub_duration, i64, -);
929
- impl_arithmetic!(sub_f32, f32, -);
930
- impl_arithmetic!(sub_f64, f64, -);
931
- impl_arithmetic!(div_u8, u8, /);
932
- impl_arithmetic!(div_u16, u16, /);
933
- impl_arithmetic!(div_u32, u32, /);
934
- impl_arithmetic!(div_u64, u64, /);
935
- impl_arithmetic!(div_i8, i8, /);
936
- impl_arithmetic!(div_i16, i16, /);
937
- impl_arithmetic!(div_i32, i32, /);
938
- impl_arithmetic!(div_i64, i64, /);
939
- impl_arithmetic!(div_f32, f32, /);
940
- impl_arithmetic!(div_f64, f64, /);
941
- impl_arithmetic!(mul_u8, u8, *);
942
- impl_arithmetic!(mul_u16, u16, *);
943
- impl_arithmetic!(mul_u32, u32, *);
944
- impl_arithmetic!(mul_u64, u64, *);
945
- impl_arithmetic!(mul_i8, i8, *);
946
- impl_arithmetic!(mul_i16, i16, *);
947
- impl_arithmetic!(mul_i32, i32, *);
948
- impl_arithmetic!(mul_i64, i64, *);
949
- impl_arithmetic!(mul_f32, f32, *);
950
- impl_arithmetic!(mul_f64, f64, *);
951
- impl_arithmetic!(rem_u8, u8, %);
952
- impl_arithmetic!(rem_u16, u16, %);
953
- impl_arithmetic!(rem_u32, u32, %);
954
- impl_arithmetic!(rem_u64, u64, %);
955
- impl_arithmetic!(rem_i8, i8, %);
956
- impl_arithmetic!(rem_i16, i16, %);
957
- impl_arithmetic!(rem_i32, i32, %);
958
- impl_arithmetic!(rem_i64, i64, %);
959
- impl_arithmetic!(rem_f32, f32, %);
960
- impl_arithmetic!(rem_f64, f64, %);
961
-
962
- macro_rules! impl_eq_num {
963
- ($name:ident, $type:ty) => {
964
- impl RbSeries {
965
- pub fn $name(&self, rhs: $type) -> RbResult<Self> {
966
- let s = self.series.borrow().equal(rhs).map_err(RbPolarsErr::from)?;
967
- Ok(RbSeries::new(s.into_series()))
968
- }
969
- }
970
- };
971
- }
972
-
973
- impl_eq_num!(eq_u8, u8);
974
- impl_eq_num!(eq_u16, u16);
975
- impl_eq_num!(eq_u32, u32);
976
- impl_eq_num!(eq_u64, u64);
977
- impl_eq_num!(eq_i8, i8);
978
- impl_eq_num!(eq_i16, i16);
979
- impl_eq_num!(eq_i32, i32);
980
- impl_eq_num!(eq_i64, i64);
981
- impl_eq_num!(eq_f32, f32);
982
- impl_eq_num!(eq_f64, f64);
983
- // impl_eq_num!(eq_str, &str);
984
-
985
- macro_rules! impl_neq_num {
986
- ($name:ident, $type:ty) => {
987
- impl RbSeries {
988
- pub fn $name(&self, rhs: $type) -> RbResult<Self> {
989
- let s = self
990
- .series
991
- .borrow()
992
- .not_equal(rhs)
993
- .map_err(RbPolarsErr::from)?;
994
- Ok(RbSeries::new(s.into_series()))
995
- }
996
- }
997
- };
998
- }
999
-
1000
- impl_neq_num!(neq_u8, u8);
1001
- impl_neq_num!(neq_u16, u16);
1002
- impl_neq_num!(neq_u32, u32);
1003
- impl_neq_num!(neq_u64, u64);
1004
- impl_neq_num!(neq_i8, i8);
1005
- impl_neq_num!(neq_i16, i16);
1006
- impl_neq_num!(neq_i32, i32);
1007
- impl_neq_num!(neq_i64, i64);
1008
- impl_neq_num!(neq_f32, f32);
1009
- impl_neq_num!(neq_f64, f64);
1010
- // impl_neq_num!(neq_str, &str);
1011
-
1012
- macro_rules! impl_gt_num {
1013
- ($name:ident, $type:ty) => {
1014
- impl RbSeries {
1015
- pub fn $name(&self, rhs: $type) -> RbResult<Self> {
1016
- let s = self.series.borrow().gt(rhs).map_err(RbPolarsErr::from)?;
1017
- Ok(RbSeries::new(s.into_series()))
1018
- }
1019
- }
1020
- };
1021
- }
1022
-
1023
- impl_gt_num!(gt_u8, u8);
1024
- impl_gt_num!(gt_u16, u16);
1025
- impl_gt_num!(gt_u32, u32);
1026
- impl_gt_num!(gt_u64, u64);
1027
- impl_gt_num!(gt_i8, i8);
1028
- impl_gt_num!(gt_i16, i16);
1029
- impl_gt_num!(gt_i32, i32);
1030
- impl_gt_num!(gt_i64, i64);
1031
- impl_gt_num!(gt_f32, f32);
1032
- impl_gt_num!(gt_f64, f64);
1033
- // impl_gt_num!(gt_str, &str);
1034
-
1035
- macro_rules! impl_gt_eq_num {
1036
- ($name:ident, $type:ty) => {
1037
- impl RbSeries {
1038
- pub fn $name(&self, rhs: $type) -> RbResult<Self> {
1039
- let s = self.series.borrow().gt_eq(rhs).map_err(RbPolarsErr::from)?;
1040
- Ok(RbSeries::new(s.into_series()))
1041
- }
1042
- }
1043
- };
1044
- }
1045
-
1046
- impl_gt_eq_num!(gt_eq_u8, u8);
1047
- impl_gt_eq_num!(gt_eq_u16, u16);
1048
- impl_gt_eq_num!(gt_eq_u32, u32);
1049
- impl_gt_eq_num!(gt_eq_u64, u64);
1050
- impl_gt_eq_num!(gt_eq_i8, i8);
1051
- impl_gt_eq_num!(gt_eq_i16, i16);
1052
- impl_gt_eq_num!(gt_eq_i32, i32);
1053
- impl_gt_eq_num!(gt_eq_i64, i64);
1054
- impl_gt_eq_num!(gt_eq_f32, f32);
1055
- impl_gt_eq_num!(gt_eq_f64, f64);
1056
- // impl_gt_eq_num!(gt_eq_str, &str);
1057
-
1058
- macro_rules! impl_lt_num {
1059
- ($name:ident, $type:ty) => {
1060
- impl RbSeries {
1061
- pub fn $name(&self, rhs: $type) -> RbResult<RbSeries> {
1062
- let s = self.series.borrow().lt(rhs).map_err(RbPolarsErr::from)?;
1063
- Ok(RbSeries::new(s.into_series()))
1064
- }
1065
- }
1066
- };
1067
- }
1068
-
1069
- impl_lt_num!(lt_u8, u8);
1070
- impl_lt_num!(lt_u16, u16);
1071
- impl_lt_num!(lt_u32, u32);
1072
- impl_lt_num!(lt_u64, u64);
1073
- impl_lt_num!(lt_i8, i8);
1074
- impl_lt_num!(lt_i16, i16);
1075
- impl_lt_num!(lt_i32, i32);
1076
- impl_lt_num!(lt_i64, i64);
1077
- impl_lt_num!(lt_f32, f32);
1078
- impl_lt_num!(lt_f64, f64);
1079
- // impl_lt_num!(lt_str, &str);
1080
-
1081
- macro_rules! impl_lt_eq_num {
1082
- ($name:ident, $type:ty) => {
1083
- impl RbSeries {
1084
- pub fn $name(&self, rhs: $type) -> RbResult<Self> {
1085
- let s = self.series.borrow().lt_eq(rhs).map_err(RbPolarsErr::from)?;
1086
- Ok(RbSeries::new(s.into_series()))
1087
- }
1088
- }
1089
- };
1090
- }
1091
-
1092
- impl_lt_eq_num!(lt_eq_u8, u8);
1093
- impl_lt_eq_num!(lt_eq_u16, u16);
1094
- impl_lt_eq_num!(lt_eq_u32, u32);
1095
- impl_lt_eq_num!(lt_eq_u64, u64);
1096
- impl_lt_eq_num!(lt_eq_i8, i8);
1097
- impl_lt_eq_num!(lt_eq_i16, i16);
1098
- impl_lt_eq_num!(lt_eq_i32, i32);
1099
- impl_lt_eq_num!(lt_eq_i64, i64);
1100
- impl_lt_eq_num!(lt_eq_f32, f32);
1101
- impl_lt_eq_num!(lt_eq_f64, f64);
1102
- // impl_lt_eq_num!(lt_eq_str, &str);
1103
-
1104
- pub fn to_series_collection(rs: RArray) -> RbResult<Vec<Series>> {
1105
- let mut series = Vec::new();
1106
- for item in rs.each() {
1107
- series.push(item?.try_convert::<&RbSeries>()?.series.borrow().clone());
1108
- }
1109
- Ok(series)
1110
- }
1111
-
1112
- pub fn to_rbseries_collection(s: Vec<Series>) -> RArray {
1113
- RArray::from_iter(s.into_iter().map(RbSeries::new))
1114
- }
1115
-
1116
721
  impl RbSeries {
1117
- pub fn new_opt_date(name: String, values: RArray, _strict: Option<bool>) -> RbResult<Self> {
1118
- let len = values.len();
1119
- let mut builder = PrimitiveChunkedBuilder::<Int32Type>::new(&name, len);
1120
- for item in values.each() {
1121
- let v = item?;
1122
- if v.is_nil() {
1123
- builder.append_null();
1124
- } else {
1125
- // convert to DateTime for UTC
1126
- let v = v
1127
- .funcall::<_, _, Value>("to_datetime", ())?
1128
- .funcall::<_, _, Value>("to_time", ())?
1129
- .funcall::<_, _, i64>("to_i", ())?;
1130
-
1131
- // TODO use strict
1132
- builder.append_value((v / 86400) as i32);
1133
- }
1134
- }
1135
- let ca: ChunkedArray<Int32Type> = builder.finish();
1136
- Ok(ca.into_date().into_series().into())
1137
- }
1138
-
1139
- pub fn new_opt_datetime(name: String, values: RArray, _strict: Option<bool>) -> RbResult<Self> {
1140
- let len = values.len();
1141
- let mut builder = PrimitiveChunkedBuilder::<Int64Type>::new(&name, len);
1142
- for item in values.each() {
1143
- let v = item?;
1144
- if v.is_nil() {
1145
- builder.append_null();
1146
- } else {
1147
- let sec: i64 = v.funcall("to_i", ())?;
1148
- let nsec: i64 = v.funcall("nsec", ())?;
1149
- // TODO use strict
1150
- builder.append_value(sec * 1_000_000_000 + nsec);
1151
- }
1152
- }
1153
- let ca: ChunkedArray<Int64Type> = builder.finish();
1154
- Ok(ca
1155
- .into_datetime(TimeUnit::Nanoseconds, None)
1156
- .into_series()
722
+ pub fn extend_constant(&self, value: Wrap<AnyValue>, n: usize) -> RbResult<Self> {
723
+ Ok(self
724
+ .series
725
+ .borrow()
726
+ .clone()
727
+ .extend_constant(value.0, n)
728
+ .map_err(RbPolarsErr::from)?
1157
729
  .into())
1158
730
  }
1159
731
  }