polars-df 0.14.0 → 0.16.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 (87) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +35 -0
  3. data/Cargo.lock +1523 -378
  4. data/LICENSE.txt +1 -0
  5. data/README.md +38 -4
  6. data/ext/polars/Cargo.toml +15 -5
  7. data/ext/polars/src/batched_csv.rs +7 -10
  8. data/ext/polars/src/conversion/any_value.rs +31 -21
  9. data/ext/polars/src/conversion/mod.rs +155 -48
  10. data/ext/polars/src/dataframe/construction.rs +0 -3
  11. data/ext/polars/src/dataframe/export.rs +9 -2
  12. data/ext/polars/src/dataframe/general.rs +15 -57
  13. data/ext/polars/src/dataframe/io.rs +77 -169
  14. data/ext/polars/src/dataframe/mod.rs +1 -0
  15. data/ext/polars/src/dataframe/serde.rs +15 -0
  16. data/ext/polars/src/error.rs +31 -48
  17. data/ext/polars/src/exceptions.rs +24 -0
  18. data/ext/polars/src/expr/binary.rs +4 -42
  19. data/ext/polars/src/expr/datetime.rs +5 -4
  20. data/ext/polars/src/expr/general.rs +16 -22
  21. data/ext/polars/src/expr/list.rs +18 -11
  22. data/ext/polars/src/expr/meta.rs +6 -2
  23. data/ext/polars/src/expr/rolling.rs +6 -7
  24. data/ext/polars/src/expr/string.rs +9 -36
  25. data/ext/polars/src/file.rs +78 -23
  26. data/ext/polars/src/functions/aggregation.rs +4 -4
  27. data/ext/polars/src/functions/business.rs +15 -0
  28. data/ext/polars/src/functions/io.rs +34 -13
  29. data/ext/polars/src/functions/lazy.rs +22 -12
  30. data/ext/polars/src/functions/meta.rs +1 -1
  31. data/ext/polars/src/functions/mod.rs +1 -0
  32. data/ext/polars/src/interop/arrow/mod.rs +1 -0
  33. data/ext/polars/src/interop/arrow/to_ruby.rs +83 -0
  34. data/ext/polars/src/interop/mod.rs +1 -0
  35. data/ext/polars/src/lazyframe/general.rs +920 -0
  36. data/ext/polars/src/lazyframe/mod.rs +3 -827
  37. data/ext/polars/src/lazyframe/serde.rs +31 -0
  38. data/ext/polars/src/lib.rs +54 -27
  39. data/ext/polars/src/map/dataframe.rs +10 -6
  40. data/ext/polars/src/map/lazy.rs +65 -4
  41. data/ext/polars/src/map/mod.rs +9 -8
  42. data/ext/polars/src/on_startup.rs +1 -1
  43. data/ext/polars/src/series/aggregation.rs +1 -5
  44. data/ext/polars/src/series/arithmetic.rs +10 -10
  45. data/ext/polars/src/series/construction.rs +2 -2
  46. data/ext/polars/src/series/export.rs +1 -1
  47. data/ext/polars/src/series/general.rs +631 -0
  48. data/ext/polars/src/series/import.rs +55 -0
  49. data/ext/polars/src/series/mod.rs +11 -638
  50. data/ext/polars/src/series/scatter.rs +2 -2
  51. data/ext/polars/src/utils.rs +0 -20
  52. data/lib/polars/batched_csv_reader.rb +0 -2
  53. data/lib/polars/binary_expr.rb +133 -9
  54. data/lib/polars/binary_name_space.rb +101 -6
  55. data/lib/polars/config.rb +4 -0
  56. data/lib/polars/data_frame.rb +452 -101
  57. data/lib/polars/data_type_group.rb +28 -0
  58. data/lib/polars/data_types.rb +3 -1
  59. data/lib/polars/date_time_expr.rb +244 -0
  60. data/lib/polars/date_time_name_space.rb +87 -0
  61. data/lib/polars/expr.rb +103 -2
  62. data/lib/polars/functions/aggregation/horizontal.rb +10 -4
  63. data/lib/polars/functions/as_datatype.rb +51 -2
  64. data/lib/polars/functions/col.rb +1 -1
  65. data/lib/polars/functions/eager.rb +1 -3
  66. data/lib/polars/functions/lazy.rb +95 -13
  67. data/lib/polars/functions/range/time_range.rb +21 -21
  68. data/lib/polars/io/csv.rb +14 -16
  69. data/lib/polars/io/database.rb +2 -2
  70. data/lib/polars/io/delta.rb +126 -0
  71. data/lib/polars/io/ipc.rb +14 -4
  72. data/lib/polars/io/ndjson.rb +10 -0
  73. data/lib/polars/io/parquet.rb +168 -111
  74. data/lib/polars/lazy_frame.rb +684 -20
  75. data/lib/polars/list_name_space.rb +169 -0
  76. data/lib/polars/selectors.rb +1226 -0
  77. data/lib/polars/series.rb +465 -35
  78. data/lib/polars/string_cache.rb +27 -1
  79. data/lib/polars/string_expr.rb +0 -1
  80. data/lib/polars/string_name_space.rb +73 -3
  81. data/lib/polars/struct_name_space.rb +31 -7
  82. data/lib/polars/utils/various.rb +5 -1
  83. data/lib/polars/utils.rb +45 -10
  84. data/lib/polars/version.rb +1 -1
  85. data/lib/polars.rb +17 -1
  86. metadata +16 -9
  87. data/lib/polars/functions.rb +0 -57
@@ -3,17 +3,15 @@ mod arithmetic;
3
3
  mod comparison;
4
4
  mod construction;
5
5
  mod export;
6
+ mod general;
7
+ mod import;
6
8
  mod scatter;
7
9
 
8
- use magnus::{exception, prelude::*, Error, IntoValue, RArray, Value};
10
+ use magnus::{prelude::*, RArray};
9
11
  use polars::prelude::*;
10
- use polars::series::IsSorted;
11
12
  use std::cell::RefCell;
12
13
 
13
- use crate::apply_method_all_arrow_series2;
14
- use crate::conversion::*;
15
- use crate::map::series::{call_lambda_and_extract, ApplyLambda};
16
- use crate::{RbDataFrame, RbPolarsErr, RbResult};
14
+ use crate::RbResult;
17
15
 
18
16
  #[magnus::wrap(class = "Polars::RbSeries")]
19
17
  pub struct RbSeries {
@@ -34,7 +32,7 @@ impl RbSeries {
34
32
  }
35
33
  }
36
34
 
37
- pub fn to_series_collection(rs: RArray) -> RbResult<Vec<Series>> {
35
+ pub fn to_series(rs: RArray) -> RbResult<Vec<Series>> {
38
36
  let mut series = Vec::new();
39
37
  for item in rs.into_iter() {
40
38
  series.push(<&RbSeries>::try_convert(item)?.series.borrow().clone());
@@ -42,635 +40,10 @@ pub fn to_series_collection(rs: RArray) -> RbResult<Vec<Series>> {
42
40
  Ok(series)
43
41
  }
44
42
 
45
- pub fn to_rbseries_collection(s: Vec<Series>) -> RArray {
46
- RArray::from_iter(s.into_iter().map(RbSeries::new))
47
- }
48
-
49
- impl RbSeries {
50
- pub fn struct_unnest(&self) -> RbResult<RbDataFrame> {
51
- let binding = self.series.borrow();
52
- let ca = binding.struct_().map_err(RbPolarsErr::from)?;
53
- let df: DataFrame = ca.clone().unnest();
54
- Ok(df.into())
55
- }
56
-
57
- // pub fn struct_fields(&self) -> RbResult<Vec<&str>> {
58
- // let ca = self.series.borrow().struct_().map_err(RbPolarsErr::from)?;
59
- // Ok(ca.fields().iter().map(|s| s.name()).collect())
60
- // }
61
-
62
- pub fn is_sorted_ascending_flag(&self) -> bool {
63
- matches!(self.series.borrow().is_sorted_flag(), IsSorted::Ascending)
64
- }
65
-
66
- pub fn is_sorted_descending_flag(&self) -> bool {
67
- matches!(self.series.borrow().is_sorted_flag(), IsSorted::Descending)
68
- }
69
-
70
- pub fn can_fast_explode_flag(&self) -> bool {
71
- match self.series.borrow().list() {
72
- Err(_) => false,
73
- Ok(list) => list._can_fast_explode(),
74
- }
75
- }
76
-
77
- pub fn cat_uses_lexical_ordering(&self) -> RbResult<bool> {
78
- let binding = self.series.borrow();
79
- let ca = binding.categorical().map_err(RbPolarsErr::from)?;
80
- Ok(ca.uses_lexical_ordering())
81
- }
82
-
83
- pub fn cat_is_local(&self) -> RbResult<bool> {
84
- let binding = self.series.borrow();
85
- let ca = binding.categorical().map_err(RbPolarsErr::from)?;
86
- Ok(ca.get_rev_map().is_local())
87
- }
88
-
89
- pub fn cat_to_local(&self) -> RbResult<Self> {
90
- let binding = self.series.borrow();
91
- let ca = binding.categorical().map_err(RbPolarsErr::from)?;
92
- Ok(ca.to_local().into_series().into())
93
- }
94
-
95
- pub fn estimated_size(&self) -> usize {
96
- self.series.borrow().estimated_size()
97
- }
98
-
99
- pub fn get_fmt(&self, index: usize, str_lengths: usize) -> String {
100
- let val = format!("{}", self.series.borrow().get(index).unwrap());
101
- if let DataType::String | DataType::Categorical(_, _) = self.series.borrow().dtype() {
102
- let v_trunc = &val[..val
103
- .char_indices()
104
- .take(str_lengths)
105
- .last()
106
- .map(|(i, c)| i + c.len_utf8())
107
- .unwrap_or(0)];
108
- if val == v_trunc {
109
- val
110
- } else {
111
- format!("{}…", v_trunc)
112
- }
113
- } else {
114
- val
115
- }
116
- }
117
-
118
- pub fn rechunk(&self, in_place: bool) -> Option<Self> {
119
- let series = self.series.borrow_mut().rechunk();
120
- if in_place {
121
- *self.series.borrow_mut() = series;
122
- None
123
- } else {
124
- Some(series.into())
125
- }
126
- }
127
-
128
- pub fn get_idx(&self, idx: usize) -> RbResult<Value> {
129
- Ok(Wrap(self.series.borrow().get(idx).map_err(RbPolarsErr::from)?).into_value())
130
- }
131
-
132
- pub fn bitand(&self, other: &RbSeries) -> RbResult<Self> {
133
- let out = self
134
- .series
135
- .borrow()
136
- .bitand(&other.series.borrow())
137
- .map_err(RbPolarsErr::from)?;
138
- Ok(out.into())
139
- }
140
-
141
- pub fn bitor(&self, other: &RbSeries) -> RbResult<Self> {
142
- let out = self
143
- .series
144
- .borrow()
145
- .bitor(&other.series.borrow())
146
- .map_err(RbPolarsErr::from)?;
147
- Ok(out.into())
148
- }
149
-
150
- pub fn bitxor(&self, other: &RbSeries) -> RbResult<Self> {
151
- let out = self
152
- .series
153
- .borrow()
154
- .bitxor(&other.series.borrow())
155
- .map_err(RbPolarsErr::from)?;
156
- Ok(out.into())
157
- }
158
-
159
- pub fn chunk_lengths(&self) -> Vec<usize> {
160
- self.series.borrow().chunk_lengths().collect()
161
- }
162
-
163
- pub fn name(&self) -> String {
164
- self.series.borrow().name().to_string()
165
- }
166
-
167
- pub fn rename(&self, name: String) {
168
- self.series.borrow_mut().rename(name.into());
169
- }
170
-
171
- pub fn dtype(&self) -> Value {
172
- Wrap(self.series.borrow().dtype().clone()).into_value()
173
- }
174
-
175
- pub fn inner_dtype(&self) -> Option<Value> {
176
- self.series
177
- .borrow()
178
- .dtype()
179
- .inner_dtype()
180
- .map(|dt| Wrap(dt.clone()).into_value())
181
- }
182
-
183
- pub fn set_sorted_flag(&self, descending: bool) -> Self {
184
- let mut out = self.series.borrow().clone();
185
- if descending {
186
- out.set_sorted_flag(IsSorted::Descending);
187
- } else {
188
- out.set_sorted_flag(IsSorted::Ascending)
189
- }
190
- out.into()
191
- }
192
-
193
- pub fn n_chunks(&self) -> usize {
194
- self.series.borrow().n_chunks()
195
- }
196
-
197
- pub fn append(&self, other: &RbSeries) -> RbResult<()> {
198
- let mut binding = self.series.borrow_mut();
199
- let res = binding.append(&other.series.borrow());
200
- if let Err(e) = res {
201
- Err(Error::new(exception::runtime_error(), e.to_string()))
202
- } else {
203
- Ok(())
204
- }
205
- }
206
-
207
- pub fn extend(&self, other: &RbSeries) -> RbResult<()> {
208
- self.series
209
- .borrow_mut()
210
- .extend(&other.series.borrow())
211
- .map_err(RbPolarsErr::from)?;
212
- Ok(())
213
- }
214
-
215
- pub fn new_from_index(&self, index: usize, length: usize) -> RbResult<Self> {
216
- if index >= self.series.borrow().len() {
217
- Err(Error::new(exception::arg_error(), "index is out of bounds"))
218
- } else {
219
- Ok(self.series.borrow().new_from_index(index, length).into())
220
- }
221
- }
222
-
223
- pub fn filter(&self, filter: &RbSeries) -> RbResult<Self> {
224
- let filter_series = &filter.series.borrow();
225
- if let Ok(ca) = filter_series.bool() {
226
- let series = self.series.borrow().filter(ca).unwrap();
227
- Ok(series.into())
228
- } else {
229
- Err(Error::new(
230
- exception::runtime_error(),
231
- "Expected a boolean mask".to_string(),
232
- ))
233
- }
234
- }
235
-
236
- pub fn sort(&self, descending: bool, nulls_last: bool, multithreaded: bool) -> RbResult<Self> {
237
- Ok(self
238
- .series
239
- .borrow_mut()
240
- .sort(
241
- SortOptions::default()
242
- .with_order_descending(descending)
243
- .with_nulls_last(nulls_last)
244
- .with_multithreaded(multithreaded),
245
- )
246
- .map_err(RbPolarsErr::from)?
247
- .into())
248
- }
249
-
250
- pub fn value_counts(
251
- &self,
252
- sort: bool,
253
- parallel: bool,
254
- name: String,
255
- normalize: bool,
256
- ) -> RbResult<RbDataFrame> {
257
- let out = self
258
- .series
259
- .borrow()
260
- .value_counts(sort, parallel, name.into(), normalize)
261
- .map_err(RbPolarsErr::from)?;
262
- Ok(out.into())
263
- }
264
-
265
- pub fn slice(&self, offset: i64, length: Option<usize>) -> Self {
266
- let length = length.unwrap_or_else(|| self.series.borrow().len());
267
- self.series.borrow().slice(offset, length).into()
268
- }
269
-
270
- pub fn take_with_series(&self, indices: &RbSeries) -> RbResult<Self> {
271
- let binding = indices.series.borrow();
272
- let idx = binding.idx().map_err(RbPolarsErr::from)?;
273
- let take = self.series.borrow().take(idx).map_err(RbPolarsErr::from)?;
274
- Ok(RbSeries::new(take))
275
- }
276
-
277
- pub fn null_count(&self) -> RbResult<usize> {
278
- Ok(self.series.borrow().null_count())
279
- }
280
-
281
- pub fn has_nulls(&self) -> bool {
282
- self.series.borrow().has_nulls()
283
- }
284
-
285
- pub fn sample_n(
286
- &self,
287
- n: usize,
288
- with_replacement: bool,
289
- shuffle: bool,
290
- seed: Option<u64>,
291
- ) -> RbResult<Self> {
292
- let s = self
293
- .series
294
- .borrow()
295
- .sample_n(n, with_replacement, shuffle, seed)
296
- .map_err(RbPolarsErr::from)?;
297
- Ok(s.into())
298
- }
299
-
300
- pub fn sample_frac(
301
- &self,
302
- frac: f64,
303
- with_replacement: bool,
304
- shuffle: bool,
305
- seed: Option<u64>,
306
- ) -> RbResult<Self> {
307
- let s = self
308
- .series
309
- .borrow()
310
- .sample_frac(frac, with_replacement, shuffle, seed)
311
- .map_err(RbPolarsErr::from)?;
312
- Ok(s.into())
313
- }
314
-
315
- pub fn equals(
316
- &self,
317
- other: &RbSeries,
318
- check_dtypes: bool,
319
- check_names: bool,
320
- null_equal: bool,
321
- ) -> bool {
322
- if check_dtypes && (self.series.borrow().dtype() != other.series.borrow().dtype()) {
323
- return false;
324
- }
325
- if check_names && (self.series.borrow().name() != other.series.borrow().name()) {
326
- return false;
327
- }
328
- if null_equal {
329
- self.series.borrow().equals_missing(&other.series.borrow())
330
- } else {
331
- self.series.borrow().equals(&other.series.borrow())
332
- }
333
- }
334
-
335
- pub fn not(&self) -> RbResult<Self> {
336
- let binding = self.series.borrow();
337
- let bool = binding.bool().map_err(RbPolarsErr::from)?;
338
- Ok((!bool).into_series().into())
339
- }
340
-
341
- pub fn to_s(&self) -> String {
342
- format!("{}", self.series.borrow())
343
- }
344
-
345
- pub fn len(&self) -> usize {
346
- self.series.borrow().len()
347
- }
348
-
349
- pub fn clone(&self) -> Self {
350
- RbSeries::new(self.series.borrow().clone())
351
- }
352
-
353
- pub fn apply_lambda(
354
- &self,
355
- lambda: Value,
356
- output_type: Option<Wrap<DataType>>,
357
- skip_nulls: bool,
358
- ) -> RbResult<Self> {
359
- let series = &self.series.borrow();
360
-
361
- let output_type = output_type.map(|dt| dt.0);
362
-
363
- macro_rules! dispatch_apply {
364
- ($self:expr, $method:ident, $($args:expr),*) => {
365
- if matches!($self.dtype(), DataType::Object(_, _)) {
366
- // let ca = $self.0.unpack::<ObjectType<ObjectValue>>().unwrap();
367
- // ca.$method($($args),*)
368
- todo!()
369
- } else {
370
- apply_method_all_arrow_series2!(
371
- $self,
372
- $method,
373
- $($args),*
374
- )
375
- }
376
-
377
- }
378
-
379
- }
380
-
381
- if matches!(
382
- series.dtype(),
383
- DataType::Datetime(_, _)
384
- | DataType::Date
385
- | DataType::Duration(_)
386
- | DataType::Categorical(_, _)
387
- | DataType::Time
388
- ) || !skip_nulls
389
- {
390
- let mut avs = Vec::with_capacity(series.len());
391
- let iter = series.iter().map(|av| {
392
- let input = Wrap(av);
393
- call_lambda_and_extract::<_, Wrap<AnyValue>>(lambda, input)
394
- .unwrap()
395
- .0
396
- });
397
- avs.extend(iter);
398
- return Ok(Series::new(self.name().into(), &avs).into());
399
- }
400
-
401
- let out = match output_type {
402
- Some(DataType::Int8) => {
403
- let ca: Int8Chunked = dispatch_apply!(
404
- series,
405
- apply_lambda_with_primitive_out_type,
406
- lambda,
407
- 0,
408
- None
409
- )?;
410
- ca.into_series()
411
- }
412
- Some(DataType::Int16) => {
413
- let ca: Int16Chunked = dispatch_apply!(
414
- series,
415
- apply_lambda_with_primitive_out_type,
416
- lambda,
417
- 0,
418
- None
419
- )?;
420
- ca.into_series()
421
- }
422
- Some(DataType::Int32) => {
423
- let ca: Int32Chunked = dispatch_apply!(
424
- series,
425
- apply_lambda_with_primitive_out_type,
426
- lambda,
427
- 0,
428
- None
429
- )?;
430
- ca.into_series()
431
- }
432
- Some(DataType::Int64) => {
433
- let ca: Int64Chunked = dispatch_apply!(
434
- series,
435
- apply_lambda_with_primitive_out_type,
436
- lambda,
437
- 0,
438
- None
439
- )?;
440
- ca.into_series()
441
- }
442
- Some(DataType::UInt8) => {
443
- let ca: UInt8Chunked = dispatch_apply!(
444
- series,
445
- apply_lambda_with_primitive_out_type,
446
- lambda,
447
- 0,
448
- None
449
- )?;
450
- ca.into_series()
451
- }
452
- Some(DataType::UInt16) => {
453
- let ca: UInt16Chunked = dispatch_apply!(
454
- series,
455
- apply_lambda_with_primitive_out_type,
456
- lambda,
457
- 0,
458
- None
459
- )?;
460
- ca.into_series()
461
- }
462
- Some(DataType::UInt32) => {
463
- let ca: UInt32Chunked = dispatch_apply!(
464
- series,
465
- apply_lambda_with_primitive_out_type,
466
- lambda,
467
- 0,
468
- None
469
- )?;
470
- ca.into_series()
471
- }
472
- Some(DataType::UInt64) => {
473
- let ca: UInt64Chunked = dispatch_apply!(
474
- series,
475
- apply_lambda_with_primitive_out_type,
476
- lambda,
477
- 0,
478
- None
479
- )?;
480
- ca.into_series()
481
- }
482
- Some(DataType::Float32) => {
483
- let ca: Float32Chunked = dispatch_apply!(
484
- series,
485
- apply_lambda_with_primitive_out_type,
486
- lambda,
487
- 0,
488
- None
489
- )?;
490
- ca.into_series()
491
- }
492
- Some(DataType::Float64) => {
493
- let ca: Float64Chunked = dispatch_apply!(
494
- series,
495
- apply_lambda_with_primitive_out_type,
496
- lambda,
497
- 0,
498
- None
499
- )?;
500
- ca.into_series()
501
- }
502
- Some(DataType::Boolean) => {
503
- let ca: BooleanChunked =
504
- dispatch_apply!(series, apply_lambda_with_bool_out_type, lambda, 0, None)?;
505
- ca.into_series()
506
- }
507
- Some(DataType::Date) => {
508
- let ca: Int32Chunked = dispatch_apply!(
509
- series,
510
- apply_lambda_with_primitive_out_type,
511
- lambda,
512
- 0,
513
- None
514
- )?;
515
- ca.into_date().into_series()
516
- }
517
- Some(DataType::Datetime(tu, tz)) => {
518
- let ca: Int64Chunked = dispatch_apply!(
519
- series,
520
- apply_lambda_with_primitive_out_type,
521
- lambda,
522
- 0,
523
- None
524
- )?;
525
- ca.into_datetime(tu, tz).into_series()
526
- }
527
- Some(DataType::String) => {
528
- let ca = dispatch_apply!(series, apply_lambda_with_utf8_out_type, lambda, 0, None)?;
529
-
530
- ca.into_series()
531
- }
532
- Some(DataType::Object(_, _)) => {
533
- let ca =
534
- dispatch_apply!(series, apply_lambda_with_object_out_type, lambda, 0, None)?;
535
- ca.into_series()
536
- }
537
- None => return dispatch_apply!(series, apply_lambda_unknown, lambda),
538
-
539
- _ => return dispatch_apply!(series, apply_lambda_unknown, lambda),
540
- };
541
-
542
- Ok(RbSeries::new(out))
543
- }
544
-
545
- pub fn zip_with(&self, mask: &RbSeries, other: &RbSeries) -> RbResult<Self> {
546
- let binding = mask.series.borrow();
547
- let mask = binding.bool().map_err(RbPolarsErr::from)?;
548
- let s = self
549
- .series
550
- .borrow()
551
- .zip_with(mask, &other.series.borrow())
552
- .map_err(RbPolarsErr::from)?;
553
- Ok(RbSeries::new(s))
554
- }
555
-
556
- pub fn to_dummies(&self, sep: Option<String>, drop_first: bool) -> RbResult<RbDataFrame> {
557
- let df = self
558
- .series
559
- .borrow()
560
- .to_dummies(sep.as_deref(), drop_first)
561
- .map_err(RbPolarsErr::from)?;
562
- Ok(df.into())
563
- }
564
-
565
- pub fn n_unique(&self) -> RbResult<usize> {
566
- let n = self.series.borrow().n_unique().map_err(RbPolarsErr::from)?;
567
- Ok(n)
568
- }
569
-
570
- pub fn floor(&self) -> RbResult<Self> {
571
- let s = self.series.borrow().floor().map_err(RbPolarsErr::from)?;
572
- Ok(s.into())
573
- }
574
-
575
- pub fn shrink_to_fit(&self) {
576
- self.series.borrow_mut().shrink_to_fit();
577
- }
578
-
579
- pub fn dot(&self, other: &RbSeries) -> RbResult<f64> {
580
- let out = self
581
- .series
582
- .borrow()
583
- .dot(&other.series.borrow())
584
- .map_err(RbPolarsErr::from)?;
585
- Ok(out)
586
- }
587
-
588
- pub fn skew(&self, bias: bool) -> RbResult<Option<f64>> {
589
- let out = self.series.borrow().skew(bias).map_err(RbPolarsErr::from)?;
590
- Ok(out)
591
- }
592
-
593
- pub fn kurtosis(&self, fisher: bool, bias: bool) -> RbResult<Option<f64>> {
594
- let out = self
595
- .series
596
- .borrow()
597
- .kurtosis(fisher, bias)
598
- .map_err(RbPolarsErr::from)?;
599
- Ok(out)
600
- }
601
-
602
- pub fn cast(&self, dtype: Wrap<DataType>, strict: bool) -> RbResult<Self> {
603
- let dtype = dtype.0;
604
- let out = if strict {
605
- self.series.borrow().strict_cast(&dtype)
606
- } else {
607
- self.series.borrow().cast(&dtype)
608
- };
609
- let out = out.map_err(RbPolarsErr::from)?;
610
- Ok(out.into())
611
- }
612
-
613
- pub fn time_unit(&self) -> Option<String> {
614
- if let DataType::Datetime(tu, _) | DataType::Duration(tu) = self.series.borrow().dtype() {
615
- Some(
616
- match tu {
617
- TimeUnit::Nanoseconds => "ns",
618
- TimeUnit::Microseconds => "us",
619
- TimeUnit::Milliseconds => "ms",
620
- }
621
- .to_string(),
622
- )
623
- } else {
624
- None
625
- }
626
- }
627
- }
628
-
629
- macro_rules! impl_set_with_mask {
630
- ($name:ident, $native:ty, $cast:ident, $variant:ident) => {
631
- fn $name(
632
- series: &Series,
633
- filter: &RbSeries,
634
- value: Option<$native>,
635
- ) -> PolarsResult<Series> {
636
- let binding = filter.series.borrow();
637
- let mask = binding.bool()?;
638
- let ca = series.$cast()?;
639
- let new = ca.set(mask, value)?;
640
- Ok(new.into_series())
641
- }
642
-
643
- impl RbSeries {
644
- pub fn $name(&self, filter: &RbSeries, value: Option<$native>) -> RbResult<Self> {
645
- let series =
646
- $name(&self.series.borrow(), filter, value).map_err(RbPolarsErr::from)?;
647
- Ok(Self::new(series))
648
- }
649
- }
650
- };
651
- }
652
-
653
- // impl_set_with_mask!(set_with_mask_str, &str, utf8, Utf8);
654
- impl_set_with_mask!(set_with_mask_f64, f64, f64, Float64);
655
- impl_set_with_mask!(set_with_mask_f32, f32, f32, Float32);
656
- impl_set_with_mask!(set_with_mask_u8, u8, u8, UInt8);
657
- impl_set_with_mask!(set_with_mask_u16, u16, u16, UInt16);
658
- impl_set_with_mask!(set_with_mask_u32, u32, u32, UInt32);
659
- impl_set_with_mask!(set_with_mask_u64, u64, u64, UInt64);
660
- impl_set_with_mask!(set_with_mask_i8, i8, i8, Int8);
661
- impl_set_with_mask!(set_with_mask_i16, i16, i16, Int16);
662
- impl_set_with_mask!(set_with_mask_i32, i32, i32, Int32);
663
- impl_set_with_mask!(set_with_mask_i64, i64, i64, Int64);
664
- impl_set_with_mask!(set_with_mask_bool, bool, bool, Boolean);
665
-
666
- impl RbSeries {
667
- pub fn extend_constant(&self, value: Wrap<AnyValue>, n: usize) -> RbResult<Self> {
668
- Ok(self
669
- .series
670
- .borrow()
671
- .clone()
672
- .extend_constant(value.0, n)
673
- .map_err(RbPolarsErr::from)?
674
- .into())
675
- }
43
+ pub fn to_rbseries(s: Vec<Column>) -> RArray {
44
+ RArray::from_iter(
45
+ s.into_iter()
46
+ .map(|c| c.take_materialized_series())
47
+ .map(RbSeries::new),
48
+ )
676
49
  }
@@ -2,7 +2,7 @@ use polars::export::arrow::array::Array;
2
2
  use polars::prelude::*;
3
3
 
4
4
  use crate::error::RbPolarsErr;
5
- use crate::{RbResult, RbSeries};
5
+ use crate::{RbErr, RbResult, RbSeries};
6
6
 
7
7
  impl RbSeries {
8
8
  pub fn scatter(&self, idx: &RbSeries, values: &RbSeries) -> RbResult<()> {
@@ -12,7 +12,7 @@ impl RbSeries {
12
12
  *s = out;
13
13
  Ok(())
14
14
  }
15
- Err(e) => Err(RbPolarsErr::from(e)),
15
+ Err(e) => Err(RbErr::from(RbPolarsErr::from(e))),
16
16
  }
17
17
  }
18
18
  }
@@ -1,23 +1,3 @@
1
- use polars::prelude::*;
2
-
3
- pub fn reinterpret(s: &Series, signed: bool) -> polars::prelude::PolarsResult<Series> {
4
- match (s.dtype(), signed) {
5
- (DataType::UInt64, true) => {
6
- let ca = s.u64().unwrap();
7
- Ok(ca.reinterpret_signed().into_series())
8
- }
9
- (DataType::UInt64, false) => Ok(s.clone()),
10
- (DataType::Int64, false) => {
11
- let ca = s.i64().unwrap();
12
- Ok(ca.reinterpret_unsigned().into_series())
13
- }
14
- (DataType::Int64, true) => Ok(s.clone()),
15
- _ => Err(PolarsError::ComputeError(
16
- "reinterpret is only allowed for 64bit integers dtype, use cast otherwise".into(),
17
- )),
18
- }
19
- }
20
-
21
1
  #[macro_export]
22
2
  macro_rules! apply_method_all_arrow_series2 {
23
3
  ($self:expr, $method:ident, $($args:expr),*) => {