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.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/Cargo.lock +447 -410
  4. data/Cargo.toml +0 -1
  5. data/README.md +6 -5
  6. data/ext/polars/Cargo.toml +10 -5
  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 +8 -3
  10. data/ext/polars/src/batched_csv.rs +7 -5
  11. data/ext/polars/src/conversion.rs +269 -59
  12. data/ext/polars/src/dataframe.rs +38 -40
  13. data/ext/polars/src/error.rs +6 -2
  14. data/ext/polars/src/expr/array.rs +15 -0
  15. data/ext/polars/src/expr/binary.rs +69 -0
  16. data/ext/polars/src/expr/categorical.rs +10 -0
  17. data/ext/polars/src/expr/datetime.rs +223 -0
  18. data/ext/polars/src/expr/general.rs +963 -0
  19. data/ext/polars/src/expr/list.rs +151 -0
  20. data/ext/polars/src/{lazy → expr}/meta.rs +16 -6
  21. data/ext/polars/src/expr/string.rs +314 -0
  22. data/ext/polars/src/expr/struct.rs +15 -0
  23. data/ext/polars/src/expr.rs +34 -0
  24. data/ext/polars/src/functions/eager.rs +93 -0
  25. data/ext/polars/src/functions/io.rs +34 -0
  26. data/ext/polars/src/functions/lazy.rs +249 -0
  27. data/ext/polars/src/functions/meta.rs +8 -0
  28. data/ext/polars/src/functions/mod.rs +5 -0
  29. data/ext/polars/src/functions/whenthen.rs +43 -0
  30. data/ext/polars/src/{lazy/dataframe.rs → lazyframe.rs} +26 -35
  31. data/ext/polars/src/lazygroupby.rs +29 -0
  32. data/ext/polars/src/lib.rs +223 -316
  33. data/ext/polars/src/object.rs +1 -1
  34. data/ext/polars/src/rb_modules.rs +12 -0
  35. data/ext/polars/src/series/aggregation.rs +83 -0
  36. data/ext/polars/src/series/arithmetic.rs +88 -0
  37. data/ext/polars/src/series/comparison.rs +251 -0
  38. data/ext/polars/src/series/construction.rs +190 -0
  39. data/ext/polars/src/series.rs +151 -551
  40. data/lib/polars/array_expr.rb +84 -0
  41. data/lib/polars/array_name_space.rb +77 -0
  42. data/lib/polars/batched_csv_reader.rb +1 -1
  43. data/lib/polars/convert.rb +2 -2
  44. data/lib/polars/data_frame.rb +289 -96
  45. data/lib/polars/data_types.rb +169 -33
  46. data/lib/polars/date_time_expr.rb +142 -2
  47. data/lib/polars/date_time_name_space.rb +17 -3
  48. data/lib/polars/expr.rb +145 -78
  49. data/lib/polars/functions.rb +0 -1
  50. data/lib/polars/group_by.rb +1 -22
  51. data/lib/polars/lazy_frame.rb +84 -31
  52. data/lib/polars/lazy_functions.rb +71 -32
  53. data/lib/polars/list_expr.rb +94 -45
  54. data/lib/polars/list_name_space.rb +13 -13
  55. data/lib/polars/rolling_group_by.rb +4 -2
  56. data/lib/polars/series.rb +249 -87
  57. data/lib/polars/string_expr.rb +277 -45
  58. data/lib/polars/string_name_space.rb +137 -22
  59. data/lib/polars/struct_name_space.rb +32 -0
  60. data/lib/polars/utils.rb +138 -54
  61. data/lib/polars/version.rb +1 -1
  62. data/lib/polars.rb +5 -2
  63. metadata +29 -11
  64. data/ext/polars/src/lazy/dsl.rs +0 -1775
  65. data/ext/polars/src/lazy/mod.rs +0 -5
  66. data/ext/polars/src/lazy/utils.rs +0 -13
  67. data/ext/polars/src/list_construction.rs +0 -100
  68. /data/ext/polars/src/{numo.rs → series/export.rs} +0 -0
  69. /data/ext/polars/src/{set.rs → series/set_at_idx.rs} +0 -0
@@ -1,1775 +0,0 @@
1
- use magnus::block::Proc;
2
- use magnus::{class, IntoValue, RArray, RString, Value};
3
- use polars::chunked_array::ops::SortOptions;
4
- use polars::lazy::dsl;
5
- use polars::lazy::dsl::Operator;
6
- use polars::prelude::*;
7
- use polars::series::ops::NullBehavior;
8
-
9
- use crate::conversion::*;
10
- use crate::lazy::apply::*;
11
- use crate::lazy::utils::rb_exprs_to_exprs;
12
- use crate::utils::reinterpret;
13
- use crate::{RbPolarsErr, RbResult, RbSeries};
14
-
15
- #[magnus::wrap(class = "Polars::RbExpr")]
16
- #[derive(Clone)]
17
- pub struct RbExpr {
18
- pub inner: dsl::Expr,
19
- }
20
-
21
- impl From<dsl::Expr> for RbExpr {
22
- fn from(inner: dsl::Expr) -> Self {
23
- RbExpr { inner }
24
- }
25
- }
26
-
27
- impl RbExpr {
28
- pub fn add(&self, rhs: &RbExpr) -> RbResult<Self> {
29
- Ok(dsl::binary_expr(self.inner.clone(), Operator::Plus, rhs.inner.clone()).into())
30
- }
31
-
32
- pub fn sub(&self, rhs: &RbExpr) -> RbResult<Self> {
33
- Ok(dsl::binary_expr(self.inner.clone(), Operator::Minus, rhs.inner.clone()).into())
34
- }
35
-
36
- pub fn mul(&self, rhs: &RbExpr) -> RbResult<Self> {
37
- Ok(dsl::binary_expr(self.inner.clone(), Operator::Multiply, rhs.inner.clone()).into())
38
- }
39
-
40
- pub fn truediv(&self, rhs: &RbExpr) -> RbResult<Self> {
41
- Ok(dsl::binary_expr(self.inner.clone(), Operator::TrueDivide, rhs.inner.clone()).into())
42
- }
43
-
44
- pub fn _mod(&self, rhs: &RbExpr) -> RbResult<Self> {
45
- Ok(dsl::binary_expr(self.inner.clone(), Operator::Modulus, rhs.inner.clone()).into())
46
- }
47
-
48
- pub fn floordiv(&self, rhs: &RbExpr) -> RbResult<Self> {
49
- Ok(dsl::binary_expr(self.inner.clone(), Operator::FloorDivide, rhs.inner.clone()).into())
50
- }
51
-
52
- pub fn to_str(&self) -> String {
53
- format!("{:?}", self.inner)
54
- }
55
-
56
- pub fn eq(&self, other: &RbExpr) -> Self {
57
- self.clone().inner.eq(other.inner.clone()).into()
58
- }
59
-
60
- pub fn neq(&self, other: &RbExpr) -> Self {
61
- self.clone().inner.neq(other.inner.clone()).into()
62
- }
63
-
64
- pub fn gt(&self, other: &RbExpr) -> Self {
65
- self.clone().inner.gt(other.inner.clone()).into()
66
- }
67
-
68
- pub fn gt_eq(&self, other: &RbExpr) -> Self {
69
- self.clone().inner.gt_eq(other.inner.clone()).into()
70
- }
71
-
72
- pub fn lt_eq(&self, other: &RbExpr) -> Self {
73
- self.clone().inner.lt_eq(other.inner.clone()).into()
74
- }
75
-
76
- pub fn lt(&self, other: &RbExpr) -> Self {
77
- self.clone().inner.lt(other.inner.clone()).into()
78
- }
79
-
80
- pub fn alias(&self, name: String) -> Self {
81
- self.clone().inner.alias(&name).into()
82
- }
83
-
84
- pub fn is_not(&self) -> Self {
85
- self.clone().inner.not().into()
86
- }
87
-
88
- pub fn is_null(&self) -> Self {
89
- self.clone().inner.is_null().into()
90
- }
91
-
92
- pub fn is_not_null(&self) -> Self {
93
- self.clone().inner.is_not_null().into()
94
- }
95
-
96
- pub fn is_infinite(&self) -> Self {
97
- self.clone().inner.is_infinite().into()
98
- }
99
-
100
- pub fn is_finite(&self) -> Self {
101
- self.clone().inner.is_finite().into()
102
- }
103
-
104
- pub fn is_nan(&self) -> Self {
105
- self.clone().inner.is_nan().into()
106
- }
107
-
108
- pub fn is_not_nan(&self) -> Self {
109
- self.clone().inner.is_not_nan().into()
110
- }
111
-
112
- pub fn min(&self) -> Self {
113
- self.clone().inner.min().into()
114
- }
115
-
116
- pub fn max(&self) -> Self {
117
- self.clone().inner.max().into()
118
- }
119
-
120
- pub fn nan_max(&self) -> Self {
121
- self.clone().inner.nan_max().into()
122
- }
123
-
124
- pub fn nan_min(&self) -> Self {
125
- self.clone().inner.nan_min().into()
126
- }
127
-
128
- pub fn mean(&self) -> Self {
129
- self.clone().inner.mean().into()
130
- }
131
-
132
- pub fn median(&self) -> Self {
133
- self.clone().inner.median().into()
134
- }
135
-
136
- pub fn sum(&self) -> Self {
137
- self.clone().inner.sum().into()
138
- }
139
-
140
- pub fn n_unique(&self) -> Self {
141
- self.clone().inner.n_unique().into()
142
- }
143
-
144
- pub fn arg_unique(&self) -> Self {
145
- self.clone().inner.arg_unique().into()
146
- }
147
-
148
- pub fn unique(&self) -> Self {
149
- self.clone().inner.unique().into()
150
- }
151
-
152
- pub fn unique_stable(&self) -> Self {
153
- self.clone().inner.unique_stable().into()
154
- }
155
-
156
- pub fn first(&self) -> Self {
157
- self.clone().inner.first().into()
158
- }
159
- pub fn last(&self) -> Self {
160
- self.clone().inner.last().into()
161
- }
162
-
163
- pub fn list(&self) -> Self {
164
- self.clone().inner.list().into()
165
- }
166
-
167
- pub fn quantile(
168
- &self,
169
- quantile: &RbExpr,
170
- interpolation: Wrap<QuantileInterpolOptions>,
171
- ) -> Self {
172
- self.clone()
173
- .inner
174
- .quantile(quantile.inner.clone(), interpolation.0)
175
- .into()
176
- }
177
-
178
- pub fn agg_groups(&self) -> Self {
179
- self.clone().inner.agg_groups().into()
180
- }
181
-
182
- pub fn count(&self) -> Self {
183
- self.clone().inner.count().into()
184
- }
185
-
186
- pub fn value_counts(&self, multithreaded: bool, sorted: bool) -> Self {
187
- self.inner
188
- .clone()
189
- .value_counts(multithreaded, sorted)
190
- .into()
191
- }
192
-
193
- pub fn unique_counts(&self) -> Self {
194
- self.inner.clone().unique_counts().into()
195
- }
196
-
197
- pub fn null_count(&self) -> Self {
198
- self.inner.clone().null_count().into()
199
- }
200
-
201
- pub fn cast(&self, data_type: Wrap<DataType>, strict: bool) -> RbResult<Self> {
202
- let dt = data_type.0;
203
- let expr = if strict {
204
- self.inner.clone().strict_cast(dt)
205
- } else {
206
- self.inner.clone().cast(dt)
207
- };
208
- Ok(expr.into())
209
- }
210
-
211
- pub fn sort_with(&self, descending: bool, nulls_last: bool) -> Self {
212
- self.clone()
213
- .inner
214
- .sort_with(SortOptions {
215
- descending,
216
- nulls_last,
217
- multithreaded: true,
218
- })
219
- .into()
220
- }
221
-
222
- pub fn arg_sort(&self, reverse: bool, nulls_last: bool) -> Self {
223
- self.clone()
224
- .inner
225
- .arg_sort(SortOptions {
226
- descending: reverse,
227
- nulls_last,
228
- multithreaded: true,
229
- })
230
- .into()
231
- }
232
-
233
- pub fn top_k(&self, k: usize, reverse: bool) -> Self {
234
- self.inner.clone().top_k(k, reverse).into()
235
- }
236
-
237
- pub fn arg_max(&self) -> Self {
238
- self.clone().inner.arg_max().into()
239
- }
240
-
241
- pub fn arg_min(&self) -> Self {
242
- self.clone().inner.arg_min().into()
243
- }
244
-
245
- pub fn search_sorted(&self, element: &RbExpr, side: Wrap<SearchSortedSide>) -> Self {
246
- self.inner
247
- .clone()
248
- .search_sorted(element.inner.clone(), side.0)
249
- .into()
250
- }
251
-
252
- pub fn take(&self, idx: &RbExpr) -> Self {
253
- self.clone().inner.take(idx.inner.clone()).into()
254
- }
255
-
256
- pub fn sort_by(&self, by: RArray, reverse: Vec<bool>) -> RbResult<Self> {
257
- let by = rb_exprs_to_exprs(by)?;
258
- Ok(self.clone().inner.sort_by(by, reverse).into())
259
- }
260
-
261
- pub fn backward_fill(&self, limit: FillNullLimit) -> Self {
262
- self.clone().inner.backward_fill(limit).into()
263
- }
264
-
265
- pub fn forward_fill(&self, limit: FillNullLimit) -> Self {
266
- self.clone().inner.forward_fill(limit).into()
267
- }
268
-
269
- pub fn shift(&self, periods: i64) -> Self {
270
- self.clone().inner.shift(periods).into()
271
- }
272
- pub fn shift_and_fill(&self, periods: i64, fill_value: &RbExpr) -> Self {
273
- self.clone()
274
- .inner
275
- .shift_and_fill(periods, fill_value.inner.clone())
276
- .into()
277
- }
278
-
279
- pub fn fill_null(&self, expr: &RbExpr) -> Self {
280
- self.clone().inner.fill_null(expr.inner.clone()).into()
281
- }
282
-
283
- pub fn fill_null_with_strategy(
284
- &self,
285
- strategy: String,
286
- limit: FillNullLimit,
287
- ) -> RbResult<Self> {
288
- let strat = parse_fill_null_strategy(&strategy, limit)?;
289
- Ok(self
290
- .inner
291
- .clone()
292
- .apply(
293
- move |s| s.fill_null(strat).map(Some),
294
- GetOutput::same_type(),
295
- )
296
- .with_fmt("fill_null_with_strategy")
297
- .into())
298
- }
299
-
300
- pub fn fill_nan(&self, expr: &RbExpr) -> Self {
301
- self.inner.clone().fill_nan(expr.inner.clone()).into()
302
- }
303
-
304
- pub fn drop_nulls(&self) -> Self {
305
- self.inner.clone().drop_nulls().into()
306
- }
307
-
308
- pub fn drop_nans(&self) -> Self {
309
- self.inner.clone().drop_nans().into()
310
- }
311
-
312
- pub fn filter(&self, predicate: &RbExpr) -> Self {
313
- self.clone().inner.filter(predicate.inner.clone()).into()
314
- }
315
-
316
- pub fn reverse(&self) -> Self {
317
- self.clone().inner.reverse().into()
318
- }
319
-
320
- pub fn std(&self, ddof: u8) -> Self {
321
- self.clone().inner.std(ddof).into()
322
- }
323
-
324
- pub fn var(&self, ddof: u8) -> Self {
325
- self.clone().inner.var(ddof).into()
326
- }
327
-
328
- pub fn is_unique(&self) -> Self {
329
- self.clone().inner.is_unique().into()
330
- }
331
-
332
- pub fn is_first(&self) -> Self {
333
- self.clone().inner.is_first().into()
334
- }
335
-
336
- pub fn explode(&self) -> Self {
337
- self.clone().inner.explode().into()
338
- }
339
-
340
- pub fn take_every(&self, n: usize) -> Self {
341
- self.clone()
342
- .inner
343
- .map(
344
- move |s: Series| Ok(Some(s.take_every(n))),
345
- GetOutput::same_type(),
346
- )
347
- .with_fmt("take_every")
348
- .into()
349
- }
350
-
351
- pub fn tail(&self, n: Option<usize>) -> Self {
352
- self.clone().inner.tail(n).into()
353
- }
354
-
355
- pub fn head(&self, n: Option<usize>) -> Self {
356
- self.clone().inner.head(n).into()
357
- }
358
-
359
- pub fn slice(&self, offset: &RbExpr, length: &RbExpr) -> Self {
360
- self.inner
361
- .clone()
362
- .slice(offset.inner.clone(), length.inner.clone())
363
- .into()
364
- }
365
-
366
- pub fn append(&self, other: &RbExpr, upcast: bool) -> Self {
367
- self.inner
368
- .clone()
369
- .append(other.inner.clone(), upcast)
370
- .into()
371
- }
372
-
373
- pub fn rechunk(&self) -> Self {
374
- self.inner
375
- .clone()
376
- .map(|s| Ok(Some(s.rechunk())), GetOutput::same_type())
377
- .into()
378
- }
379
-
380
- pub fn round(&self, decimals: u32) -> Self {
381
- self.clone().inner.round(decimals).into()
382
- }
383
-
384
- pub fn floor(&self) -> Self {
385
- self.clone().inner.floor().into()
386
- }
387
-
388
- pub fn ceil(&self) -> Self {
389
- self.clone().inner.ceil().into()
390
- }
391
-
392
- pub fn clip(&self, min: Value, max: Value) -> Self {
393
- let min = min.try_convert::<Wrap<AnyValue>>().unwrap().0;
394
- let max = max.try_convert::<Wrap<AnyValue>>().unwrap().0;
395
- self.clone().inner.clip(min, max).into()
396
- }
397
-
398
- pub fn clip_min(&self, min: Value) -> Self {
399
- let min = min.try_convert::<Wrap<AnyValue>>().unwrap().0;
400
- self.clone().inner.clip_min(min).into()
401
- }
402
-
403
- pub fn clip_max(&self, max: Value) -> Self {
404
- let max = max.try_convert::<Wrap<AnyValue>>().unwrap().0;
405
- self.clone().inner.clip_max(max).into()
406
- }
407
-
408
- pub fn abs(&self) -> Self {
409
- self.clone().inner.abs().into()
410
- }
411
-
412
- pub fn sin(&self) -> Self {
413
- self.clone().inner.sin().into()
414
- }
415
-
416
- pub fn cos(&self) -> Self {
417
- self.clone().inner.cos().into()
418
- }
419
-
420
- pub fn tan(&self) -> Self {
421
- self.clone().inner.tan().into()
422
- }
423
-
424
- pub fn arcsin(&self) -> Self {
425
- self.clone().inner.arcsin().into()
426
- }
427
-
428
- pub fn arccos(&self) -> Self {
429
- self.clone().inner.arccos().into()
430
- }
431
-
432
- pub fn arctan(&self) -> Self {
433
- self.clone().inner.arctan().into()
434
- }
435
-
436
- pub fn sinh(&self) -> Self {
437
- self.clone().inner.sinh().into()
438
- }
439
-
440
- pub fn cosh(&self) -> Self {
441
- self.clone().inner.cosh().into()
442
- }
443
-
444
- pub fn tanh(&self) -> Self {
445
- self.clone().inner.tanh().into()
446
- }
447
-
448
- pub fn arcsinh(&self) -> Self {
449
- self.clone().inner.arcsinh().into()
450
- }
451
-
452
- pub fn arccosh(&self) -> Self {
453
- self.clone().inner.arccosh().into()
454
- }
455
-
456
- pub fn arctanh(&self) -> Self {
457
- self.clone().inner.arctanh().into()
458
- }
459
-
460
- pub fn sign(&self) -> Self {
461
- self.clone().inner.sign().into()
462
- }
463
-
464
- pub fn is_duplicated(&self) -> Self {
465
- self.clone().inner.is_duplicated().into()
466
- }
467
-
468
- pub fn over(&self, partition_by: RArray) -> RbResult<Self> {
469
- let partition_by = rb_exprs_to_exprs(partition_by)?;
470
- Ok(self.clone().inner.over(partition_by).into())
471
- }
472
-
473
- pub fn _and(&self, expr: &RbExpr) -> Self {
474
- self.clone().inner.and(expr.inner.clone()).into()
475
- }
476
-
477
- pub fn _xor(&self, expr: &RbExpr) -> Self {
478
- self.clone().inner.xor(expr.inner.clone()).into()
479
- }
480
-
481
- pub fn _or(&self, expr: &RbExpr) -> Self {
482
- self.clone().inner.or(expr.inner.clone()).into()
483
- }
484
-
485
- pub fn is_in(&self, expr: &RbExpr) -> Self {
486
- self.clone().inner.is_in(expr.inner.clone()).into()
487
- }
488
-
489
- pub fn repeat_by(&self, by: &RbExpr) -> Self {
490
- self.clone().inner.repeat_by(by.inner.clone()).into()
491
- }
492
-
493
- pub fn pow(&self, exponent: &RbExpr) -> Self {
494
- self.clone().inner.pow(exponent.inner.clone()).into()
495
- }
496
-
497
- pub fn cumsum(&self, reverse: bool) -> Self {
498
- self.clone().inner.cumsum(reverse).into()
499
- }
500
-
501
- pub fn cummax(&self, reverse: bool) -> Self {
502
- self.clone().inner.cummax(reverse).into()
503
- }
504
-
505
- pub fn cummin(&self, reverse: bool) -> Self {
506
- self.clone().inner.cummin(reverse).into()
507
- }
508
-
509
- pub fn cumprod(&self, reverse: bool) -> Self {
510
- self.clone().inner.cumprod(reverse).into()
511
- }
512
-
513
- pub fn product(&self) -> Self {
514
- self.clone().inner.product().into()
515
- }
516
-
517
- pub fn shrink_dtype(&self) -> Self {
518
- self.inner.clone().shrink_dtype().into()
519
- }
520
-
521
- pub fn str_parse_date(
522
- &self,
523
- fmt: Option<String>,
524
- strict: bool,
525
- exact: bool,
526
- cache: bool,
527
- ) -> Self {
528
- self.inner
529
- .clone()
530
- .str()
531
- .strptime(StrpTimeOptions {
532
- date_dtype: DataType::Date,
533
- fmt,
534
- strict,
535
- exact,
536
- cache,
537
- tz_aware: false,
538
- utc: false,
539
- })
540
- .into()
541
- }
542
-
543
- pub fn str_parse_datetime(
544
- &self,
545
- fmt: Option<String>,
546
- strict: bool,
547
- exact: bool,
548
- cache: bool,
549
- tz_aware: bool,
550
- utc: bool,
551
- ) -> Self {
552
- let tu = match fmt {
553
- Some(ref fmt) => {
554
- if fmt.contains("%.9f")
555
- || fmt.contains("%9f")
556
- || fmt.contains("%f")
557
- || fmt.contains("%.f")
558
- {
559
- TimeUnit::Nanoseconds
560
- } else if fmt.contains("%.3f") || fmt.contains("%3f") {
561
- TimeUnit::Milliseconds
562
- } else {
563
- TimeUnit::Microseconds
564
- }
565
- }
566
- None => TimeUnit::Microseconds,
567
- };
568
- self.inner
569
- .clone()
570
- .str()
571
- .strptime(StrpTimeOptions {
572
- date_dtype: DataType::Datetime(tu, None),
573
- fmt,
574
- strict,
575
- exact,
576
- cache,
577
- tz_aware,
578
- utc,
579
- })
580
- .into()
581
- }
582
-
583
- pub fn str_parse_time(
584
- &self,
585
- fmt: Option<String>,
586
- strict: bool,
587
- exact: bool,
588
- cache: bool,
589
- ) -> Self {
590
- self.inner
591
- .clone()
592
- .str()
593
- .strptime(StrpTimeOptions {
594
- date_dtype: DataType::Time,
595
- fmt,
596
- strict,
597
- exact,
598
- cache,
599
- tz_aware: false,
600
- utc: false,
601
- })
602
- .into()
603
- }
604
-
605
- pub fn str_strip(&self, matches: Option<String>) -> Self {
606
- self.inner.clone().str().strip(matches).into()
607
- }
608
-
609
- pub fn str_rstrip(&self, matches: Option<String>) -> Self {
610
- self.inner.clone().str().rstrip(matches).into()
611
- }
612
-
613
- pub fn str_lstrip(&self, matches: Option<String>) -> Self {
614
- self.inner.clone().str().lstrip(matches).into()
615
- }
616
-
617
- pub fn str_slice(&self, start: i64, length: Option<u64>) -> Self {
618
- let function = move |s: Series| {
619
- let ca = s.utf8()?;
620
- Ok(Some(ca.str_slice(start, length)?.into_series()))
621
- };
622
- self.clone()
623
- .inner
624
- .map(function, GetOutput::from_type(DataType::Utf8))
625
- .with_fmt("str.slice")
626
- .into()
627
- }
628
-
629
- pub fn str_to_uppercase(&self) -> Self {
630
- self.inner.clone().str().to_uppercase().into()
631
- }
632
-
633
- pub fn str_to_lowercase(&self) -> Self {
634
- self.inner.clone().str().to_lowercase().into()
635
- }
636
-
637
- pub fn str_lengths(&self) -> Self {
638
- let function = |s: Series| {
639
- let ca = s.utf8()?;
640
- Ok(Some(ca.str_lengths().into_series()))
641
- };
642
- self.clone()
643
- .inner
644
- .map(function, GetOutput::from_type(DataType::UInt32))
645
- .with_fmt("str.lengths")
646
- .into()
647
- }
648
-
649
- pub fn str_n_chars(&self) -> Self {
650
- let function = |s: Series| {
651
- let ca = s.utf8()?;
652
- Ok(Some(ca.str_n_chars().into_series()))
653
- };
654
- self.clone()
655
- .inner
656
- .map(function, GetOutput::from_type(DataType::UInt32))
657
- .with_fmt("str.n_chars")
658
- .into()
659
- }
660
-
661
- pub fn str_replace(&self, pat: &RbExpr, val: &RbExpr, literal: bool) -> Self {
662
- self.inner
663
- .clone()
664
- .str()
665
- .replace(pat.inner.clone(), val.inner.clone(), literal)
666
- .into()
667
- }
668
-
669
- pub fn str_replace_all(&self, pat: &RbExpr, val: &RbExpr, literal: bool) -> Self {
670
- self.inner
671
- .clone()
672
- .str()
673
- .replace_all(pat.inner.clone(), val.inner.clone(), literal)
674
- .into()
675
- }
676
-
677
- pub fn str_zfill(&self, alignment: usize) -> Self {
678
- self.clone().inner.str().zfill(alignment).into()
679
- }
680
-
681
- pub fn str_ljust(&self, width: usize, fillchar: char) -> Self {
682
- self.clone().inner.str().ljust(width, fillchar).into()
683
- }
684
-
685
- pub fn str_rjust(&self, width: usize, fillchar: char) -> Self {
686
- self.clone().inner.str().rjust(width, fillchar).into()
687
- }
688
-
689
- pub fn str_contains(&self, pat: &RbExpr, literal: Option<bool>, strict: bool) -> Self {
690
- match literal {
691
- Some(true) => self
692
- .inner
693
- .clone()
694
- .str()
695
- .contains_literal(pat.inner.clone())
696
- .into(),
697
- _ => self
698
- .inner
699
- .clone()
700
- .str()
701
- .contains(pat.inner.clone(), strict)
702
- .into(),
703
- }
704
- }
705
-
706
- pub fn str_ends_with(&self, sub: &RbExpr) -> Self {
707
- self.inner.clone().str().ends_with(sub.inner.clone()).into()
708
- }
709
-
710
- pub fn str_starts_with(&self, sub: &RbExpr) -> Self {
711
- self.inner
712
- .clone()
713
- .str()
714
- .starts_with(sub.inner.clone())
715
- .into()
716
- }
717
-
718
- pub fn binary_contains(&self, lit: Vec<u8>) -> Self {
719
- self.inner.clone().binary().contains_literal(lit).into()
720
- }
721
-
722
- pub fn binary_ends_with(&self, sub: Vec<u8>) -> Self {
723
- self.inner.clone().binary().ends_with(sub).into()
724
- }
725
-
726
- pub fn binary_starts_with(&self, sub: Vec<u8>) -> Self {
727
- self.inner.clone().binary().starts_with(sub).into()
728
- }
729
-
730
- pub fn str_hex_encode(&self) -> Self {
731
- self.clone()
732
- .inner
733
- .map(
734
- move |s| s.utf8().map(|s| Some(s.hex_encode().into_series())),
735
- GetOutput::same_type(),
736
- )
737
- .with_fmt("str.hex_encode")
738
- .into()
739
- }
740
-
741
- pub fn str_hex_decode(&self, strict: bool) -> Self {
742
- self.clone()
743
- .inner
744
- .map(
745
- move |s| s.utf8()?.hex_decode(strict).map(|s| Some(s.into_series())),
746
- GetOutput::same_type(),
747
- )
748
- .with_fmt("str.hex_decode")
749
- .into()
750
- }
751
-
752
- pub fn str_base64_encode(&self) -> Self {
753
- self.clone()
754
- .inner
755
- .map(
756
- move |s| s.utf8().map(|s| Some(s.base64_encode().into_series())),
757
- GetOutput::same_type(),
758
- )
759
- .with_fmt("str.base64_encode")
760
- .into()
761
- }
762
-
763
- pub fn str_base64_decode(&self, strict: bool) -> Self {
764
- self.clone()
765
- .inner
766
- .map(
767
- move |s| {
768
- s.utf8()?
769
- .base64_decode(strict)
770
- .map(|s| Some(s.into_series()))
771
- },
772
- GetOutput::same_type(),
773
- )
774
- .with_fmt("str.base64_decode")
775
- .into()
776
- }
777
-
778
- pub fn binary_hex_encode(&self) -> Self {
779
- self.clone()
780
- .inner
781
- .map(
782
- move |s| s.binary().map(|s| Some(s.hex_encode().into_series())),
783
- GetOutput::same_type(),
784
- )
785
- .with_fmt("binary.hex_encode")
786
- .into()
787
- }
788
-
789
- pub fn binary_hex_decode(&self, strict: bool) -> Self {
790
- self.clone()
791
- .inner
792
- .map(
793
- move |s| {
794
- s.binary()?
795
- .hex_decode(strict)
796
- .map(|s| Some(s.into_series()))
797
- },
798
- GetOutput::same_type(),
799
- )
800
- .with_fmt("binary.hex_decode")
801
- .into()
802
- }
803
-
804
- pub fn binary_base64_encode(&self) -> Self {
805
- self.clone()
806
- .inner
807
- .map(
808
- move |s| s.binary().map(|s| Some(s.base64_encode().into_series())),
809
- GetOutput::same_type(),
810
- )
811
- .with_fmt("binary.base64_encode")
812
- .into()
813
- }
814
-
815
- pub fn binary_base64_decode(&self, strict: bool) -> Self {
816
- self.clone()
817
- .inner
818
- .map(
819
- move |s| {
820
- s.binary()?
821
- .base64_decode(strict)
822
- .map(|s| Some(s.into_series()))
823
- },
824
- GetOutput::same_type(),
825
- )
826
- .with_fmt("binary.base64_decode")
827
- .into()
828
- }
829
-
830
- pub fn str_json_path_match(&self, pat: String) -> Self {
831
- let function = move |s: Series| {
832
- let ca = s.utf8()?;
833
- match ca.json_path_match(&pat) {
834
- Ok(ca) => Ok(Some(ca.into_series())),
835
- Err(e) => Err(PolarsError::ComputeError(format!("{:?}", e).into())),
836
- }
837
- };
838
- self.clone()
839
- .inner
840
- .map(function, GetOutput::from_type(DataType::Utf8))
841
- .with_fmt("str.json_path_match")
842
- .into()
843
- }
844
-
845
- pub fn str_extract(&self, pat: String, group_index: usize) -> Self {
846
- self.inner.clone().str().extract(&pat, group_index).into()
847
- }
848
-
849
- pub fn str_extract_all(&self, pat: &RbExpr) -> Self {
850
- self.inner
851
- .clone()
852
- .str()
853
- .extract_all(pat.inner.clone())
854
- .into()
855
- }
856
-
857
- pub fn count_match(&self, pat: String) -> Self {
858
- self.inner.clone().str().count_match(&pat).into()
859
- }
860
-
861
- pub fn strftime(&self, fmt: String) -> Self {
862
- self.inner.clone().dt().strftime(&fmt).into()
863
- }
864
-
865
- pub fn str_split(&self, by: String) -> Self {
866
- self.inner.clone().str().split(&by).into()
867
- }
868
-
869
- pub fn str_split_inclusive(&self, by: String) -> Self {
870
- self.inner.clone().str().split_inclusive(&by).into()
871
- }
872
-
873
- pub fn str_split_exact(&self, by: String, n: usize) -> Self {
874
- self.inner.clone().str().split_exact(&by, n).into()
875
- }
876
-
877
- pub fn str_split_exact_inclusive(&self, by: String, n: usize) -> Self {
878
- self.inner
879
- .clone()
880
- .str()
881
- .split_exact_inclusive(&by, n)
882
- .into()
883
- }
884
-
885
- pub fn str_splitn(&self, by: String, n: usize) -> Self {
886
- self.inner.clone().str().splitn(&by, n).into()
887
- }
888
-
889
- pub fn arr_lengths(&self) -> Self {
890
- self.inner.clone().arr().lengths().into()
891
- }
892
-
893
- pub fn arr_contains(&self, other: &RbExpr) -> Self {
894
- self.inner
895
- .clone()
896
- .arr()
897
- .contains(other.inner.clone())
898
- .into()
899
- }
900
-
901
- pub fn year(&self) -> Self {
902
- self.clone().inner.dt().year().into()
903
- }
904
-
905
- pub fn iso_year(&self) -> Self {
906
- self.clone().inner.dt().iso_year().into()
907
- }
908
-
909
- pub fn quarter(&self) -> Self {
910
- self.clone().inner.dt().quarter().into()
911
- }
912
-
913
- pub fn month(&self) -> Self {
914
- self.clone().inner.dt().month().into()
915
- }
916
-
917
- pub fn week(&self) -> Self {
918
- self.clone().inner.dt().week().into()
919
- }
920
-
921
- pub fn weekday(&self) -> Self {
922
- self.clone().inner.dt().weekday().into()
923
- }
924
-
925
- pub fn day(&self) -> Self {
926
- self.clone().inner.dt().day().into()
927
- }
928
-
929
- pub fn ordinal_day(&self) -> Self {
930
- self.clone().inner.dt().ordinal_day().into()
931
- }
932
-
933
- pub fn hour(&self) -> Self {
934
- self.clone().inner.dt().hour().into()
935
- }
936
-
937
- pub fn minute(&self) -> Self {
938
- self.clone().inner.dt().minute().into()
939
- }
940
-
941
- pub fn second(&self) -> Self {
942
- self.clone().inner.dt().second().into()
943
- }
944
-
945
- pub fn millisecond(&self) -> Self {
946
- self.clone().inner.dt().millisecond().into()
947
- }
948
-
949
- pub fn microsecond(&self) -> Self {
950
- self.clone().inner.dt().microsecond().into()
951
- }
952
-
953
- pub fn nanosecond(&self) -> Self {
954
- self.clone().inner.dt().nanosecond().into()
955
- }
956
-
957
- pub fn duration_days(&self) -> Self {
958
- self.inner
959
- .clone()
960
- .map(
961
- |s| Ok(Some(s.duration()?.days().into_series())),
962
- GetOutput::from_type(DataType::Int64),
963
- )
964
- .into()
965
- }
966
-
967
- pub fn duration_hours(&self) -> Self {
968
- self.inner
969
- .clone()
970
- .map(
971
- |s| Ok(Some(s.duration()?.hours().into_series())),
972
- GetOutput::from_type(DataType::Int64),
973
- )
974
- .into()
975
- }
976
-
977
- pub fn duration_minutes(&self) -> Self {
978
- self.inner
979
- .clone()
980
- .map(
981
- |s| Ok(Some(s.duration()?.minutes().into_series())),
982
- GetOutput::from_type(DataType::Int64),
983
- )
984
- .into()
985
- }
986
-
987
- pub fn duration_seconds(&self) -> Self {
988
- self.inner
989
- .clone()
990
- .map(
991
- |s| Ok(Some(s.duration()?.seconds().into_series())),
992
- GetOutput::from_type(DataType::Int64),
993
- )
994
- .into()
995
- }
996
-
997
- pub fn duration_nanoseconds(&self) -> Self {
998
- self.inner
999
- .clone()
1000
- .map(
1001
- |s| Ok(Some(s.duration()?.nanoseconds().into_series())),
1002
- GetOutput::from_type(DataType::Int64),
1003
- )
1004
- .into()
1005
- }
1006
-
1007
- pub fn duration_microseconds(&self) -> Self {
1008
- self.inner
1009
- .clone()
1010
- .map(
1011
- |s| Ok(Some(s.duration()?.microseconds().into_series())),
1012
- GetOutput::from_type(DataType::Int64),
1013
- )
1014
- .into()
1015
- }
1016
-
1017
- pub fn duration_milliseconds(&self) -> Self {
1018
- self.inner
1019
- .clone()
1020
- .map(
1021
- |s| Ok(Some(s.duration()?.milliseconds().into_series())),
1022
- GetOutput::from_type(DataType::Int64),
1023
- )
1024
- .into()
1025
- }
1026
-
1027
- pub fn timestamp(&self, tu: Wrap<TimeUnit>) -> Self {
1028
- self.inner.clone().dt().timestamp(tu.0).into()
1029
- }
1030
-
1031
- pub fn dt_offset_by(&self, by: String) -> Self {
1032
- let by = Duration::parse(&by);
1033
- self.inner.clone().dt().offset_by(by).into()
1034
- }
1035
-
1036
- pub fn dt_epoch_seconds(&self) -> Self {
1037
- self.clone()
1038
- .inner
1039
- .map(
1040
- |s| {
1041
- s.timestamp(TimeUnit::Milliseconds)
1042
- .map(|ca| Some((ca / 1000).into_series()))
1043
- },
1044
- GetOutput::from_type(DataType::Int64),
1045
- )
1046
- .into()
1047
- }
1048
-
1049
- pub fn dt_with_time_unit(&self, tu: Wrap<TimeUnit>) -> Self {
1050
- self.inner.clone().dt().with_time_unit(tu.0).into()
1051
- }
1052
-
1053
- pub fn dt_convert_time_zone(&self, tz: TimeZone) -> Self {
1054
- self.inner.clone().dt().convert_time_zone(tz).into()
1055
- }
1056
-
1057
- pub fn dt_cast_time_unit(&self, tu: Wrap<TimeUnit>) -> Self {
1058
- self.inner.clone().dt().cast_time_unit(tu.0).into()
1059
- }
1060
-
1061
- pub fn dt_replace_time_zone(&self, tz: Option<String>) -> Self {
1062
- self.inner.clone().dt().replace_time_zone(tz).into()
1063
- }
1064
-
1065
- #[allow(deprecated)]
1066
- pub fn dt_tz_localize(&self, tz: String) -> Self {
1067
- self.inner.clone().dt().tz_localize(tz).into()
1068
- }
1069
-
1070
- pub fn dt_truncate(&self, every: String, offset: String) -> Self {
1071
- self.inner.clone().dt().truncate(&every, &offset).into()
1072
- }
1073
-
1074
- pub fn dt_round(&self, every: String, offset: String) -> Self {
1075
- self.inner.clone().dt().round(&every, &offset).into()
1076
- }
1077
-
1078
- pub fn map(&self, lambda: Value, output_type: Option<Wrap<DataType>>, agg_list: bool) -> Self {
1079
- map_single(self, lambda, output_type, agg_list)
1080
- }
1081
-
1082
- pub fn dot(&self, other: &RbExpr) -> Self {
1083
- self.inner.clone().dot(other.inner.clone()).into()
1084
- }
1085
-
1086
- pub fn reinterpret(&self, signed: bool) -> Self {
1087
- let function = move |s: Series| reinterpret(&s, signed).map(Some);
1088
- let dt = if signed {
1089
- DataType::Int64
1090
- } else {
1091
- DataType::UInt64
1092
- };
1093
- self.clone()
1094
- .inner
1095
- .map(function, GetOutput::from_type(dt))
1096
- .into()
1097
- }
1098
-
1099
- pub fn mode(&self) -> Self {
1100
- self.inner.clone().mode().into()
1101
- }
1102
-
1103
- pub fn keep_name(&self) -> Self {
1104
- self.inner.clone().keep_name().into()
1105
- }
1106
-
1107
- pub fn prefix(&self, prefix: String) -> Self {
1108
- self.inner.clone().prefix(&prefix).into()
1109
- }
1110
-
1111
- pub fn suffix(&self, suffix: String) -> Self {
1112
- self.inner.clone().suffix(&suffix).into()
1113
- }
1114
-
1115
- pub fn map_alias(&self, lambda: Proc) -> Self {
1116
- self.inner
1117
- .clone()
1118
- .map_alias(move |name| {
1119
- let out = lambda.call::<_, String>((name,));
1120
- match out {
1121
- Ok(out) => Ok(out),
1122
- Err(e) => Err(PolarsError::ComputeError(
1123
- format!("Ruby function in 'map_alias' produced an error: {}.", e).into(),
1124
- )),
1125
- }
1126
- })
1127
- .into()
1128
- }
1129
-
1130
- pub fn exclude(&self, columns: Vec<String>) -> Self {
1131
- self.inner.clone().exclude(columns).into()
1132
- }
1133
-
1134
- pub fn interpolate(&self, method: Wrap<InterpolationMethod>) -> Self {
1135
- self.inner.clone().interpolate(method.0).into()
1136
- }
1137
-
1138
- pub fn rolling_sum(
1139
- &self,
1140
- window_size: String,
1141
- weights: Option<Vec<f64>>,
1142
- min_periods: usize,
1143
- center: bool,
1144
- by: Option<String>,
1145
- closed: Option<Wrap<ClosedWindow>>,
1146
- ) -> Self {
1147
- let options = RollingOptions {
1148
- window_size: Duration::parse(&window_size),
1149
- weights,
1150
- min_periods,
1151
- center,
1152
- by,
1153
- closed_window: closed.map(|c| c.0),
1154
- };
1155
- self.inner.clone().rolling_sum(options).into()
1156
- }
1157
-
1158
- pub fn rolling_min(
1159
- &self,
1160
- window_size: String,
1161
- weights: Option<Vec<f64>>,
1162
- min_periods: usize,
1163
- center: bool,
1164
- by: Option<String>,
1165
- closed: Option<Wrap<ClosedWindow>>,
1166
- ) -> Self {
1167
- let options = RollingOptions {
1168
- window_size: Duration::parse(&window_size),
1169
- weights,
1170
- min_periods,
1171
- center,
1172
- by,
1173
- closed_window: closed.map(|c| c.0),
1174
- };
1175
- self.inner.clone().rolling_min(options).into()
1176
- }
1177
-
1178
- pub fn rolling_max(
1179
- &self,
1180
- window_size: String,
1181
- weights: Option<Vec<f64>>,
1182
- min_periods: usize,
1183
- center: bool,
1184
- by: Option<String>,
1185
- closed: Option<Wrap<ClosedWindow>>,
1186
- ) -> Self {
1187
- let options = RollingOptions {
1188
- window_size: Duration::parse(&window_size),
1189
- weights,
1190
- min_periods,
1191
- center,
1192
- by,
1193
- closed_window: closed.map(|c| c.0),
1194
- };
1195
- self.inner.clone().rolling_max(options).into()
1196
- }
1197
-
1198
- pub fn rolling_mean(
1199
- &self,
1200
- window_size: String,
1201
- weights: Option<Vec<f64>>,
1202
- min_periods: usize,
1203
- center: bool,
1204
- by: Option<String>,
1205
- closed: Option<Wrap<ClosedWindow>>,
1206
- ) -> Self {
1207
- let options = RollingOptions {
1208
- window_size: Duration::parse(&window_size),
1209
- weights,
1210
- min_periods,
1211
- center,
1212
- by,
1213
- closed_window: closed.map(|c| c.0),
1214
- };
1215
-
1216
- self.inner.clone().rolling_mean(options).into()
1217
- }
1218
-
1219
- pub fn rolling_std(
1220
- &self,
1221
- window_size: String,
1222
- weights: Option<Vec<f64>>,
1223
- min_periods: usize,
1224
- center: bool,
1225
- by: Option<String>,
1226
- closed: Option<Wrap<ClosedWindow>>,
1227
- ) -> Self {
1228
- let options = RollingOptions {
1229
- window_size: Duration::parse(&window_size),
1230
- weights,
1231
- min_periods,
1232
- center,
1233
- by,
1234
- closed_window: closed.map(|c| c.0),
1235
- };
1236
-
1237
- self.inner.clone().rolling_std(options).into()
1238
- }
1239
-
1240
- pub fn rolling_var(
1241
- &self,
1242
- window_size: String,
1243
- weights: Option<Vec<f64>>,
1244
- min_periods: usize,
1245
- center: bool,
1246
- by: Option<String>,
1247
- closed: Option<Wrap<ClosedWindow>>,
1248
- ) -> Self {
1249
- let options = RollingOptions {
1250
- window_size: Duration::parse(&window_size),
1251
- weights,
1252
- min_periods,
1253
- center,
1254
- by,
1255
- closed_window: closed.map(|c| c.0),
1256
- };
1257
-
1258
- self.inner.clone().rolling_var(options).into()
1259
- }
1260
-
1261
- pub fn rolling_median(
1262
- &self,
1263
- window_size: String,
1264
- weights: Option<Vec<f64>>,
1265
- min_periods: usize,
1266
- center: bool,
1267
- by: Option<String>,
1268
- closed: Option<Wrap<ClosedWindow>>,
1269
- ) -> Self {
1270
- let options = RollingOptions {
1271
- window_size: Duration::parse(&window_size),
1272
- weights,
1273
- min_periods,
1274
- center,
1275
- by,
1276
- closed_window: closed.map(|c| c.0),
1277
- };
1278
- self.inner.clone().rolling_median(options).into()
1279
- }
1280
-
1281
- #[allow(clippy::too_many_arguments)]
1282
- pub fn rolling_quantile(
1283
- &self,
1284
- quantile: f64,
1285
- interpolation: Wrap<QuantileInterpolOptions>,
1286
- window_size: String,
1287
- weights: Option<Vec<f64>>,
1288
- min_periods: usize,
1289
- center: bool,
1290
- by: Option<String>,
1291
- closed: Option<Wrap<ClosedWindow>>,
1292
- ) -> Self {
1293
- let options = RollingOptions {
1294
- window_size: Duration::parse(&window_size),
1295
- weights,
1296
- min_periods,
1297
- center,
1298
- by,
1299
- closed_window: closed.map(|c| c.0),
1300
- };
1301
-
1302
- self.inner
1303
- .clone()
1304
- .rolling_quantile(quantile, interpolation.0, options)
1305
- .into()
1306
- }
1307
-
1308
- pub fn rolling_skew(&self, window_size: usize, bias: bool) -> Self {
1309
- self.inner
1310
- .clone()
1311
- .rolling_apply_float(window_size, move |ca| {
1312
- ca.clone().into_series().skew(bias).unwrap()
1313
- })
1314
- .into()
1315
- }
1316
-
1317
- pub fn lower_bound(&self) -> Self {
1318
- self.inner.clone().lower_bound().into()
1319
- }
1320
-
1321
- pub fn upper_bound(&self) -> Self {
1322
- self.inner.clone().upper_bound().into()
1323
- }
1324
-
1325
- pub fn lst_max(&self) -> Self {
1326
- self.inner.clone().arr().max().into()
1327
- }
1328
-
1329
- pub fn lst_min(&self) -> Self {
1330
- self.inner.clone().arr().min().into()
1331
- }
1332
-
1333
- pub fn lst_sum(&self) -> Self {
1334
- self.inner.clone().arr().sum().with_fmt("arr.sum").into()
1335
- }
1336
-
1337
- pub fn lst_mean(&self) -> Self {
1338
- self.inner.clone().arr().mean().with_fmt("arr.mean").into()
1339
- }
1340
-
1341
- pub fn lst_sort(&self, reverse: bool) -> Self {
1342
- self.inner
1343
- .clone()
1344
- .arr()
1345
- .sort(SortOptions {
1346
- descending: reverse,
1347
- ..Default::default()
1348
- })
1349
- .with_fmt("arr.sort")
1350
- .into()
1351
- }
1352
-
1353
- pub fn lst_reverse(&self) -> Self {
1354
- self.inner
1355
- .clone()
1356
- .arr()
1357
- .reverse()
1358
- .with_fmt("arr.reverse")
1359
- .into()
1360
- }
1361
-
1362
- pub fn lst_unique(&self) -> Self {
1363
- self.inner
1364
- .clone()
1365
- .arr()
1366
- .unique()
1367
- .with_fmt("arr.unique")
1368
- .into()
1369
- }
1370
-
1371
- pub fn lst_get(&self, index: &RbExpr) -> Self {
1372
- self.inner.clone().arr().get(index.inner.clone()).into()
1373
- }
1374
-
1375
- pub fn lst_join(&self, separator: String) -> Self {
1376
- self.inner.clone().arr().join(&separator).into()
1377
- }
1378
-
1379
- pub fn lst_arg_min(&self) -> Self {
1380
- self.inner.clone().arr().arg_min().into()
1381
- }
1382
-
1383
- pub fn lst_arg_max(&self) -> Self {
1384
- self.inner.clone().arr().arg_max().into()
1385
- }
1386
-
1387
- pub fn lst_diff(&self, n: usize, null_behavior: Wrap<NullBehavior>) -> RbResult<Self> {
1388
- Ok(self.inner.clone().arr().diff(n, null_behavior.0).into())
1389
- }
1390
-
1391
- pub fn lst_shift(&self, periods: i64) -> Self {
1392
- self.inner.clone().arr().shift(periods).into()
1393
- }
1394
-
1395
- pub fn lst_slice(&self, offset: &RbExpr, length: Option<&RbExpr>) -> Self {
1396
- let length = match length {
1397
- Some(i) => i.inner.clone(),
1398
- None => dsl::lit(i64::MAX),
1399
- };
1400
- self.inner
1401
- .clone()
1402
- .arr()
1403
- .slice(offset.inner.clone(), length)
1404
- .into()
1405
- }
1406
-
1407
- pub fn lst_eval(&self, expr: &RbExpr, parallel: bool) -> Self {
1408
- self.inner
1409
- .clone()
1410
- .arr()
1411
- .eval(expr.inner.clone(), parallel)
1412
- .into()
1413
- }
1414
-
1415
- pub fn cumulative_eval(&self, expr: &RbExpr, min_periods: usize, parallel: bool) -> Self {
1416
- self.inner
1417
- .clone()
1418
- .cumulative_eval(expr.inner.clone(), min_periods, parallel)
1419
- .into()
1420
- }
1421
-
1422
- pub fn lst_to_struct(
1423
- &self,
1424
- width_strat: Wrap<ListToStructWidthStrategy>,
1425
- _name_gen: Option<Value>,
1426
- upper_bound: usize,
1427
- ) -> RbResult<Self> {
1428
- // TODO fix
1429
- let name_gen = None;
1430
- // let name_gen = name_gen.map(|lambda| {
1431
- // Arc::new(move |idx: usize| {
1432
- // let out: Value = lambda.funcall("call", (idx,)).unwrap();
1433
- // out.try_convert::<String>().unwrap()
1434
- // }) as NameGenerator
1435
- // });
1436
-
1437
- Ok(self
1438
- .inner
1439
- .clone()
1440
- .arr()
1441
- .to_struct(width_strat.0, name_gen, upper_bound)
1442
- .into())
1443
- }
1444
-
1445
- pub fn rank(&self, method: Wrap<RankMethod>, reverse: bool) -> Self {
1446
- let options = RankOptions {
1447
- method: method.0,
1448
- descending: reverse,
1449
- };
1450
- self.inner.clone().rank(options).into()
1451
- }
1452
-
1453
- pub fn diff(&self, n: usize, null_behavior: Wrap<NullBehavior>) -> Self {
1454
- self.inner.clone().diff(n, null_behavior.0).into()
1455
- }
1456
-
1457
- pub fn pct_change(&self, n: usize) -> Self {
1458
- self.inner.clone().pct_change(n).into()
1459
- }
1460
-
1461
- pub fn skew(&self, bias: bool) -> Self {
1462
- self.inner.clone().skew(bias).into()
1463
- }
1464
-
1465
- pub fn kurtosis(&self, fisher: bool, bias: bool) -> Self {
1466
- self.inner.clone().kurtosis(fisher, bias).into()
1467
- }
1468
-
1469
- pub fn str_concat(&self, delimiter: String) -> Self {
1470
- self.inner.clone().str().concat(&delimiter).into()
1471
- }
1472
-
1473
- pub fn cat_set_ordering(&self, ordering: Wrap<CategoricalOrdering>) -> Self {
1474
- self.inner.clone().cat().set_ordering(ordering.0).into()
1475
- }
1476
-
1477
- pub fn reshape(&self, dims: Vec<i64>) -> Self {
1478
- self.inner.clone().reshape(&dims).into()
1479
- }
1480
-
1481
- pub fn cumcount(&self, reverse: bool) -> Self {
1482
- self.inner.clone().cumcount(reverse).into()
1483
- }
1484
-
1485
- pub fn to_physical(&self) -> Self {
1486
- self.inner
1487
- .clone()
1488
- .map(
1489
- |s| Ok(Some(s.to_physical_repr().into_owned())),
1490
- GetOutput::map_dtype(|dt| dt.to_physical()),
1491
- )
1492
- .with_fmt("to_physical")
1493
- .into()
1494
- }
1495
-
1496
- pub fn shuffle(&self, seed: Option<u64>) -> Self {
1497
- self.inner.clone().shuffle(seed).into()
1498
- }
1499
-
1500
- pub fn sample_n(
1501
- &self,
1502
- n: usize,
1503
- with_replacement: bool,
1504
- shuffle: bool,
1505
- seed: Option<u64>,
1506
- ) -> Self {
1507
- self.inner
1508
- .clone()
1509
- .sample_n(n, with_replacement, shuffle, seed)
1510
- .into()
1511
- }
1512
-
1513
- pub fn sample_frac(
1514
- &self,
1515
- frac: f64,
1516
- with_replacement: bool,
1517
- shuffle: bool,
1518
- seed: Option<u64>,
1519
- ) -> Self {
1520
- self.inner
1521
- .clone()
1522
- .sample_frac(frac, with_replacement, shuffle, seed)
1523
- .into()
1524
- }
1525
-
1526
- pub fn ewm_mean(
1527
- &self,
1528
- alpha: f64,
1529
- adjust: bool,
1530
- min_periods: usize,
1531
- ignore_nulls: bool,
1532
- ) -> Self {
1533
- let options = EWMOptions {
1534
- alpha,
1535
- adjust,
1536
- bias: false,
1537
- min_periods,
1538
- ignore_nulls,
1539
- };
1540
- self.inner.clone().ewm_mean(options).into()
1541
- }
1542
-
1543
- pub fn ewm_std(
1544
- &self,
1545
- alpha: f64,
1546
- adjust: bool,
1547
- bias: bool,
1548
- min_periods: usize,
1549
- ignore_nulls: bool,
1550
- ) -> Self {
1551
- let options = EWMOptions {
1552
- alpha,
1553
- adjust,
1554
- bias,
1555
- min_periods,
1556
- ignore_nulls,
1557
- };
1558
- self.inner.clone().ewm_std(options).into()
1559
- }
1560
-
1561
- pub fn ewm_var(
1562
- &self,
1563
- alpha: f64,
1564
- adjust: bool,
1565
- bias: bool,
1566
- min_periods: usize,
1567
- ignore_nulls: bool,
1568
- ) -> Self {
1569
- let options = EWMOptions {
1570
- alpha,
1571
- adjust,
1572
- bias,
1573
- min_periods,
1574
- ignore_nulls,
1575
- };
1576
- self.inner.clone().ewm_var(options).into()
1577
- }
1578
-
1579
- pub fn extend_constant(&self, value: Wrap<AnyValue>, n: usize) -> Self {
1580
- let value = value.into_value();
1581
- self.inner
1582
- .clone()
1583
- .apply(
1584
- move |s| {
1585
- let value = value.try_convert::<Wrap<AnyValue>>().unwrap().0;
1586
- s.extend_constant(value, n).map(Some)
1587
- },
1588
- GetOutput::same_type(),
1589
- )
1590
- .with_fmt("extend")
1591
- .into()
1592
- }
1593
-
1594
- pub fn any(&self) -> Self {
1595
- self.inner.clone().any().into()
1596
- }
1597
-
1598
- pub fn all(&self) -> Self {
1599
- self.inner.clone().all().into()
1600
- }
1601
-
1602
- pub fn struct_field_by_name(&self, name: String) -> Self {
1603
- self.inner.clone().struct_().field_by_name(&name).into()
1604
- }
1605
-
1606
- pub fn struct_field_by_index(&self, index: i64) -> Self {
1607
- self.inner.clone().struct_().field_by_index(index).into()
1608
- }
1609
-
1610
- pub fn struct_rename_fields(&self, names: Vec<String>) -> Self {
1611
- self.inner.clone().struct_().rename_fields(names).into()
1612
- }
1613
-
1614
- pub fn log(&self, base: f64) -> Self {
1615
- self.inner.clone().log(base).into()
1616
- }
1617
-
1618
- pub fn exp(&self) -> Self {
1619
- self.inner.clone().exp().into()
1620
- }
1621
-
1622
- pub fn entropy(&self, base: f64, normalize: bool) -> Self {
1623
- self.inner.clone().entropy(base, normalize).into()
1624
- }
1625
-
1626
- pub fn hash(&self, seed: u64, seed_1: u64, seed_2: u64, seed_3: u64) -> Self {
1627
- self.inner.clone().hash(seed, seed_1, seed_2, seed_3).into()
1628
- }
1629
- }
1630
-
1631
- pub fn col(name: String) -> RbExpr {
1632
- dsl::col(&name).into()
1633
- }
1634
-
1635
- pub fn count() -> RbExpr {
1636
- dsl::count().into()
1637
- }
1638
-
1639
- pub fn first() -> RbExpr {
1640
- dsl::first().into()
1641
- }
1642
-
1643
- pub fn last() -> RbExpr {
1644
- dsl::last().into()
1645
- }
1646
-
1647
- pub fn cols(names: Vec<String>) -> RbExpr {
1648
- dsl::cols(names).into()
1649
- }
1650
-
1651
- pub fn dtype_cols(dtypes: Vec<DataType>) -> RbExpr {
1652
- dsl::dtype_cols(dtypes).into()
1653
- }
1654
-
1655
- pub fn fold(acc: &RbExpr, lambda: Value, exprs: RArray) -> RbResult<RbExpr> {
1656
- let exprs = rb_exprs_to_exprs(exprs)?;
1657
-
1658
- let func = move |a: Series, b: Series| binary_lambda(lambda, a, b);
1659
- Ok(polars::lazy::dsl::fold_exprs(acc.inner.clone(), func, exprs).into())
1660
- }
1661
-
1662
- pub fn cumfold(acc: &RbExpr, lambda: Value, exprs: RArray, include_init: bool) -> RbResult<RbExpr> {
1663
- let exprs = rb_exprs_to_exprs(exprs)?;
1664
-
1665
- let func = move |a: Series, b: Series| binary_lambda(lambda, a, b);
1666
- Ok(polars::lazy::dsl::cumfold_exprs(acc.inner.clone(), func, exprs, include_init).into())
1667
- }
1668
-
1669
- // TODO improve
1670
- pub fn lit(value: Value) -> RbResult<RbExpr> {
1671
- if value.is_nil() {
1672
- Ok(dsl::lit(Null {}).into())
1673
- } else if let Ok(series) = value.try_convert::<&RbSeries>() {
1674
- Ok(dsl::lit(series.series.borrow().clone()).into())
1675
- } else if let Some(v) = RString::from_value(value) {
1676
- Ok(dsl::lit(v.try_convert::<String>()?).into())
1677
- } else if value.is_kind_of(class::integer()) {
1678
- match value.try_convert::<i64>() {
1679
- Ok(val) => {
1680
- if val > 0 && val < i32::MAX as i64 || val < 0 && val > i32::MIN as i64 {
1681
- Ok(dsl::lit(val as i32).into())
1682
- } else {
1683
- Ok(dsl::lit(val).into())
1684
- }
1685
- }
1686
- _ => {
1687
- let val = value.try_convert::<u64>()?;
1688
- Ok(dsl::lit(val).into())
1689
- }
1690
- }
1691
- } else {
1692
- Ok(dsl::lit(value.try_convert::<f64>()?).into())
1693
- }
1694
- }
1695
-
1696
- pub fn arange(low: &RbExpr, high: &RbExpr, step: usize) -> RbExpr {
1697
- polars::lazy::dsl::arange(low.inner.clone(), high.inner.clone(), step).into()
1698
- }
1699
-
1700
- pub fn repeat(value: Value, n_times: &RbExpr) -> RbResult<RbExpr> {
1701
- if value.is_nil() {
1702
- Ok(polars::lazy::dsl::repeat(Null {}, n_times.inner.clone()).into())
1703
- } else {
1704
- todo!();
1705
- }
1706
- }
1707
-
1708
- pub fn pearson_corr(a: &RbExpr, b: &RbExpr, ddof: u8) -> RbExpr {
1709
- polars::lazy::dsl::pearson_corr(a.inner.clone(), b.inner.clone(), ddof).into()
1710
- }
1711
-
1712
- pub fn spearman_rank_corr(a: &RbExpr, b: &RbExpr, ddof: u8, propagate_nans: bool) -> RbExpr {
1713
- polars::lazy::dsl::spearman_rank_corr(a.inner.clone(), b.inner.clone(), ddof, propagate_nans)
1714
- .into()
1715
- }
1716
-
1717
- pub fn cov(a: &RbExpr, b: &RbExpr) -> RbExpr {
1718
- polars::lazy::dsl::cov(a.inner.clone(), b.inner.clone()).into()
1719
- }
1720
-
1721
- pub fn arg_sort_by(by: RArray, reverse: Vec<bool>) -> RbResult<RbExpr> {
1722
- let by = rb_exprs_to_exprs(by)?;
1723
- Ok(polars::lazy::dsl::arg_sort_by(by, &reverse).into())
1724
- }
1725
-
1726
- #[magnus::wrap(class = "Polars::RbWhen")]
1727
- #[derive(Clone)]
1728
- pub struct RbWhen {
1729
- pub inner: dsl::When,
1730
- }
1731
-
1732
- impl From<dsl::When> for RbWhen {
1733
- fn from(inner: dsl::When) -> Self {
1734
- RbWhen { inner }
1735
- }
1736
- }
1737
-
1738
- #[magnus::wrap(class = "Polars::RbWhenThen")]
1739
- #[derive(Clone)]
1740
- pub struct RbWhenThen {
1741
- pub inner: dsl::WhenThen,
1742
- }
1743
-
1744
- impl From<dsl::WhenThen> for RbWhenThen {
1745
- fn from(inner: dsl::WhenThen) -> Self {
1746
- RbWhenThen { inner }
1747
- }
1748
- }
1749
-
1750
- impl RbWhen {
1751
- pub fn then(&self, expr: &RbExpr) -> RbWhenThen {
1752
- self.inner.clone().then(expr.inner.clone()).into()
1753
- }
1754
- }
1755
-
1756
- impl RbWhenThen {
1757
- pub fn overwise(&self, expr: &RbExpr) -> RbExpr {
1758
- self.inner.clone().otherwise(expr.inner.clone()).into()
1759
- }
1760
- }
1761
-
1762
- pub fn when(predicate: &RbExpr) -> RbWhen {
1763
- dsl::when(predicate.inner.clone()).into()
1764
- }
1765
-
1766
- pub fn concat_str(s: RArray, sep: String) -> RbResult<RbExpr> {
1767
- let s = rb_exprs_to_exprs(s)?;
1768
- Ok(dsl::concat_str(s, &sep).into())
1769
- }
1770
-
1771
- pub fn concat_lst(s: RArray) -> RbResult<RbExpr> {
1772
- let s = rb_exprs_to_exprs(s)?;
1773
- let expr = dsl::concat_lst(s).map_err(RbPolarsErr::from)?;
1774
- Ok(expr.into())
1775
- }