polars-df 0.10.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -0
  3. data/Cargo.lock +392 -351
  4. data/README.md +6 -6
  5. data/ext/polars/Cargo.toml +12 -7
  6. data/ext/polars/src/batched_csv.rs +53 -52
  7. data/ext/polars/src/conversion/any_value.rs +261 -0
  8. data/ext/polars/src/conversion/chunked_array.rs +4 -4
  9. data/ext/polars/src/conversion/mod.rs +60 -66
  10. data/ext/polars/src/dataframe/construction.rs +184 -0
  11. data/ext/polars/src/dataframe/export.rs +48 -0
  12. data/ext/polars/src/dataframe/general.rs +597 -0
  13. data/ext/polars/src/dataframe/io.rs +473 -0
  14. data/ext/polars/src/dataframe/mod.rs +26 -0
  15. data/ext/polars/src/error.rs +26 -4
  16. data/ext/polars/src/expr/categorical.rs +0 -10
  17. data/ext/polars/src/expr/datetime.rs +4 -8
  18. data/ext/polars/src/expr/general.rs +129 -94
  19. data/ext/polars/src/expr/mod.rs +2 -2
  20. data/ext/polars/src/expr/rolling.rs +201 -77
  21. data/ext/polars/src/expr/string.rs +11 -36
  22. data/ext/polars/src/functions/eager.rs +10 -10
  23. data/ext/polars/src/functions/lazy.rs +23 -21
  24. data/ext/polars/src/functions/range.rs +69 -1
  25. data/ext/polars/src/interop/mod.rs +1 -0
  26. data/ext/polars/src/interop/numo/mod.rs +2 -0
  27. data/ext/polars/src/interop/numo/to_numo_df.rs +23 -0
  28. data/ext/polars/src/interop/numo/to_numo_series.rs +61 -0
  29. data/ext/polars/src/lazyframe/mod.rs +135 -136
  30. data/ext/polars/src/lib.rs +94 -59
  31. data/ext/polars/src/map/dataframe.rs +2 -2
  32. data/ext/polars/src/map/lazy.rs +5 -25
  33. data/ext/polars/src/map/series.rs +7 -1
  34. data/ext/polars/src/rb_modules.rs +25 -1
  35. data/ext/polars/src/series/aggregation.rs +49 -30
  36. data/ext/polars/src/series/arithmetic.rs +21 -11
  37. data/ext/polars/src/series/construction.rs +56 -38
  38. data/ext/polars/src/series/export.rs +131 -49
  39. data/ext/polars/src/series/mod.rs +32 -141
  40. data/ext/polars/src/sql.rs +3 -1
  41. data/lib/polars/array_expr.rb +4 -4
  42. data/lib/polars/batched_csv_reader.rb +11 -5
  43. data/lib/polars/cat_expr.rb +0 -36
  44. data/lib/polars/cat_name_space.rb +0 -37
  45. data/lib/polars/convert.rb +6 -1
  46. data/lib/polars/data_frame.rb +176 -403
  47. data/lib/polars/data_types.rb +1 -1
  48. data/lib/polars/date_time_expr.rb +525 -572
  49. data/lib/polars/date_time_name_space.rb +263 -460
  50. data/lib/polars/dynamic_group_by.rb +5 -5
  51. data/lib/polars/exceptions.rb +7 -0
  52. data/lib/polars/expr.rb +1394 -243
  53. data/lib/polars/expr_dispatch.rb +1 -1
  54. data/lib/polars/functions/aggregation/horizontal.rb +8 -8
  55. data/lib/polars/functions/as_datatype.rb +63 -40
  56. data/lib/polars/functions/lazy.rb +63 -14
  57. data/lib/polars/functions/lit.rb +1 -1
  58. data/lib/polars/functions/range/date_range.rb +90 -57
  59. data/lib/polars/functions/range/datetime_range.rb +149 -0
  60. data/lib/polars/functions/range/int_range.rb +2 -2
  61. data/lib/polars/functions/range/time_range.rb +141 -0
  62. data/lib/polars/functions/repeat.rb +1 -1
  63. data/lib/polars/functions/whenthen.rb +1 -1
  64. data/lib/polars/group_by.rb +88 -23
  65. data/lib/polars/io/avro.rb +24 -0
  66. data/lib/polars/{io.rb → io/csv.rb} +299 -493
  67. data/lib/polars/io/database.rb +73 -0
  68. data/lib/polars/io/ipc.rb +247 -0
  69. data/lib/polars/io/json.rb +29 -0
  70. data/lib/polars/io/ndjson.rb +80 -0
  71. data/lib/polars/io/parquet.rb +227 -0
  72. data/lib/polars/lazy_frame.rb +143 -272
  73. data/lib/polars/lazy_group_by.rb +100 -3
  74. data/lib/polars/list_expr.rb +11 -11
  75. data/lib/polars/list_name_space.rb +5 -1
  76. data/lib/polars/rolling_group_by.rb +7 -9
  77. data/lib/polars/series.rb +103 -187
  78. data/lib/polars/string_expr.rb +78 -102
  79. data/lib/polars/string_name_space.rb +5 -4
  80. data/lib/polars/testing.rb +2 -2
  81. data/lib/polars/utils/constants.rb +9 -0
  82. data/lib/polars/utils/convert.rb +97 -0
  83. data/lib/polars/utils/parse.rb +89 -0
  84. data/lib/polars/utils/various.rb +76 -0
  85. data/lib/polars/utils/wrap.rb +19 -0
  86. data/lib/polars/utils.rb +8 -300
  87. data/lib/polars/version.rb +1 -1
  88. data/lib/polars/whenthen.rb +6 -6
  89. data/lib/polars.rb +20 -1
  90. metadata +28 -7
  91. data/ext/polars/src/conversion/anyvalue.rs +0 -186
  92. data/ext/polars/src/dataframe.rs +0 -1208
@@ -0,0 +1,597 @@
1
+ use either::Either;
2
+ use magnus::{prelude::*, typed_data::Obj, IntoValue, RArray, Value};
3
+ use polars::frame::NullStrategy;
4
+ use polars::prelude::pivot::{pivot, pivot_stable};
5
+ use polars::prelude::*;
6
+
7
+ use crate::conversion::*;
8
+ use crate::map::dataframe::{
9
+ apply_lambda_unknown, apply_lambda_with_bool_out_type, apply_lambda_with_primitive_out_type,
10
+ apply_lambda_with_utf8_out_type,
11
+ };
12
+ use crate::series::{to_rbseries_collection, to_series_collection};
13
+ use crate::{RbDataFrame, RbExpr, RbLazyFrame, RbPolarsErr, RbResult, RbSeries};
14
+
15
+ impl RbDataFrame {
16
+ pub fn init(columns: RArray) -> RbResult<Self> {
17
+ let mut cols = Vec::new();
18
+ for i in columns.into_iter() {
19
+ cols.push(<&RbSeries>::try_convert(i)?.series.borrow().clone());
20
+ }
21
+ let df = DataFrame::new(cols).map_err(RbPolarsErr::from)?;
22
+ Ok(RbDataFrame::new(df))
23
+ }
24
+
25
+ pub fn estimated_size(&self) -> usize {
26
+ self.df.borrow().estimated_size()
27
+ }
28
+
29
+ pub fn dtype_strings(&self) -> Vec<String> {
30
+ self.df
31
+ .borrow()
32
+ .get_columns()
33
+ .iter()
34
+ .map(|s| format!("{}", s.dtype()))
35
+ .collect()
36
+ }
37
+
38
+ pub fn add(&self, s: &RbSeries) -> RbResult<Self> {
39
+ let df = (&*self.df.borrow() + &*s.series.borrow()).map_err(RbPolarsErr::from)?;
40
+ Ok(df.into())
41
+ }
42
+
43
+ pub fn sub(&self, s: &RbSeries) -> RbResult<Self> {
44
+ let df = (&*self.df.borrow() - &*s.series.borrow()).map_err(RbPolarsErr::from)?;
45
+ Ok(df.into())
46
+ }
47
+
48
+ pub fn div(&self, s: &RbSeries) -> RbResult<Self> {
49
+ let df = (&*self.df.borrow() / &*s.series.borrow()).map_err(RbPolarsErr::from)?;
50
+ Ok(df.into())
51
+ }
52
+
53
+ pub fn mul(&self, s: &RbSeries) -> RbResult<Self> {
54
+ let df = (&*self.df.borrow() * &*s.series.borrow()).map_err(RbPolarsErr::from)?;
55
+ Ok(df.into())
56
+ }
57
+
58
+ pub fn rem(&self, s: &RbSeries) -> RbResult<Self> {
59
+ let df = (&*self.df.borrow() % &*s.series.borrow()).map_err(RbPolarsErr::from)?;
60
+ Ok(df.into())
61
+ }
62
+
63
+ pub fn add_df(&self, s: &Self) -> RbResult<Self> {
64
+ let df = (&*self.df.borrow() + &*s.df.borrow()).map_err(RbPolarsErr::from)?;
65
+ Ok(df.into())
66
+ }
67
+
68
+ pub fn sub_df(&self, s: &Self) -> RbResult<Self> {
69
+ let df = (&*self.df.borrow() - &*s.df.borrow()).map_err(RbPolarsErr::from)?;
70
+ Ok(df.into())
71
+ }
72
+
73
+ pub fn div_df(&self, s: &Self) -> RbResult<Self> {
74
+ let df = (&*self.df.borrow() / &*s.df.borrow()).map_err(RbPolarsErr::from)?;
75
+ Ok(df.into())
76
+ }
77
+
78
+ pub fn mul_df(&self, s: &Self) -> RbResult<Self> {
79
+ let df = (&*self.df.borrow() * &*s.df.borrow()).map_err(RbPolarsErr::from)?;
80
+ Ok(df.into())
81
+ }
82
+
83
+ pub fn rem_df(&self, s: &Self) -> RbResult<Self> {
84
+ let df = (&*self.df.borrow() % &*s.df.borrow()).map_err(RbPolarsErr::from)?;
85
+ Ok(df.into())
86
+ }
87
+
88
+ pub fn sample_n(
89
+ &self,
90
+ n: &RbSeries,
91
+ with_replacement: bool,
92
+ shuffle: bool,
93
+ seed: Option<u64>,
94
+ ) -> RbResult<Self> {
95
+ let df = self
96
+ .df
97
+ .borrow()
98
+ .sample_n(&n.series.borrow(), with_replacement, shuffle, seed)
99
+ .map_err(RbPolarsErr::from)?;
100
+ Ok(df.into())
101
+ }
102
+
103
+ pub fn sample_frac(
104
+ &self,
105
+ frac: &RbSeries,
106
+ with_replacement: bool,
107
+ shuffle: bool,
108
+ seed: Option<u64>,
109
+ ) -> RbResult<Self> {
110
+ let df = self
111
+ .df
112
+ .borrow()
113
+ .sample_frac(&frac.series.borrow(), with_replacement, shuffle, seed)
114
+ .map_err(RbPolarsErr::from)?;
115
+ Ok(df.into())
116
+ }
117
+
118
+ pub fn rechunk(&self) -> Self {
119
+ let mut df = self.df.borrow_mut().clone();
120
+ df.as_single_chunk_par();
121
+ df.into()
122
+ }
123
+
124
+ pub fn as_str(&self) -> String {
125
+ format!("{}", self.df.borrow())
126
+ }
127
+
128
+ pub fn get_columns(&self) -> RArray {
129
+ let cols = self.df.borrow().get_columns().to_vec();
130
+ to_rbseries_collection(cols)
131
+ }
132
+
133
+ pub fn columns(&self) -> Vec<String> {
134
+ self.df
135
+ .borrow()
136
+ .get_column_names()
137
+ .iter()
138
+ .map(|v| v.to_string())
139
+ .collect()
140
+ }
141
+
142
+ pub fn set_column_names(&self, names: Vec<String>) -> RbResult<()> {
143
+ self.df
144
+ .borrow_mut()
145
+ .set_column_names(&names)
146
+ .map_err(RbPolarsErr::from)?;
147
+ Ok(())
148
+ }
149
+
150
+ pub fn dtypes(&self) -> RArray {
151
+ RArray::from_iter(
152
+ self.df
153
+ .borrow()
154
+ .iter()
155
+ .map(|s| Wrap(s.dtype().clone()).into_value()),
156
+ )
157
+ }
158
+
159
+ pub fn n_chunks(&self) -> usize {
160
+ self.df.borrow().n_chunks()
161
+ }
162
+
163
+ pub fn shape(&self) -> (usize, usize) {
164
+ self.df.borrow().shape()
165
+ }
166
+
167
+ pub fn height(&self) -> usize {
168
+ self.df.borrow().height()
169
+ }
170
+
171
+ pub fn width(&self) -> usize {
172
+ self.df.borrow().width()
173
+ }
174
+
175
+ pub fn hstack(&self, columns: RArray) -> RbResult<Self> {
176
+ let columns = to_series_collection(columns)?;
177
+ let df = self
178
+ .df
179
+ .borrow()
180
+ .hstack(&columns)
181
+ .map_err(RbPolarsErr::from)?;
182
+ Ok(df.into())
183
+ }
184
+
185
+ pub fn hstack_mut(&self, columns: RArray) -> RbResult<()> {
186
+ let columns = to_series_collection(columns)?;
187
+ self.df
188
+ .borrow_mut()
189
+ .hstack_mut(&columns)
190
+ .map_err(RbPolarsErr::from)?;
191
+ Ok(())
192
+ }
193
+
194
+ pub fn vstack(&self, df: &RbDataFrame) -> RbResult<Self> {
195
+ let df = self
196
+ .df
197
+ .borrow()
198
+ .vstack(&df.df.borrow())
199
+ .map_err(RbPolarsErr::from)?;
200
+ Ok(df.into())
201
+ }
202
+
203
+ pub fn vstack_mut(&self, df: &RbDataFrame) -> RbResult<()> {
204
+ self.df
205
+ .borrow_mut()
206
+ .vstack_mut(&df.df.borrow())
207
+ .map_err(RbPolarsErr::from)?;
208
+ Ok(())
209
+ }
210
+
211
+ pub fn extend(&self, df: &RbDataFrame) -> RbResult<()> {
212
+ self.df
213
+ .borrow_mut()
214
+ .extend(&df.df.borrow())
215
+ .map_err(RbPolarsErr::from)?;
216
+ Ok(())
217
+ }
218
+
219
+ pub fn drop_in_place(&self, name: String) -> RbResult<RbSeries> {
220
+ let s = self
221
+ .df
222
+ .borrow_mut()
223
+ .drop_in_place(&name)
224
+ .map_err(RbPolarsErr::from)?;
225
+ Ok(RbSeries::new(s))
226
+ }
227
+
228
+ pub fn select_at_idx(&self, idx: usize) -> Option<RbSeries> {
229
+ self.df
230
+ .borrow()
231
+ .select_at_idx(idx)
232
+ .map(|s| RbSeries::new(s.clone()))
233
+ }
234
+
235
+ pub fn get_column_index(&self, name: String) -> Option<usize> {
236
+ self.df.borrow().get_column_index(&name)
237
+ }
238
+
239
+ pub fn get_column(&self, name: String) -> RbResult<RbSeries> {
240
+ self.df
241
+ .borrow()
242
+ .column(&name)
243
+ .map(|s| RbSeries::new(s.clone()))
244
+ .map_err(RbPolarsErr::from)
245
+ }
246
+
247
+ pub fn select(&self, selection: Vec<String>) -> RbResult<Self> {
248
+ let df = self
249
+ .df
250
+ .borrow()
251
+ .select(selection)
252
+ .map_err(RbPolarsErr::from)?;
253
+ Ok(RbDataFrame::new(df))
254
+ }
255
+
256
+ pub fn gather(&self, indices: Vec<IdxSize>) -> RbResult<Self> {
257
+ let indices = IdxCa::from_vec("", indices);
258
+ let df = self.df.borrow().take(&indices).map_err(RbPolarsErr::from)?;
259
+ Ok(RbDataFrame::new(df))
260
+ }
261
+
262
+ pub fn take_with_series(&self, indices: &RbSeries) -> RbResult<Self> {
263
+ let binding = indices.series.borrow();
264
+ let idx = binding.idx().map_err(RbPolarsErr::from)?;
265
+ let df = self.df.borrow().take(idx).map_err(RbPolarsErr::from)?;
266
+ Ok(RbDataFrame::new(df))
267
+ }
268
+
269
+ pub fn replace(&self, column: String, new_col: &RbSeries) -> RbResult<()> {
270
+ self.df
271
+ .borrow_mut()
272
+ .replace(&column, new_col.series.borrow().clone())
273
+ .map_err(RbPolarsErr::from)?;
274
+ Ok(())
275
+ }
276
+
277
+ pub fn replace_column(&self, index: usize, new_col: &RbSeries) -> RbResult<()> {
278
+ self.df
279
+ .borrow_mut()
280
+ .replace_column(index, new_col.series.borrow().clone())
281
+ .map_err(RbPolarsErr::from)?;
282
+ Ok(())
283
+ }
284
+
285
+ pub fn insert_column(&self, index: usize, new_col: &RbSeries) -> RbResult<()> {
286
+ self.df
287
+ .borrow_mut()
288
+ .insert_column(index, new_col.series.borrow().clone())
289
+ .map_err(RbPolarsErr::from)?;
290
+ Ok(())
291
+ }
292
+
293
+ pub fn slice(&self, offset: usize, length: Option<usize>) -> Self {
294
+ let df = self.df.borrow().slice(
295
+ offset as i64,
296
+ length.unwrap_or_else(|| self.df.borrow().height()),
297
+ );
298
+ df.into()
299
+ }
300
+
301
+ pub fn head(&self, length: Option<usize>) -> Self {
302
+ self.df.borrow().head(length).into()
303
+ }
304
+
305
+ pub fn tail(&self, length: Option<usize>) -> Self {
306
+ self.df.borrow().tail(length).into()
307
+ }
308
+
309
+ pub fn is_unique(&self) -> RbResult<RbSeries> {
310
+ let mask = self.df.borrow().is_unique().map_err(RbPolarsErr::from)?;
311
+ Ok(mask.into_series().into())
312
+ }
313
+
314
+ pub fn is_duplicated(&self) -> RbResult<RbSeries> {
315
+ let mask = self
316
+ .df
317
+ .borrow()
318
+ .is_duplicated()
319
+ .map_err(RbPolarsErr::from)?;
320
+ Ok(mask.into_series().into())
321
+ }
322
+
323
+ pub fn equals(&self, other: &RbDataFrame, null_equal: bool) -> bool {
324
+ if null_equal {
325
+ self.df.borrow().equals_missing(&other.df.borrow())
326
+ } else {
327
+ self.df.borrow().equals(&other.df.borrow())
328
+ }
329
+ }
330
+
331
+ pub fn with_row_index(&self, name: String, offset: Option<IdxSize>) -> RbResult<Self> {
332
+ let df = self
333
+ .df
334
+ .borrow()
335
+ .with_row_index(&name, offset)
336
+ .map_err(RbPolarsErr::from)?;
337
+ Ok(df.into())
338
+ }
339
+
340
+ pub fn clone(&self) -> Self {
341
+ RbDataFrame::new(self.df.borrow().clone())
342
+ }
343
+
344
+ pub fn unpivot(
345
+ &self,
346
+ on: Vec<String>,
347
+ index: Vec<String>,
348
+ value_name: Option<String>,
349
+ variable_name: Option<String>,
350
+ ) -> RbResult<Self> {
351
+ let args = UnpivotArgs {
352
+ on: strings_to_smartstrings(on),
353
+ index: strings_to_smartstrings(index),
354
+ value_name: value_name.map(|s| s.into()),
355
+ variable_name: variable_name.map(|s| s.into()),
356
+ streamable: false,
357
+ };
358
+
359
+ let df = self.df.borrow().unpivot2(args).map_err(RbPolarsErr::from)?;
360
+ Ok(RbDataFrame::new(df))
361
+ }
362
+
363
+ #[allow(clippy::too_many_arguments)]
364
+ pub fn pivot_expr(
365
+ &self,
366
+ on: Vec<String>,
367
+ index: Option<Vec<String>>,
368
+ values: Option<Vec<String>>,
369
+ maintain_order: bool,
370
+ sort_columns: bool,
371
+ aggregate_expr: Option<&RbExpr>,
372
+ separator: Option<String>,
373
+ ) -> RbResult<Self> {
374
+ let fun = if maintain_order { pivot_stable } else { pivot };
375
+ let agg_expr = aggregate_expr.map(|aggregate_expr| aggregate_expr.inner.clone());
376
+ let df = fun(
377
+ &self.df.borrow(),
378
+ on,
379
+ index,
380
+ values,
381
+ sort_columns,
382
+ agg_expr,
383
+ separator.as_deref(),
384
+ )
385
+ .map_err(RbPolarsErr::from)?;
386
+ Ok(RbDataFrame::new(df))
387
+ }
388
+
389
+ pub fn partition_by(
390
+ &self,
391
+ by: Vec<String>,
392
+ maintain_order: bool,
393
+ include_key: bool,
394
+ ) -> RbResult<RArray> {
395
+ let out = if maintain_order {
396
+ self.df.borrow().partition_by_stable(by, include_key)
397
+ } else {
398
+ self.df.borrow().partition_by(by, include_key)
399
+ }
400
+ .map_err(RbPolarsErr::from)?;
401
+ Ok(RArray::from_iter(out.into_iter().map(RbDataFrame::new)))
402
+ }
403
+
404
+ pub fn lazy(&self) -> RbLazyFrame {
405
+ self.df.borrow().clone().lazy().into()
406
+ }
407
+
408
+ pub fn max_horizontal(&self) -> RbResult<Option<RbSeries>> {
409
+ let s = self
410
+ .df
411
+ .borrow()
412
+ .max_horizontal()
413
+ .map_err(RbPolarsErr::from)?;
414
+ Ok(s.map(|s| s.into()))
415
+ }
416
+
417
+ pub fn min_horizontal(&self) -> RbResult<Option<RbSeries>> {
418
+ let s = self
419
+ .df
420
+ .borrow()
421
+ .min_horizontal()
422
+ .map_err(RbPolarsErr::from)?;
423
+ Ok(s.map(|s| s.into()))
424
+ }
425
+
426
+ pub fn sum_horizontal(&self, ignore_nulls: bool) -> RbResult<Option<RbSeries>> {
427
+ let null_strategy = if ignore_nulls {
428
+ NullStrategy::Ignore
429
+ } else {
430
+ NullStrategy::Propagate
431
+ };
432
+ let s = self
433
+ .df
434
+ .borrow()
435
+ .sum_horizontal(null_strategy)
436
+ .map_err(RbPolarsErr::from)?;
437
+ Ok(s.map(|s| s.into()))
438
+ }
439
+
440
+ pub fn mean_horizontal(&self, ignore_nulls: bool) -> RbResult<Option<RbSeries>> {
441
+ let null_strategy = if ignore_nulls {
442
+ NullStrategy::Ignore
443
+ } else {
444
+ NullStrategy::Propagate
445
+ };
446
+ let s = self
447
+ .df
448
+ .borrow()
449
+ .mean_horizontal(null_strategy)
450
+ .map_err(RbPolarsErr::from)?;
451
+ Ok(s.map(|s| s.into()))
452
+ }
453
+
454
+ pub fn to_dummies(
455
+ &self,
456
+ columns: Option<Vec<String>>,
457
+ separator: Option<String>,
458
+ drop_first: bool,
459
+ ) -> RbResult<Self> {
460
+ let df = match columns {
461
+ Some(cols) => self.df.borrow().columns_to_dummies(
462
+ cols.iter().map(|x| x as &str).collect(),
463
+ separator.as_deref(),
464
+ drop_first,
465
+ ),
466
+ None => self
467
+ .df
468
+ .borrow()
469
+ .to_dummies(separator.as_deref(), drop_first),
470
+ }
471
+ .map_err(RbPolarsErr::from)?;
472
+ Ok(df.into())
473
+ }
474
+
475
+ pub fn null_count(&self) -> Self {
476
+ let df = self.df.borrow().null_count();
477
+ df.into()
478
+ }
479
+
480
+ pub fn map_rows(
481
+ &self,
482
+ lambda: Value,
483
+ output_type: Option<Wrap<DataType>>,
484
+ inference_size: usize,
485
+ ) -> RbResult<(Value, bool)> {
486
+ let df = &self.df.borrow();
487
+
488
+ let output_type = output_type.map(|dt| dt.0);
489
+ let out = match output_type {
490
+ Some(DataType::Int32) => {
491
+ apply_lambda_with_primitive_out_type::<Int32Type>(df, lambda, 0, None).into_series()
492
+ }
493
+ Some(DataType::Int64) => {
494
+ apply_lambda_with_primitive_out_type::<Int64Type>(df, lambda, 0, None).into_series()
495
+ }
496
+ Some(DataType::UInt32) => {
497
+ apply_lambda_with_primitive_out_type::<UInt32Type>(df, lambda, 0, None)
498
+ .into_series()
499
+ }
500
+ Some(DataType::UInt64) => {
501
+ apply_lambda_with_primitive_out_type::<UInt64Type>(df, lambda, 0, None)
502
+ .into_series()
503
+ }
504
+ Some(DataType::Float32) => {
505
+ apply_lambda_with_primitive_out_type::<Float32Type>(df, lambda, 0, None)
506
+ .into_series()
507
+ }
508
+ Some(DataType::Float64) => {
509
+ apply_lambda_with_primitive_out_type::<Float64Type>(df, lambda, 0, None)
510
+ .into_series()
511
+ }
512
+ Some(DataType::Boolean) => {
513
+ apply_lambda_with_bool_out_type(df, lambda, 0, None).into_series()
514
+ }
515
+ Some(DataType::Date) => {
516
+ apply_lambda_with_primitive_out_type::<Int32Type>(df, lambda, 0, None)
517
+ .into_date()
518
+ .into_series()
519
+ }
520
+ Some(DataType::Datetime(tu, tz)) => {
521
+ apply_lambda_with_primitive_out_type::<Int64Type>(df, lambda, 0, None)
522
+ .into_datetime(tu, tz)
523
+ .into_series()
524
+ }
525
+ Some(DataType::String) => {
526
+ apply_lambda_with_utf8_out_type(df, lambda, 0, None).into_series()
527
+ }
528
+ _ => return apply_lambda_unknown(df, lambda, inference_size),
529
+ };
530
+
531
+ Ok((Obj::wrap(RbSeries::from(out)).as_value(), false))
532
+ }
533
+
534
+ pub fn shrink_to_fit(&self) {
535
+ self.df.borrow_mut().shrink_to_fit();
536
+ }
537
+
538
+ pub fn hash_rows(&self, k0: u64, k1: u64, k2: u64, k3: u64) -> RbResult<RbSeries> {
539
+ let hb = ahash::RandomState::with_seeds(k0, k1, k2, k3);
540
+ let hash = self
541
+ .df
542
+ .borrow_mut()
543
+ .hash_rows(Some(hb))
544
+ .map_err(RbPolarsErr::from)?;
545
+ Ok(hash.into_series().into())
546
+ }
547
+
548
+ pub fn transpose(&self, keep_names_as: Option<String>, column_names: Value) -> RbResult<Self> {
549
+ let new_col_names = if let Ok(name) = <Vec<String>>::try_convert(column_names) {
550
+ Some(Either::Right(name))
551
+ } else if let Ok(name) = String::try_convert(column_names) {
552
+ Some(Either::Left(name))
553
+ } else {
554
+ None
555
+ };
556
+ Ok(self
557
+ .df
558
+ .borrow_mut()
559
+ .transpose(keep_names_as.as_deref(), new_col_names)
560
+ .map_err(RbPolarsErr::from)?
561
+ .into())
562
+ }
563
+
564
+ pub fn upsample(
565
+ &self,
566
+ by: Vec<String>,
567
+ index_column: String,
568
+ every: String,
569
+ stable: bool,
570
+ ) -> RbResult<Self> {
571
+ let out = if stable {
572
+ self.df
573
+ .borrow()
574
+ .upsample_stable(by, &index_column, Duration::parse(&every))
575
+ } else {
576
+ self.df
577
+ .borrow()
578
+ .upsample(by, &index_column, Duration::parse(&every))
579
+ };
580
+ let out = out.map_err(RbPolarsErr::from)?;
581
+ Ok(out.into())
582
+ }
583
+
584
+ pub fn to_struct(&self, name: String) -> RbSeries {
585
+ let s = self.df.borrow().clone().into_struct(&name);
586
+ s.into_series().into()
587
+ }
588
+
589
+ pub fn unnest(&self, names: Vec<String>) -> RbResult<Self> {
590
+ let df = self.df.borrow().unnest(names).map_err(RbPolarsErr::from)?;
591
+ Ok(df.into())
592
+ }
593
+
594
+ pub fn clear(&self) -> Self {
595
+ self.df.borrow().clear().into()
596
+ }
597
+ }