polars-df 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/Cargo.lock +272 -191
  4. data/Cargo.toml +0 -1
  5. data/README.md +2 -2
  6. data/ext/polars/Cargo.toml +8 -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 +7 -5
  11. data/ext/polars/src/conversion.rs +106 -4
  12. data/ext/polars/src/dataframe.rs +19 -17
  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/expr/general.rs +933 -0
  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} +12 -33
  30. data/ext/polars/src/lazygroupby.rs +29 -0
  31. data/ext/polars/src/lib.rs +205 -303
  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 +99 -539
  38. data/lib/polars/convert.rb +2 -2
  39. data/lib/polars/data_frame.rb +201 -50
  40. data/lib/polars/data_types.rb +6 -4
  41. data/lib/polars/date_time_expr.rb +142 -2
  42. data/lib/polars/expr.rb +70 -10
  43. data/lib/polars/lazy_frame.rb +4 -3
  44. data/lib/polars/lazy_functions.rb +4 -1
  45. data/lib/polars/list_expr.rb +68 -19
  46. data/lib/polars/series.rb +181 -73
  47. data/lib/polars/string_expr.rb +149 -43
  48. data/lib/polars/string_name_space.rb +4 -4
  49. data/lib/polars/struct_name_space.rb +32 -0
  50. data/lib/polars/utils.rb +41 -7
  51. data/lib/polars/version.rb +1 -1
  52. data/lib/polars.rb +2 -2
  53. metadata +26 -11
  54. data/ext/polars/src/lazy/dsl.rs +0 -1775
  55. data/ext/polars/src/lazy/mod.rs +0 -5
  56. data/ext/polars/src/lazy/utils.rs +0 -13
  57. data/ext/polars/src/list_construction.rs +0 -100
  58. /data/ext/polars/src/{numo.rs → series/export.rs} +0 -0
  59. /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,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 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_binary(name: String, val: Wrap<BinaryChunked>, _strict: bool) -> Self {
129
- let mut s = val.0.into_series();
130
- s.rename(&name);
131
- RbSeries::new(s)
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 new_object(name: String, val: RArray, _strict: bool) -> RbResult<Self> {
135
- let val = val
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 new_list(name: String, seq: Value, dtype: Wrap<DataType>) -> RbResult<Self> {
144
- 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
+ }
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 set_sorted(&self, reverse: bool) -> Self {
166
+ pub fn set_sorted_flag(&self, descending: bool) -> Self {
236
167
  let mut out = self.series.borrow().clone();
237
- if reverse {
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,49 +300,81 @@ impl RbSeries {
494
300
  self.series.borrow().len()
495
301
  }
496
302
 
497
- pub fn to_a(&self) -> RArray {
303
+ pub fn to_a(&self) -> Value {
498
304
  let series = &self.series.borrow();
499
305
 
500
- fn to_list_recursive(series: &Series) -> RArray {
306
+ fn to_list_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()
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()
515
348
  }
516
349
  DataType::Date => {
517
350
  let a = RArray::with_capacity(series.len());
518
351
  for v in series.iter() {
519
352
  a.push::<Value>(Wrap(v).into_value()).unwrap();
520
353
  }
521
- return a;
354
+ return a.into_value();
522
355
  }
523
356
  DataType::Datetime(_, _) => {
524
357
  let a = RArray::with_capacity(series.len());
525
358
  for v in series.iter() {
526
359
  a.push::<Value>(Wrap(v).into_value()).unwrap();
527
360
  }
528
- return a;
361
+ return a.into_value();
529
362
  }
530
363
  DataType::Utf8 => {
531
364
  let ca = series.utf8().unwrap();
532
- 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();
533
374
  }
534
375
  DataType::Binary => {
535
- let a = RArray::with_capacity(series.len());
536
- for v in series.iter() {
537
- a.push::<Value>(Wrap(v).into_value()).unwrap();
538
- }
539
- return a;
376
+ let ca = series.binary().unwrap();
377
+ return Wrap(ca).into_value();
540
378
  }
541
379
  DataType::Null | DataType::Unknown => {
542
380
  panic!("to_a not implemented for null/unknown")
@@ -549,32 +387,6 @@ impl RbSeries {
549
387
  to_list_recursive(series)
550
388
  }
551
389
 
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())
576
- }
577
-
578
390
  pub fn clone(&self) -> Self {
579
391
  RbSeries::new(self.series.borrow().clone())
580
392
  }
@@ -906,266 +718,14 @@ impl_set_with_mask!(set_with_mask_i32, i32, i32, Int32);
906
718
  impl_set_with_mask!(set_with_mask_i64, i64, i64, Int64);
907
719
  impl_set_with_mask!(set_with_mask_bool, bool, bool, Boolean);
908
720
 
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
721
  impl RbSeries {
1129
- pub fn new_opt_date(name: String, values: RArray, _strict: Option<bool>) -> RbResult<Self> {
1130
- let len = values.len();
1131
- let mut builder = PrimitiveChunkedBuilder::<Int32Type>::new(&name, len);
1132
- for item in values.each() {
1133
- let v = item?;
1134
- if v.is_nil() {
1135
- builder.append_null();
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()
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)?
1169
729
  .into())
1170
730
  }
1171
731
  }