polars-df 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,8 +3,10 @@ use polars::chunked_array::ops::SortOptions;
3
3
  use polars::lazy::dsl;
4
4
  use polars::lazy::dsl::Operator;
5
5
  use polars::prelude::*;
6
+ use polars::series::ops::NullBehavior;
6
7
 
7
- use crate::conversion::parse_fill_null_strategy;
8
+ use crate::conversion::*;
9
+ use crate::lazy::apply::*;
8
10
  use crate::lazy::utils::rb_exprs_to_exprs;
9
11
  use crate::RbResult;
10
12
 
@@ -21,10 +23,30 @@ impl From<dsl::Expr> for RbExpr {
21
23
  }
22
24
 
23
25
  impl RbExpr {
26
+ pub fn add(&self, rhs: &RbExpr) -> RbResult<Self> {
27
+ Ok(dsl::binary_expr(self.inner.clone(), Operator::Plus, rhs.inner.clone()).into())
28
+ }
29
+
30
+ pub fn sub(&self, rhs: &RbExpr) -> RbResult<Self> {
31
+ Ok(dsl::binary_expr(self.inner.clone(), Operator::Minus, rhs.inner.clone()).into())
32
+ }
33
+
24
34
  pub fn mul(&self, rhs: &RbExpr) -> RbResult<Self> {
25
35
  Ok(dsl::binary_expr(self.inner.clone(), Operator::Multiply, rhs.inner.clone()).into())
26
36
  }
27
37
 
38
+ pub fn truediv(&self, rhs: &RbExpr) -> RbResult<Self> {
39
+ Ok(dsl::binary_expr(self.inner.clone(), Operator::TrueDivide, rhs.inner.clone()).into())
40
+ }
41
+
42
+ pub fn _mod(&self, rhs: &RbExpr) -> RbResult<Self> {
43
+ Ok(dsl::binary_expr(self.inner.clone(), Operator::Modulus, rhs.inner.clone()).into())
44
+ }
45
+
46
+ pub fn floordiv(&self, rhs: &RbExpr) -> RbResult<Self> {
47
+ Ok(dsl::binary_expr(self.inner.clone(), Operator::FloorDivide, rhs.inner.clone()).into())
48
+ }
49
+
28
50
  pub fn to_str(&self) -> String {
29
51
  format!("{:?}", self.inner)
30
52
  }
@@ -69,6 +91,22 @@ impl RbExpr {
69
91
  self.clone().inner.is_not_null().into()
70
92
  }
71
93
 
94
+ pub fn is_infinite(&self) -> Self {
95
+ self.clone().inner.is_infinite().into()
96
+ }
97
+
98
+ pub fn is_finite(&self) -> Self {
99
+ self.clone().inner.is_finite().into()
100
+ }
101
+
102
+ pub fn is_nan(&self) -> Self {
103
+ self.clone().inner.is_nan().into()
104
+ }
105
+
106
+ pub fn is_not_nan(&self) -> Self {
107
+ self.clone().inner.is_not_nan().into()
108
+ }
109
+
72
110
  pub fn min(&self) -> Self {
73
111
  self.clone().inner.min().into()
74
112
  }
@@ -77,6 +115,14 @@ impl RbExpr {
77
115
  self.clone().inner.max().into()
78
116
  }
79
117
 
118
+ pub fn nan_max(&self) -> Self {
119
+ self.clone().inner.nan_max().into()
120
+ }
121
+
122
+ pub fn nan_min(&self) -> Self {
123
+ self.clone().inner.nan_min().into()
124
+ }
125
+
80
126
  pub fn mean(&self) -> Self {
81
127
  self.clone().inner.mean().into()
82
128
  }
@@ -93,6 +139,10 @@ impl RbExpr {
93
139
  self.clone().inner.n_unique().into()
94
140
  }
95
141
 
142
+ pub fn arg_unique(&self) -> Self {
143
+ self.clone().inner.arg_unique().into()
144
+ }
145
+
96
146
  pub fn unique(&self) -> Self {
97
147
  self.clone().inner.unique().into()
98
148
  }
@@ -112,10 +162,46 @@ impl RbExpr {
112
162
  self.clone().inner.list().into()
113
163
  }
114
164
 
165
+ pub fn quantile(&self, quantile: f64, interpolation: Wrap<QuantileInterpolOptions>) -> Self {
166
+ self.clone()
167
+ .inner
168
+ .quantile(quantile, interpolation.0)
169
+ .into()
170
+ }
171
+
172
+ pub fn agg_groups(&self) -> Self {
173
+ self.clone().inner.agg_groups().into()
174
+ }
175
+
115
176
  pub fn count(&self) -> Self {
116
177
  self.clone().inner.count().into()
117
178
  }
118
179
 
180
+ pub fn value_counts(&self, multithreaded: bool, sorted: bool) -> Self {
181
+ self.inner
182
+ .clone()
183
+ .value_counts(multithreaded, sorted)
184
+ .into()
185
+ }
186
+
187
+ pub fn unique_counts(&self) -> Self {
188
+ self.inner.clone().unique_counts().into()
189
+ }
190
+
191
+ pub fn null_count(&self) -> Self {
192
+ self.inner.clone().null_count().into()
193
+ }
194
+
195
+ pub fn cast(&self, data_type: Wrap<DataType>, strict: bool) -> RbResult<Self> {
196
+ let dt = data_type.0;
197
+ let expr = if strict {
198
+ self.inner.clone().strict_cast(dt)
199
+ } else {
200
+ self.inner.clone().cast(dt)
201
+ };
202
+ Ok(expr.into())
203
+ }
204
+
119
205
  pub fn sort_with(&self, descending: bool, nulls_last: bool) -> Self {
120
206
  self.clone()
121
207
  .inner
@@ -126,11 +212,62 @@ impl RbExpr {
126
212
  .into()
127
213
  }
128
214
 
215
+ pub fn arg_sort(&self, reverse: bool, nulls_last: bool) -> Self {
216
+ self.clone()
217
+ .inner
218
+ .arg_sort(SortOptions {
219
+ descending: reverse,
220
+ nulls_last,
221
+ })
222
+ .into()
223
+ }
224
+
225
+ pub fn top_k(&self, k: usize, reverse: bool) -> Self {
226
+ self.inner.clone().top_k(k, reverse).into()
227
+ }
228
+
229
+ pub fn arg_max(&self) -> Self {
230
+ self.clone().inner.arg_max().into()
231
+ }
232
+
233
+ pub fn arg_min(&self) -> Self {
234
+ self.clone().inner.arg_min().into()
235
+ }
236
+
237
+ pub fn search_sorted(&self, element: &RbExpr) -> Self {
238
+ self.inner
239
+ .clone()
240
+ .search_sorted(element.inner.clone())
241
+ .into()
242
+ }
243
+
244
+ pub fn take(&self, idx: &RbExpr) -> Self {
245
+ self.clone().inner.take(idx.inner.clone()).into()
246
+ }
247
+
129
248
  pub fn sort_by(&self, by: RArray, reverse: Vec<bool>) -> RbResult<Self> {
130
249
  let by = rb_exprs_to_exprs(by)?;
131
250
  Ok(self.clone().inner.sort_by(by, reverse).into())
132
251
  }
133
252
 
253
+ pub fn backward_fill(&self, limit: FillNullLimit) -> Self {
254
+ self.clone().inner.backward_fill(limit).into()
255
+ }
256
+
257
+ pub fn forward_fill(&self, limit: FillNullLimit) -> Self {
258
+ self.clone().inner.forward_fill(limit).into()
259
+ }
260
+
261
+ pub fn shift(&self, periods: i64) -> Self {
262
+ self.clone().inner.shift(periods).into()
263
+ }
264
+ pub fn shift_and_fill(&self, periods: i64, fill_value: &RbExpr) -> Self {
265
+ self.clone()
266
+ .inner
267
+ .shift_and_fill(periods, fill_value.inner.clone())
268
+ .into()
269
+ }
270
+
134
271
  pub fn fill_null(&self, expr: &RbExpr) -> Self {
135
272
  self.clone().inner.fill_null(expr.inner.clone()).into()
136
273
  }
@@ -177,6 +314,26 @@ impl RbExpr {
177
314
  self.clone().inner.var(ddof).into()
178
315
  }
179
316
 
317
+ pub fn is_unique(&self) -> Self {
318
+ self.clone().inner.is_unique().into()
319
+ }
320
+
321
+ pub fn is_first(&self) -> Self {
322
+ self.clone().inner.is_first().into()
323
+ }
324
+
325
+ pub fn explode(&self) -> Self {
326
+ self.clone().inner.explode().into()
327
+ }
328
+
329
+ pub fn take_every(&self, n: usize) -> Self {
330
+ self.clone()
331
+ .inner
332
+ .map(move |s: Series| Ok(s.take_every(n)), GetOutput::same_type())
333
+ .with_fmt("take_every")
334
+ .into()
335
+ }
336
+
180
337
  pub fn tail(&self, n: Option<usize>) -> Self {
181
338
  self.clone().inner.tail(n).into()
182
339
  }
@@ -185,6 +342,115 @@ impl RbExpr {
185
342
  self.clone().inner.head(n).into()
186
343
  }
187
344
 
345
+ pub fn slice(&self, offset: &RbExpr, length: &RbExpr) -> Self {
346
+ self.inner
347
+ .clone()
348
+ .slice(offset.inner.clone(), length.inner.clone())
349
+ .into()
350
+ }
351
+
352
+ pub fn append(&self, other: &RbExpr, upcast: bool) -> Self {
353
+ self.inner
354
+ .clone()
355
+ .append(other.inner.clone(), upcast)
356
+ .into()
357
+ }
358
+
359
+ pub fn rechunk(&self) -> Self {
360
+ self.inner
361
+ .clone()
362
+ .map(|s| Ok(s.rechunk()), GetOutput::same_type())
363
+ .into()
364
+ }
365
+
366
+ pub fn round(&self, decimals: u32) -> Self {
367
+ self.clone().inner.round(decimals).into()
368
+ }
369
+
370
+ pub fn floor(&self) -> Self {
371
+ self.clone().inner.floor().into()
372
+ }
373
+
374
+ pub fn ceil(&self) -> Self {
375
+ self.clone().inner.ceil().into()
376
+ }
377
+
378
+ pub fn clip(&self, min: Value, max: Value) -> Self {
379
+ let min = min.try_convert::<Wrap<AnyValue>>().unwrap().0;
380
+ let max = max.try_convert::<Wrap<AnyValue>>().unwrap().0;
381
+ self.clone().inner.clip(min, max).into()
382
+ }
383
+
384
+ pub fn clip_min(&self, min: Value) -> Self {
385
+ let min = min.try_convert::<Wrap<AnyValue>>().unwrap().0;
386
+ self.clone().inner.clip_min(min).into()
387
+ }
388
+
389
+ pub fn clip_max(&self, max: Value) -> Self {
390
+ let max = max.try_convert::<Wrap<AnyValue>>().unwrap().0;
391
+ self.clone().inner.clip_max(max).into()
392
+ }
393
+
394
+ pub fn abs(&self) -> Self {
395
+ self.clone().inner.abs().into()
396
+ }
397
+
398
+ pub fn sin(&self) -> Self {
399
+ self.clone().inner.sin().into()
400
+ }
401
+
402
+ pub fn cos(&self) -> Self {
403
+ self.clone().inner.cos().into()
404
+ }
405
+
406
+ pub fn tan(&self) -> Self {
407
+ self.clone().inner.tan().into()
408
+ }
409
+
410
+ pub fn arcsin(&self) -> Self {
411
+ self.clone().inner.arcsin().into()
412
+ }
413
+
414
+ pub fn arccos(&self) -> Self {
415
+ self.clone().inner.arccos().into()
416
+ }
417
+
418
+ pub fn arctan(&self) -> Self {
419
+ self.clone().inner.arctan().into()
420
+ }
421
+
422
+ pub fn sinh(&self) -> Self {
423
+ self.clone().inner.sinh().into()
424
+ }
425
+
426
+ pub fn cosh(&self) -> Self {
427
+ self.clone().inner.cosh().into()
428
+ }
429
+
430
+ pub fn tanh(&self) -> Self {
431
+ self.clone().inner.tanh().into()
432
+ }
433
+
434
+ pub fn arcsinh(&self) -> Self {
435
+ self.clone().inner.arcsinh().into()
436
+ }
437
+
438
+ pub fn arccosh(&self) -> Self {
439
+ self.clone().inner.arccosh().into()
440
+ }
441
+
442
+ pub fn arctanh(&self) -> Self {
443
+ self.clone().inner.arctanh().into()
444
+ }
445
+
446
+ pub fn sign(&self) -> Self {
447
+ self.clone().inner.sign().into()
448
+ }
449
+
450
+ pub fn is_duplicated(&self) -> Self {
451
+ self.clone().inner.is_duplicated().into()
452
+ }
453
+
188
454
  pub fn over(&self, partition_by: RArray) -> RbResult<Self> {
189
455
  let partition_by = rb_exprs_to_exprs(partition_by)?;
190
456
  Ok(self.clone().inner.over(partition_by).into())
@@ -202,11 +468,130 @@ impl RbExpr {
202
468
  self.clone().inner.or(expr.inner.clone()).into()
203
469
  }
204
470
 
471
+ pub fn is_in(&self, expr: &RbExpr) -> Self {
472
+ self.clone().inner.is_in(expr.inner.clone()).into()
473
+ }
474
+
475
+ pub fn repeat_by(&self, by: &RbExpr) -> Self {
476
+ self.clone().inner.repeat_by(by.inner.clone()).into()
477
+ }
478
+
479
+ pub fn pow(&self, exponent: &RbExpr) -> Self {
480
+ self.clone().inner.pow(exponent.inner.clone()).into()
481
+ }
482
+
483
+ pub fn cumsum(&self, reverse: bool) -> Self {
484
+ self.clone().inner.cumsum(reverse).into()
485
+ }
486
+
487
+ pub fn cummax(&self, reverse: bool) -> Self {
488
+ self.clone().inner.cummax(reverse).into()
489
+ }
490
+
491
+ pub fn cummin(&self, reverse: bool) -> Self {
492
+ self.clone().inner.cummin(reverse).into()
493
+ }
494
+
495
+ pub fn cumprod(&self, reverse: bool) -> Self {
496
+ self.clone().inner.cumprod(reverse).into()
497
+ }
498
+
205
499
  pub fn product(&self) -> Self {
206
500
  self.clone().inner.product().into()
207
501
  }
208
502
 
209
- pub fn str_lengths(&self) -> RbExpr {
503
+ pub fn shrink_dtype(&self) -> Self {
504
+ self.inner.clone().shrink_dtype().into()
505
+ }
506
+
507
+ pub fn str_parse_date(&self, fmt: Option<String>, strict: bool, exact: bool) -> Self {
508
+ self.inner
509
+ .clone()
510
+ .str()
511
+ .strptime(StrpTimeOptions {
512
+ date_dtype: DataType::Date,
513
+ fmt,
514
+ strict,
515
+ exact,
516
+ })
517
+ .into()
518
+ }
519
+
520
+ pub fn str_parse_datetime(&self, fmt: Option<String>, strict: bool, exact: bool) -> Self {
521
+ let tu = match fmt {
522
+ Some(ref fmt) => {
523
+ if fmt.contains("%.9f")
524
+ || fmt.contains("%9f")
525
+ || fmt.contains("%f")
526
+ || fmt.contains("%.f")
527
+ {
528
+ TimeUnit::Nanoseconds
529
+ } else if fmt.contains("%.3f") || fmt.contains("%3f") {
530
+ TimeUnit::Milliseconds
531
+ } else {
532
+ TimeUnit::Microseconds
533
+ }
534
+ }
535
+ None => TimeUnit::Microseconds,
536
+ };
537
+ self.inner
538
+ .clone()
539
+ .str()
540
+ .strptime(StrpTimeOptions {
541
+ date_dtype: DataType::Datetime(tu, None),
542
+ fmt,
543
+ strict,
544
+ exact,
545
+ })
546
+ .into()
547
+ }
548
+
549
+ pub fn str_parse_time(&self, fmt: Option<String>, strict: bool, exact: bool) -> Self {
550
+ self.inner
551
+ .clone()
552
+ .str()
553
+ .strptime(StrpTimeOptions {
554
+ date_dtype: DataType::Time,
555
+ fmt,
556
+ strict,
557
+ exact,
558
+ })
559
+ .into()
560
+ }
561
+
562
+ pub fn str_strip(&self, matches: Option<char>) -> Self {
563
+ self.inner.clone().str().strip(matches).into()
564
+ }
565
+
566
+ pub fn str_rstrip(&self, matches: Option<char>) -> Self {
567
+ self.inner.clone().str().rstrip(matches).into()
568
+ }
569
+
570
+ pub fn str_lstrip(&self, matches: Option<char>) -> Self {
571
+ self.inner.clone().str().lstrip(matches).into()
572
+ }
573
+
574
+ pub fn str_slice(&self, start: i64, length: Option<u64>) -> Self {
575
+ let function = move |s: Series| {
576
+ let ca = s.utf8()?;
577
+ Ok(ca.str_slice(start, length)?.into_series())
578
+ };
579
+ self.clone()
580
+ .inner
581
+ .map(function, GetOutput::from_type(DataType::Utf8))
582
+ .with_fmt("str.slice")
583
+ .into()
584
+ }
585
+
586
+ pub fn str_to_uppercase(&self) -> Self {
587
+ self.inner.clone().str().to_uppercase().into()
588
+ }
589
+
590
+ pub fn str_to_lowercase(&self) -> Self {
591
+ self.inner.clone().str().to_lowercase().into()
592
+ }
593
+
594
+ pub fn str_lengths(&self) -> Self {
210
595
  let function = |s: Series| {
211
596
  let ca = s.utf8()?;
212
597
  Ok(ca.str_lengths().into_series())
@@ -218,6 +603,46 @@ impl RbExpr {
218
603
  .into()
219
604
  }
220
605
 
606
+ pub fn str_n_chars(&self) -> Self {
607
+ let function = |s: Series| {
608
+ let ca = s.utf8()?;
609
+ Ok(ca.str_n_chars().into_series())
610
+ };
611
+ self.clone()
612
+ .inner
613
+ .map(function, GetOutput::from_type(DataType::UInt32))
614
+ .with_fmt("str.n_chars")
615
+ .into()
616
+ }
617
+
618
+ pub fn str_replace(&self, pat: &RbExpr, val: &RbExpr, literal: bool) -> Self {
619
+ self.inner
620
+ .clone()
621
+ .str()
622
+ .replace(pat.inner.clone(), val.inner.clone(), literal)
623
+ .into()
624
+ }
625
+
626
+ pub fn str_replace_all(&self, pat: &RbExpr, val: &RbExpr, literal: bool) -> Self {
627
+ self.inner
628
+ .clone()
629
+ .str()
630
+ .replace_all(pat.inner.clone(), val.inner.clone(), literal)
631
+ .into()
632
+ }
633
+
634
+ pub fn str_zfill(&self, alignment: usize) -> Self {
635
+ self.clone().inner.str().zfill(alignment).into()
636
+ }
637
+
638
+ pub fn str_ljust(&self, width: usize, fillchar: char) -> Self {
639
+ self.clone().inner.str().ljust(width, fillchar).into()
640
+ }
641
+
642
+ pub fn str_rjust(&self, width: usize, fillchar: char) -> Self {
643
+ self.clone().inner.str().rjust(width, fillchar).into()
644
+ }
645
+
221
646
  pub fn str_contains(&self, pat: String, literal: Option<bool>) -> Self {
222
647
  match literal {
223
648
  Some(true) => self.inner.clone().str().contains_literal(pat).into(),
@@ -225,23 +650,717 @@ impl RbExpr {
225
650
  }
226
651
  }
227
652
 
228
- pub fn prefix(&self, prefix: String) -> RbExpr {
653
+ pub fn str_ends_with(&self, sub: String) -> Self {
654
+ self.inner.clone().str().ends_with(sub).into()
655
+ }
656
+
657
+ pub fn str_starts_with(&self, sub: String) -> Self {
658
+ self.inner.clone().str().starts_with(sub).into()
659
+ }
660
+
661
+ pub fn str_extract(&self, pat: String, group_index: usize) -> Self {
662
+ self.inner.clone().str().extract(&pat, group_index).into()
663
+ }
664
+
665
+ pub fn str_extract_all(&self, pat: String) -> Self {
666
+ self.inner.clone().str().extract_all(&pat).into()
667
+ }
668
+
669
+ pub fn count_match(&self, pat: String) -> Self {
670
+ self.inner.clone().str().count_match(&pat).into()
671
+ }
672
+
673
+ pub fn strftime(&self, fmt: String) -> Self {
674
+ self.inner.clone().dt().strftime(&fmt).into()
675
+ }
676
+
677
+ pub fn str_split(&self, by: String) -> Self {
678
+ self.inner.clone().str().split(&by).into()
679
+ }
680
+
681
+ pub fn str_split_inclusive(&self, by: String) -> Self {
682
+ self.inner.clone().str().split_inclusive(&by).into()
683
+ }
684
+
685
+ pub fn str_split_exact(&self, by: String, n: usize) -> Self {
686
+ self.inner.clone().str().split_exact(&by, n).into()
687
+ }
688
+
689
+ pub fn str_split_exact_inclusive(&self, by: String, n: usize) -> Self {
690
+ self.inner
691
+ .clone()
692
+ .str()
693
+ .split_exact_inclusive(&by, n)
694
+ .into()
695
+ }
696
+
697
+ pub fn str_splitn(&self, by: String, n: usize) -> Self {
698
+ self.inner.clone().str().splitn(&by, n).into()
699
+ }
700
+
701
+ pub fn arr_lengths(&self) -> Self {
702
+ self.inner.clone().arr().lengths().into()
703
+ }
704
+
705
+ pub fn arr_contains(&self, other: &RbExpr) -> Self {
706
+ self.inner
707
+ .clone()
708
+ .arr()
709
+ .contains(other.inner.clone())
710
+ .into()
711
+ }
712
+
713
+ pub fn year(&self) -> Self {
714
+ self.clone().inner.dt().year().into()
715
+ }
716
+
717
+ pub fn iso_year(&self) -> Self {
718
+ self.clone().inner.dt().iso_year().into()
719
+ }
720
+
721
+ pub fn quarter(&self) -> Self {
722
+ self.clone().inner.dt().quarter().into()
723
+ }
724
+
725
+ pub fn month(&self) -> Self {
726
+ self.clone().inner.dt().month().into()
727
+ }
728
+
729
+ pub fn week(&self) -> Self {
730
+ self.clone().inner.dt().week().into()
731
+ }
732
+
733
+ pub fn weekday(&self) -> Self {
734
+ self.clone().inner.dt().weekday().into()
735
+ }
736
+
737
+ pub fn day(&self) -> Self {
738
+ self.clone().inner.dt().day().into()
739
+ }
740
+
741
+ pub fn ordinal_day(&self) -> Self {
742
+ self.clone().inner.dt().ordinal_day().into()
743
+ }
744
+
745
+ pub fn hour(&self) -> Self {
746
+ self.clone().inner.dt().hour().into()
747
+ }
748
+
749
+ pub fn minute(&self) -> Self {
750
+ self.clone().inner.dt().minute().into()
751
+ }
752
+
753
+ pub fn second(&self) -> Self {
754
+ self.clone().inner.dt().second().into()
755
+ }
756
+
757
+ pub fn millisecond(&self) -> Self {
758
+ self.clone().inner.dt().millisecond().into()
759
+ }
760
+
761
+ pub fn microsecond(&self) -> Self {
762
+ self.clone().inner.dt().microsecond().into()
763
+ }
764
+
765
+ pub fn nanosecond(&self) -> Self {
766
+ self.clone().inner.dt().nanosecond().into()
767
+ }
768
+
769
+ pub fn duration_days(&self) -> Self {
770
+ self.inner
771
+ .clone()
772
+ .map(
773
+ |s| Ok(s.duration()?.days().into_series()),
774
+ GetOutput::from_type(DataType::Int64),
775
+ )
776
+ .into()
777
+ }
778
+
779
+ pub fn duration_hours(&self) -> Self {
780
+ self.inner
781
+ .clone()
782
+ .map(
783
+ |s| Ok(s.duration()?.hours().into_series()),
784
+ GetOutput::from_type(DataType::Int64),
785
+ )
786
+ .into()
787
+ }
788
+
789
+ pub fn duration_minutes(&self) -> Self {
790
+ self.inner
791
+ .clone()
792
+ .map(
793
+ |s| Ok(s.duration()?.minutes().into_series()),
794
+ GetOutput::from_type(DataType::Int64),
795
+ )
796
+ .into()
797
+ }
798
+
799
+ pub fn duration_seconds(&self) -> Self {
800
+ self.inner
801
+ .clone()
802
+ .map(
803
+ |s| Ok(s.duration()?.seconds().into_series()),
804
+ GetOutput::from_type(DataType::Int64),
805
+ )
806
+ .into()
807
+ }
808
+
809
+ pub fn duration_nanoseconds(&self) -> Self {
810
+ self.inner
811
+ .clone()
812
+ .map(
813
+ |s| Ok(s.duration()?.nanoseconds().into_series()),
814
+ GetOutput::from_type(DataType::Int64),
815
+ )
816
+ .into()
817
+ }
818
+
819
+ pub fn duration_microseconds(&self) -> Self {
820
+ self.inner
821
+ .clone()
822
+ .map(
823
+ |s| Ok(s.duration()?.microseconds().into_series()),
824
+ GetOutput::from_type(DataType::Int64),
825
+ )
826
+ .into()
827
+ }
828
+
829
+ pub fn duration_milliseconds(&self) -> Self {
830
+ self.inner
831
+ .clone()
832
+ .map(
833
+ |s| Ok(s.duration()?.milliseconds().into_series()),
834
+ GetOutput::from_type(DataType::Int64),
835
+ )
836
+ .into()
837
+ }
838
+
839
+ pub fn timestamp(&self, tu: Wrap<TimeUnit>) -> Self {
840
+ self.inner.clone().dt().timestamp(tu.0).into()
841
+ }
842
+
843
+ pub fn dt_offset_by(&self, by: String) -> Self {
844
+ let by = Duration::parse(&by);
845
+ self.inner.clone().dt().offset_by(by).into()
846
+ }
847
+
848
+ pub fn dt_epoch_seconds(&self) -> Self {
849
+ self.clone()
850
+ .inner
851
+ .map(
852
+ |s| {
853
+ s.timestamp(TimeUnit::Milliseconds)
854
+ .map(|ca| (ca / 1000).into_series())
855
+ },
856
+ GetOutput::from_type(DataType::Int64),
857
+ )
858
+ .into()
859
+ }
860
+
861
+ pub fn dt_with_time_unit(&self, tu: Wrap<TimeUnit>) -> Self {
862
+ self.inner.clone().dt().with_time_unit(tu.0).into()
863
+ }
864
+
865
+ pub fn dt_with_time_zone(&self, tz: Option<TimeZone>) -> Self {
866
+ self.inner.clone().dt().with_time_zone(tz).into()
867
+ }
868
+
869
+ pub fn dt_cast_time_unit(&self, tu: Wrap<TimeUnit>) -> Self {
870
+ self.inner.clone().dt().cast_time_unit(tu.0).into()
871
+ }
872
+
873
+ pub fn dt_cast_time_zone(&self, tz: String) -> Self {
874
+ self.inner.clone().dt().cast_time_zone(tz).into()
875
+ }
876
+
877
+ pub fn dt_tz_localize(&self, tz: String) -> Self {
878
+ self.inner.clone().dt().tz_localize(tz).into()
879
+ }
880
+
881
+ pub fn dt_truncate(&self, every: String, offset: String) -> Self {
882
+ self.inner.clone().dt().truncate(&every, &offset).into()
883
+ }
884
+
885
+ pub fn dt_round(&self, every: String, offset: String) -> Self {
886
+ self.inner.clone().dt().round(&every, &offset).into()
887
+ }
888
+
889
+ pub fn mode(&self) -> Self {
890
+ self.inner.clone().mode().into()
891
+ }
892
+
893
+ pub fn keep_name(&self) -> Self {
894
+ self.inner.clone().keep_name().into()
895
+ }
896
+
897
+ pub fn prefix(&self, prefix: String) -> Self {
229
898
  self.inner.clone().prefix(&prefix).into()
230
899
  }
231
900
 
232
- pub fn suffix(&self, suffix: String) -> RbExpr {
901
+ pub fn suffix(&self, suffix: String) -> Self {
233
902
  self.inner.clone().suffix(&suffix).into()
234
903
  }
235
904
 
236
- pub fn interpolate(&self) -> RbExpr {
905
+ pub fn exclude(&self, columns: Vec<String>) -> Self {
906
+ self.inner.clone().exclude(columns).into()
907
+ }
908
+
909
+ pub fn interpolate(&self) -> Self {
237
910
  self.inner.clone().interpolate().into()
238
911
  }
912
+
913
+ pub fn rolling_sum(
914
+ &self,
915
+ window_size: String,
916
+ weights: Option<Vec<f64>>,
917
+ min_periods: usize,
918
+ center: bool,
919
+ by: Option<String>,
920
+ closed: Option<Wrap<ClosedWindow>>,
921
+ ) -> Self {
922
+ let options = RollingOptions {
923
+ window_size: Duration::parse(&window_size),
924
+ weights,
925
+ min_periods,
926
+ center,
927
+ by,
928
+ closed_window: closed.map(|c| c.0),
929
+ };
930
+ self.inner.clone().rolling_sum(options).into()
931
+ }
932
+
933
+ pub fn rolling_min(
934
+ &self,
935
+ window_size: String,
936
+ weights: Option<Vec<f64>>,
937
+ min_periods: usize,
938
+ center: bool,
939
+ by: Option<String>,
940
+ closed: Option<Wrap<ClosedWindow>>,
941
+ ) -> Self {
942
+ let options = RollingOptions {
943
+ window_size: Duration::parse(&window_size),
944
+ weights,
945
+ min_periods,
946
+ center,
947
+ by,
948
+ closed_window: closed.map(|c| c.0),
949
+ };
950
+ self.inner.clone().rolling_min(options).into()
951
+ }
952
+
953
+ pub fn rolling_max(
954
+ &self,
955
+ window_size: String,
956
+ weights: Option<Vec<f64>>,
957
+ min_periods: usize,
958
+ center: bool,
959
+ by: Option<String>,
960
+ closed: Option<Wrap<ClosedWindow>>,
961
+ ) -> Self {
962
+ let options = RollingOptions {
963
+ window_size: Duration::parse(&window_size),
964
+ weights,
965
+ min_periods,
966
+ center,
967
+ by,
968
+ closed_window: closed.map(|c| c.0),
969
+ };
970
+ self.inner.clone().rolling_max(options).into()
971
+ }
972
+
973
+ pub fn rolling_mean(
974
+ &self,
975
+ window_size: String,
976
+ weights: Option<Vec<f64>>,
977
+ min_periods: usize,
978
+ center: bool,
979
+ by: Option<String>,
980
+ closed: Option<Wrap<ClosedWindow>>,
981
+ ) -> Self {
982
+ let options = RollingOptions {
983
+ window_size: Duration::parse(&window_size),
984
+ weights,
985
+ min_periods,
986
+ center,
987
+ by,
988
+ closed_window: closed.map(|c| c.0),
989
+ };
990
+
991
+ self.inner.clone().rolling_mean(options).into()
992
+ }
993
+
994
+ pub fn rolling_std(
995
+ &self,
996
+ window_size: String,
997
+ weights: Option<Vec<f64>>,
998
+ min_periods: usize,
999
+ center: bool,
1000
+ by: Option<String>,
1001
+ closed: Option<Wrap<ClosedWindow>>,
1002
+ ) -> Self {
1003
+ let options = RollingOptions {
1004
+ window_size: Duration::parse(&window_size),
1005
+ weights,
1006
+ min_periods,
1007
+ center,
1008
+ by,
1009
+ closed_window: closed.map(|c| c.0),
1010
+ };
1011
+
1012
+ self.inner.clone().rolling_std(options).into()
1013
+ }
1014
+
1015
+ pub fn rolling_var(
1016
+ &self,
1017
+ window_size: String,
1018
+ weights: Option<Vec<f64>>,
1019
+ min_periods: usize,
1020
+ center: bool,
1021
+ by: Option<String>,
1022
+ closed: Option<Wrap<ClosedWindow>>,
1023
+ ) -> Self {
1024
+ let options = RollingOptions {
1025
+ window_size: Duration::parse(&window_size),
1026
+ weights,
1027
+ min_periods,
1028
+ center,
1029
+ by,
1030
+ closed_window: closed.map(|c| c.0),
1031
+ };
1032
+
1033
+ self.inner.clone().rolling_var(options).into()
1034
+ }
1035
+
1036
+ pub fn rolling_median(
1037
+ &self,
1038
+ window_size: String,
1039
+ weights: Option<Vec<f64>>,
1040
+ min_periods: usize,
1041
+ center: bool,
1042
+ by: Option<String>,
1043
+ closed: Option<Wrap<ClosedWindow>>,
1044
+ ) -> Self {
1045
+ let options = RollingOptions {
1046
+ window_size: Duration::parse(&window_size),
1047
+ weights,
1048
+ min_periods,
1049
+ center,
1050
+ by,
1051
+ closed_window: closed.map(|c| c.0),
1052
+ };
1053
+ self.inner.clone().rolling_median(options).into()
1054
+ }
1055
+
1056
+ #[allow(clippy::too_many_arguments)]
1057
+ pub fn rolling_quantile(
1058
+ &self,
1059
+ quantile: f64,
1060
+ interpolation: Wrap<QuantileInterpolOptions>,
1061
+ window_size: String,
1062
+ weights: Option<Vec<f64>>,
1063
+ min_periods: usize,
1064
+ center: bool,
1065
+ by: Option<String>,
1066
+ closed: Option<Wrap<ClosedWindow>>,
1067
+ ) -> Self {
1068
+ let options = RollingOptions {
1069
+ window_size: Duration::parse(&window_size),
1070
+ weights,
1071
+ min_periods,
1072
+ center,
1073
+ by,
1074
+ closed_window: closed.map(|c| c.0),
1075
+ };
1076
+
1077
+ self.inner
1078
+ .clone()
1079
+ .rolling_quantile(quantile, interpolation.0, options)
1080
+ .into()
1081
+ }
1082
+
1083
+ pub fn rolling_skew(&self, window_size: usize, bias: bool) -> Self {
1084
+ self.inner
1085
+ .clone()
1086
+ .rolling_apply_float(window_size, move |ca| {
1087
+ ca.clone().into_series().skew(bias).unwrap()
1088
+ })
1089
+ .into()
1090
+ }
1091
+
1092
+ pub fn lower_bound(&self) -> Self {
1093
+ self.inner.clone().lower_bound().into()
1094
+ }
1095
+
1096
+ pub fn upper_bound(&self) -> Self {
1097
+ self.inner.clone().upper_bound().into()
1098
+ }
1099
+
1100
+ pub fn lst_max(&self) -> Self {
1101
+ self.inner.clone().arr().max().into()
1102
+ }
1103
+
1104
+ pub fn lst_min(&self) -> Self {
1105
+ self.inner.clone().arr().min().into()
1106
+ }
1107
+
1108
+ pub fn lst_sum(&self) -> Self {
1109
+ self.inner.clone().arr().sum().with_fmt("arr.sum").into()
1110
+ }
1111
+
1112
+ pub fn lst_mean(&self) -> Self {
1113
+ self.inner.clone().arr().mean().with_fmt("arr.mean").into()
1114
+ }
1115
+
1116
+ pub fn lst_sort(&self, reverse: bool) -> Self {
1117
+ self.inner
1118
+ .clone()
1119
+ .arr()
1120
+ .sort(SortOptions {
1121
+ descending: reverse,
1122
+ ..Default::default()
1123
+ })
1124
+ .with_fmt("arr.sort")
1125
+ .into()
1126
+ }
1127
+
1128
+ pub fn lst_reverse(&self) -> Self {
1129
+ self.inner
1130
+ .clone()
1131
+ .arr()
1132
+ .reverse()
1133
+ .with_fmt("arr.reverse")
1134
+ .into()
1135
+ }
1136
+
1137
+ pub fn lst_unique(&self) -> Self {
1138
+ self.inner
1139
+ .clone()
1140
+ .arr()
1141
+ .unique()
1142
+ .with_fmt("arr.unique")
1143
+ .into()
1144
+ }
1145
+
1146
+ pub fn lst_get(&self, index: &RbExpr) -> Self {
1147
+ self.inner.clone().arr().get(index.inner.clone()).into()
1148
+ }
1149
+
1150
+ pub fn lst_join(&self, separator: String) -> Self {
1151
+ self.inner.clone().arr().join(&separator).into()
1152
+ }
1153
+
1154
+ pub fn lst_arg_min(&self) -> Self {
1155
+ self.inner.clone().arr().arg_min().into()
1156
+ }
1157
+
1158
+ pub fn lst_arg_max(&self) -> Self {
1159
+ self.inner.clone().arr().arg_max().into()
1160
+ }
1161
+
1162
+ pub fn lst_diff(&self, n: usize, null_behavior: Wrap<NullBehavior>) -> RbResult<Self> {
1163
+ Ok(self.inner.clone().arr().diff(n, null_behavior.0).into())
1164
+ }
1165
+
1166
+ pub fn lst_shift(&self, periods: i64) -> Self {
1167
+ self.inner.clone().arr().shift(periods).into()
1168
+ }
1169
+
1170
+ pub fn lst_slice(&self, offset: &RbExpr, length: Option<&RbExpr>) -> Self {
1171
+ let length = match length {
1172
+ Some(i) => i.inner.clone(),
1173
+ None => dsl::lit(i64::MAX),
1174
+ };
1175
+ self.inner
1176
+ .clone()
1177
+ .arr()
1178
+ .slice(offset.inner.clone(), length)
1179
+ .into()
1180
+ }
1181
+
1182
+ pub fn lst_eval(&self, expr: &RbExpr, parallel: bool) -> Self {
1183
+ self.inner
1184
+ .clone()
1185
+ .arr()
1186
+ .eval(expr.inner.clone(), parallel)
1187
+ .into()
1188
+ }
1189
+
1190
+ pub fn cumulative_eval(&self, expr: &RbExpr, min_periods: usize, parallel: bool) -> Self {
1191
+ self.inner
1192
+ .clone()
1193
+ .cumulative_eval(expr.inner.clone(), min_periods, parallel)
1194
+ .into()
1195
+ }
1196
+
1197
+ pub fn rank(&self, method: Wrap<RankMethod>, reverse: bool) -> Self {
1198
+ let options = RankOptions {
1199
+ method: method.0,
1200
+ descending: reverse,
1201
+ };
1202
+ self.inner.clone().rank(options).into()
1203
+ }
1204
+
1205
+ pub fn diff(&self, n: usize, null_behavior: Wrap<NullBehavior>) -> Self {
1206
+ self.inner.clone().diff(n, null_behavior.0).into()
1207
+ }
1208
+
1209
+ pub fn pct_change(&self, n: usize) -> Self {
1210
+ self.inner.clone().pct_change(n).into()
1211
+ }
1212
+
1213
+ pub fn skew(&self, bias: bool) -> Self {
1214
+ self.inner.clone().skew(bias).into()
1215
+ }
1216
+
1217
+ pub fn kurtosis(&self, fisher: bool, bias: bool) -> Self {
1218
+ self.inner.clone().kurtosis(fisher, bias).into()
1219
+ }
1220
+
1221
+ pub fn str_concat(&self, delimiter: String) -> Self {
1222
+ self.inner.clone().str().concat(&delimiter).into()
1223
+ }
1224
+
1225
+ pub fn cat_set_ordering(&self, ordering: Wrap<CategoricalOrdering>) -> Self {
1226
+ self.inner.clone().cat().set_ordering(ordering.0).into()
1227
+ }
1228
+
1229
+ pub fn reshape(&self, dims: Vec<i64>) -> Self {
1230
+ self.inner.clone().reshape(&dims).into()
1231
+ }
1232
+
1233
+ pub fn cumcount(&self, reverse: bool) -> Self {
1234
+ self.inner.clone().cumcount(reverse).into()
1235
+ }
1236
+
1237
+ pub fn to_physical(&self) -> Self {
1238
+ self.inner
1239
+ .clone()
1240
+ .map(
1241
+ |s| Ok(s.to_physical_repr().into_owned()),
1242
+ GetOutput::map_dtype(|dt| dt.to_physical()),
1243
+ )
1244
+ .with_fmt("to_physical")
1245
+ .into()
1246
+ }
1247
+
1248
+ pub fn shuffle(&self, seed: Option<u64>) -> Self {
1249
+ self.inner.clone().shuffle(seed).into()
1250
+ }
1251
+
1252
+ pub fn sample_n(
1253
+ &self,
1254
+ n: usize,
1255
+ with_replacement: bool,
1256
+ shuffle: bool,
1257
+ seed: Option<u64>,
1258
+ ) -> Self {
1259
+ self.inner
1260
+ .clone()
1261
+ .sample_n(n, with_replacement, shuffle, seed)
1262
+ .into()
1263
+ }
1264
+
1265
+ pub fn sample_frac(
1266
+ &self,
1267
+ frac: f64,
1268
+ with_replacement: bool,
1269
+ shuffle: bool,
1270
+ seed: Option<u64>,
1271
+ ) -> Self {
1272
+ self.inner
1273
+ .clone()
1274
+ .sample_frac(frac, with_replacement, shuffle, seed)
1275
+ .into()
1276
+ }
1277
+
1278
+ pub fn ewm_mean(&self, alpha: f64, adjust: bool, min_periods: usize) -> Self {
1279
+ let options = EWMOptions {
1280
+ alpha,
1281
+ adjust,
1282
+ bias: false,
1283
+ min_periods,
1284
+ };
1285
+ self.inner.clone().ewm_mean(options).into()
1286
+ }
1287
+
1288
+ pub fn ewm_std(&self, alpha: f64, adjust: bool, bias: bool, min_periods: usize) -> Self {
1289
+ let options = EWMOptions {
1290
+ alpha,
1291
+ adjust,
1292
+ bias,
1293
+ min_periods,
1294
+ };
1295
+ self.inner.clone().ewm_std(options).into()
1296
+ }
1297
+
1298
+ pub fn ewm_var(&self, alpha: f64, adjust: bool, bias: bool, min_periods: usize) -> Self {
1299
+ let options = EWMOptions {
1300
+ alpha,
1301
+ adjust,
1302
+ bias,
1303
+ min_periods,
1304
+ };
1305
+ self.inner.clone().ewm_var(options).into()
1306
+ }
1307
+
1308
+ pub fn any(&self) -> Self {
1309
+ self.inner.clone().any().into()
1310
+ }
1311
+
1312
+ pub fn all(&self) -> Self {
1313
+ self.inner.clone().all().into()
1314
+ }
1315
+
1316
+ pub fn struct_field_by_name(&self, name: String) -> Self {
1317
+ self.inner.clone().struct_().field_by_name(&name).into()
1318
+ }
1319
+
1320
+ pub fn struct_field_by_index(&self, index: i64) -> Self {
1321
+ self.inner.clone().struct_().field_by_index(index).into()
1322
+ }
1323
+
1324
+ pub fn struct_rename_fields(&self, names: Vec<String>) -> Self {
1325
+ self.inner.clone().struct_().rename_fields(names).into()
1326
+ }
1327
+
1328
+ pub fn log(&self, base: f64) -> Self {
1329
+ self.inner.clone().log(base).into()
1330
+ }
1331
+
1332
+ pub fn exp(&self) -> Self {
1333
+ self.inner.clone().exp().into()
1334
+ }
239
1335
  }
240
1336
 
241
1337
  pub fn col(name: String) -> RbExpr {
242
1338
  dsl::col(&name).into()
243
1339
  }
244
1340
 
1341
+ pub fn count() -> RbExpr {
1342
+ dsl::count().into()
1343
+ }
1344
+
1345
+ pub fn first() -> RbExpr {
1346
+ dsl::first().into()
1347
+ }
1348
+
1349
+ pub fn last() -> RbExpr {
1350
+ dsl::last().into()
1351
+ }
1352
+
1353
+ pub fn cols(names: Vec<String>) -> RbExpr {
1354
+ dsl::cols(names).into()
1355
+ }
1356
+
1357
+ pub fn fold(acc: &RbExpr, lambda: Value, exprs: RArray) -> RbResult<RbExpr> {
1358
+ let exprs = rb_exprs_to_exprs(exprs)?;
1359
+
1360
+ let func = move |a: Series, b: Series| binary_lambda(lambda, a, b);
1361
+ Ok(polars::lazy::dsl::fold_exprs(acc.inner.clone(), func, exprs).into())
1362
+ }
1363
+
245
1364
  // TODO improve
246
1365
  pub fn lit(value: Value) -> RbResult<RbExpr> {
247
1366
  if value.is_nil() {
@@ -296,3 +1415,13 @@ impl RbWhenThen {
296
1415
  pub fn when(predicate: &RbExpr) -> RbWhen {
297
1416
  dsl::when(predicate.inner.clone()).into()
298
1417
  }
1418
+
1419
+ pub fn concat_str(s: RArray, sep: String) -> RbResult<RbExpr> {
1420
+ let s = rb_exprs_to_exprs(s)?;
1421
+ Ok(dsl::concat_str(s, &sep).into())
1422
+ }
1423
+
1424
+ pub fn concat_lst(s: RArray) -> RbResult<RbExpr> {
1425
+ let s = rb_exprs_to_exprs(s)?;
1426
+ Ok(dsl::concat_lst(s).into())
1427
+ }