polars-df 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +14 -0
  3. data/Cargo.lock +337 -381
  4. data/README.md +4 -3
  5. data/ext/polars/Cargo.toml +5 -4
  6. data/ext/polars/src/apply/mod.rs +7 -3
  7. data/ext/polars/src/conversion.rs +171 -63
  8. data/ext/polars/src/dataframe.rs +19 -23
  9. data/ext/polars/src/error.rs +8 -0
  10. data/ext/polars/src/expr/array.rs +15 -0
  11. data/ext/polars/src/expr/general.rs +39 -9
  12. data/ext/polars/src/expr/list.rs +27 -22
  13. data/ext/polars/src/expr/string.rs +10 -9
  14. data/ext/polars/src/expr.rs +1 -0
  15. data/ext/polars/src/functions/lazy.rs +61 -21
  16. data/ext/polars/src/lazyframe.rs +14 -2
  17. data/ext/polars/src/lib.rs +25 -20
  18. data/ext/polars/src/object.rs +1 -1
  19. data/ext/polars/src/rb_modules.rs +4 -0
  20. data/ext/polars/src/series/construction.rs +28 -2
  21. data/ext/polars/src/series.rs +57 -17
  22. data/lib/polars/array_expr.rb +84 -0
  23. data/lib/polars/array_name_space.rb +77 -0
  24. data/lib/polars/batched_csv_reader.rb +1 -1
  25. data/lib/polars/data_frame.rb +91 -49
  26. data/lib/polars/data_types.rb +163 -29
  27. data/lib/polars/date_time_name_space.rb +17 -3
  28. data/lib/polars/expr.rb +76 -69
  29. data/lib/polars/functions.rb +0 -1
  30. data/lib/polars/group_by.rb +1 -22
  31. data/lib/polars/lazy_frame.rb +82 -30
  32. data/lib/polars/lazy_functions.rb +67 -31
  33. data/lib/polars/list_expr.rb +28 -28
  34. data/lib/polars/list_name_space.rb +13 -13
  35. data/lib/polars/rolling_group_by.rb +4 -2
  36. data/lib/polars/series.rb +70 -16
  37. data/lib/polars/string_expr.rb +137 -11
  38. data/lib/polars/string_name_space.rb +137 -22
  39. data/lib/polars/utils.rb +107 -57
  40. data/lib/polars/version.rb +1 -1
  41. data/lib/polars.rb +3 -0
  42. metadata +5 -2
@@ -17,7 +17,7 @@ mod utils;
17
17
  use batched_csv::RbBatchedCsv;
18
18
  use conversion::*;
19
19
  use dataframe::RbDataFrame;
20
- use error::{RbPolarsErr, RbValueError};
20
+ use error::{RbPolarsErr, RbTypeError, RbValueError};
21
21
  use expr::rb_exprs_to_exprs;
22
22
  use expr::RbExpr;
23
23
  use functions::whenthen::{RbWhen, RbWhenThen};
@@ -59,7 +59,7 @@ fn init() -> RbResult<()> {
59
59
  )?;
60
60
  module.define_singleton_method(
61
61
  "_concat_lf",
62
- function!(crate::functions::lazy::concat_lf, 3),
62
+ function!(crate::functions::lazy::concat_lf, 4),
63
63
  )?;
64
64
  module.define_singleton_method(
65
65
  "_diag_concat_df",
@@ -179,7 +179,6 @@ fn init() -> RbResult<()> {
179
179
  "take_with_series",
180
180
  method!(RbDataFrame::take_with_series, 1),
181
181
  )?;
182
- class.define_method("sort", method!(RbDataFrame::sort, 3))?;
183
182
  class.define_method("replace", method!(RbDataFrame::replace, 2))?;
184
183
  class.define_method("replace_at_idx", method!(RbDataFrame::replace_at_idx, 2))?;
185
184
  class.define_method("insert_at_idx", method!(RbDataFrame::insert_at_idx, 2))?;
@@ -193,7 +192,7 @@ fn init() -> RbResult<()> {
193
192
  class.define_method("_clone", method!(RbDataFrame::clone, 0))?;
194
193
  class.define_method("melt", method!(RbDataFrame::melt, 4))?;
195
194
  class.define_method("pivot_expr", method!(RbDataFrame::pivot_expr, 7))?;
196
- class.define_method("partition_by", method!(RbDataFrame::partition_by, 2))?;
195
+ class.define_method("partition_by", method!(RbDataFrame::partition_by, 3))?;
197
196
  class.define_method("shift", method!(RbDataFrame::shift, 1))?;
198
197
  class.define_method("lazy", method!(RbDataFrame::lazy, 0))?;
199
198
  class.define_method("max", method!(RbDataFrame::max, 0))?;
@@ -208,7 +207,7 @@ fn init() -> RbResult<()> {
208
207
  class.define_method("hmin", method!(RbDataFrame::hmin, 0))?;
209
208
  class.define_method("hsum", method!(RbDataFrame::hsum, 1))?;
210
209
  class.define_method("quantile", method!(RbDataFrame::quantile, 2))?;
211
- class.define_method("to_dummies", method!(RbDataFrame::to_dummies, 2))?;
210
+ class.define_method("to_dummies", method!(RbDataFrame::to_dummies, 3))?;
212
211
  class.define_method("null_count", method!(RbDataFrame::null_count, 0))?;
213
212
  class.define_method("apply", method!(RbDataFrame::apply, 3))?;
214
213
  class.define_method("shrink_to_fit", method!(RbDataFrame::shrink_to_fit, 0))?;
@@ -330,13 +329,14 @@ fn init() -> RbResult<()> {
330
329
  class.define_method("cumprod", method!(RbExpr::cumprod, 1))?;
331
330
  class.define_method("product", method!(RbExpr::product, 0))?;
332
331
  class.define_method("shrink_dtype", method!(RbExpr::shrink_dtype, 0))?;
333
- class.define_method("str_parse_date", method!(RbExpr::str_to_date, 4))?;
334
- class.define_method("str_parse_datetime", method!(RbExpr::str_to_datetime, 8))?;
335
- class.define_method("str_parse_time", method!(RbExpr::str_to_time, 3))?;
332
+ class.define_method("str_to_date", method!(RbExpr::str_to_date, 4))?;
333
+ class.define_method("str_to_datetime", method!(RbExpr::str_to_datetime, 6))?;
334
+ class.define_method("str_to_time", method!(RbExpr::str_to_time, 3))?;
336
335
  class.define_method("str_strip", method!(RbExpr::str_strip, 1))?;
337
336
  class.define_method("str_rstrip", method!(RbExpr::str_rstrip, 1))?;
338
337
  class.define_method("str_lstrip", method!(RbExpr::str_lstrip, 1))?;
339
338
  class.define_method("str_slice", method!(RbExpr::str_slice, 2))?;
339
+ class.define_method("str_explode", method!(RbExpr::str_explode, 0))?;
340
340
  class.define_method("str_to_uppercase", method!(RbExpr::str_to_uppercase, 0))?;
341
341
  class.define_method("str_to_lowercase", method!(RbExpr::str_to_lowercase, 0))?;
342
342
  class.define_method("str_lengths", method!(RbExpr::str_lengths, 0))?;
@@ -349,6 +349,9 @@ fn init() -> RbResult<()> {
349
349
  class.define_method("str_contains", method!(RbExpr::str_contains, 3))?;
350
350
  class.define_method("str_ends_with", method!(RbExpr::str_ends_with, 1))?;
351
351
  class.define_method("str_starts_with", method!(RbExpr::str_starts_with, 1))?;
352
+ class.define_method("array_max", method!(RbExpr::array_max, 0))?;
353
+ class.define_method("array_min", method!(RbExpr::array_min, 0))?;
354
+ class.define_method("array_sum", method!(RbExpr::array_sum, 0))?;
352
355
  class.define_method("binary_contains", method!(RbExpr::bin_contains, 1))?;
353
356
  class.define_method("binary_ends_with", method!(RbExpr::bin_ends_with, 1))?;
354
357
  class.define_method("binary_starts_with", method!(RbExpr::bin_starts_with, 1))?;
@@ -357,7 +360,7 @@ fn init() -> RbResult<()> {
357
360
  class.define_method("str_base64_encode", method!(RbExpr::str_base64_encode, 0))?;
358
361
  class.define_method("str_base64_decode", method!(RbExpr::str_base64_decode, 1))?;
359
362
  class.define_method("str_parse_int", method!(RbExpr::str_parse_int, 2))?;
360
- class.define_method("str_json_extract", method!(RbExpr::str_json_extract, 1))?;
363
+ class.define_method("str_json_extract", method!(RbExpr::str_json_extract, 2))?;
361
364
  class.define_method("binary_hex_encode", method!(RbExpr::bin_hex_encode, 0))?;
362
365
  class.define_method("binary_hex_decode", method!(RbExpr::bin_hex_decode, 1))?;
363
366
  class.define_method(
@@ -457,8 +460,8 @@ fn init() -> RbResult<()> {
457
460
  class.define_method("rolling_min", method!(RbExpr::rolling_min, 6))?;
458
461
  class.define_method("rolling_max", method!(RbExpr::rolling_max, 6))?;
459
462
  class.define_method("rolling_mean", method!(RbExpr::rolling_mean, 6))?;
460
- class.define_method("rolling_std", method!(RbExpr::rolling_std, 6))?;
461
- class.define_method("rolling_var", method!(RbExpr::rolling_var, 6))?;
463
+ class.define_method("rolling_std", method!(RbExpr::rolling_std, 7))?;
464
+ class.define_method("rolling_var", method!(RbExpr::rolling_var, 7))?;
462
465
  class.define_method("rolling_median", method!(RbExpr::rolling_median, 6))?;
463
466
  class.define_method("rolling_quantile", method!(RbExpr::rolling_quantile, 8))?;
464
467
  class.define_method("rolling_skew", method!(RbExpr::rolling_skew, 2))?;
@@ -492,9 +495,9 @@ fn init() -> RbResult<()> {
492
495
  class.define_method("reshape", method!(RbExpr::reshape, 1))?;
493
496
  class.define_method("cumcount", method!(RbExpr::cumcount, 1))?;
494
497
  class.define_method("to_physical", method!(RbExpr::to_physical, 0))?;
495
- class.define_method("shuffle", method!(RbExpr::shuffle, 1))?;
496
- class.define_method("sample_n", method!(RbExpr::sample_n, 4))?;
497
- class.define_method("sample_frac", method!(RbExpr::sample_frac, 4))?;
498
+ class.define_method("shuffle", method!(RbExpr::shuffle, 2))?;
499
+ class.define_method("sample_n", method!(RbExpr::sample_n, 5))?;
500
+ class.define_method("sample_frac", method!(RbExpr::sample_frac, 5))?;
498
501
  class.define_method("ewm_mean", method!(RbExpr::ewm_mean, 4))?;
499
502
  class.define_method("ewm_std", method!(RbExpr::ewm_std, 5))?;
500
503
  class.define_method("ewm_var", method!(RbExpr::ewm_var, 5))?;
@@ -517,6 +520,7 @@ fn init() -> RbResult<()> {
517
520
  class.define_method("exp", method!(RbExpr::exp, 0))?;
518
521
  class.define_method("entropy", method!(RbExpr::entropy, 2))?;
519
522
  class.define_method("_hash", method!(RbExpr::hash, 4))?;
523
+ class.define_method("set_sorted_flag", method!(RbExpr::set_sorted_flag, 1))?;
520
524
 
521
525
  // meta
522
526
  class.define_method("meta_pop", method!(RbExpr::meta_pop, 0))?;
@@ -541,9 +545,9 @@ fn init() -> RbResult<()> {
541
545
  class.define_singleton_method("cols", function!(crate::functions::lazy::cols, 1))?;
542
546
  class.define_singleton_method("fold", function!(crate::functions::lazy::fold, 3))?;
543
547
  class.define_singleton_method("cumfold", function!(crate::functions::lazy::cumfold, 4))?;
544
- class.define_singleton_method("lit", function!(crate::functions::lazy::lit, 1))?;
548
+ class.define_singleton_method("lit", function!(crate::functions::lazy::lit, 2))?;
545
549
  class.define_singleton_method("arange", function!(crate::functions::lazy::arange, 3))?;
546
- class.define_singleton_method("repeat", function!(crate::functions::lazy::repeat, 2))?;
550
+ class.define_singleton_method("repeat", function!(crate::functions::lazy::repeat, 3))?;
547
551
  class.define_singleton_method(
548
552
  "pearson_corr",
549
553
  function!(crate::functions::lazy::pearson_corr, 3),
@@ -589,8 +593,8 @@ fn init() -> RbResult<()> {
589
593
  "optimization_toggle",
590
594
  method!(RbLazyFrame::optimization_toggle, 7),
591
595
  )?;
592
- class.define_method("sort", method!(RbLazyFrame::sort, 3))?;
593
- class.define_method("sort_by_exprs", method!(RbLazyFrame::sort_by_exprs, 3))?;
596
+ class.define_method("sort", method!(RbLazyFrame::sort, 4))?;
597
+ class.define_method("sort_by_exprs", method!(RbLazyFrame::sort_by_exprs, 4))?;
594
598
  class.define_method("cache", method!(RbLazyFrame::cache, 0))?;
595
599
  class.define_method("collect", method!(RbLazyFrame::collect, 0))?;
596
600
  class.define_method("sink_parquet", method!(RbLazyFrame::sink_parquet, 7))?;
@@ -598,7 +602,7 @@ fn init() -> RbResult<()> {
598
602
  class.define_method("filter", method!(RbLazyFrame::filter, 1))?;
599
603
  class.define_method("select", method!(RbLazyFrame::select, 1))?;
600
604
  class.define_method("groupby", method!(RbLazyFrame::groupby, 2))?;
601
- class.define_method("groupby_rolling", method!(RbLazyFrame::groupby_rolling, 5))?;
605
+ class.define_method("groupby_rolling", method!(RbLazyFrame::groupby_rolling, 6))?;
602
606
  class.define_method("groupby_dynamic", method!(RbLazyFrame::groupby_dynamic, 9))?;
603
607
  class.define_method("with_context", method!(RbLazyFrame::with_context, 1))?;
604
608
  class.define_method("join_asof", method!(RbLazyFrame::join_asof, 11))?;
@@ -658,6 +662,7 @@ fn init() -> RbResult<()> {
658
662
  class.define_singleton_method("new_null", function!(RbSeries::new_null, 3))?;
659
663
  class.define_singleton_method("new_object", function!(RbSeries::new_object, 3))?;
660
664
  class.define_singleton_method("new_series_list", function!(RbSeries::new_series_list, 3))?;
665
+ class.define_singleton_method("new_array", function!(RbSeries::new_array, 5))?;
661
666
  class.define_singleton_method("new_decimal", function!(RbSeries::new_decimal, 3))?;
662
667
  class.define_singleton_method("repeat", function!(RbSeries::repeat, 4))?;
663
668
  class.define_method("struct_unnest", method!(RbSeries::struct_unnest, 0))?;
@@ -725,7 +730,7 @@ fn init() -> RbResult<()> {
725
730
  class.define_method("_clone", method!(RbSeries::clone, 0))?;
726
731
  class.define_method("apply_lambda", method!(RbSeries::apply_lambda, 3))?;
727
732
  class.define_method("zip_with", method!(RbSeries::zip_with, 2))?;
728
- class.define_method("to_dummies", method!(RbSeries::to_dummies, 1))?;
733
+ class.define_method("to_dummies", method!(RbSeries::to_dummies, 2))?;
729
734
  class.define_method("peak_max", method!(RbSeries::peak_max, 0))?;
730
735
  class.define_method("peak_min", method!(RbSeries::peak_min, 0))?;
731
736
  class.define_method("n_unique", method!(RbSeries::n_unique, 0))?;
@@ -10,7 +10,7 @@ use polars_core::prelude::AnyValue;
10
10
  use crate::prelude::ObjectValue;
11
11
  use crate::Wrap;
12
12
 
13
- // pub(crate) const OBJECT_NAME: &str = "object";
13
+ pub(crate) const OBJECT_NAME: &str = "object";
14
14
 
15
15
  pub(crate) fn register_object_builder() {
16
16
  if !registry::is_object_builder_registered() {
@@ -12,6 +12,10 @@ pub(crate) fn utils() -> RModule {
12
12
  *memoize!(RModule: polars().const_get("Utils").unwrap())
13
13
  }
14
14
 
15
+ pub(crate) fn bigdecimal() -> RClass {
16
+ *memoize!(RClass: class::object().const_get("BigDecimal").unwrap())
17
+ }
18
+
15
19
  pub(crate) fn date() -> RClass {
16
20
  *memoize!(RClass: class::object().const_get("Date").unwrap())
17
21
  }
@@ -1,10 +1,10 @@
1
1
  use magnus::RArray;
2
2
  use polars_core::prelude::*;
3
3
 
4
- use crate::conversion::{slice_extract_wrapped, Wrap};
4
+ use crate::conversion::{slice_extract_wrapped, vec_extract_wrapped, Wrap};
5
5
  use crate::prelude::ObjectValue;
6
6
  use crate::series::to_series_collection;
7
- use crate::{RbPolarsErr, RbResult, RbSeries};
7
+ use crate::{RbPolarsErr, RbResult, RbSeries, RbValueError};
8
8
 
9
9
  impl RbSeries {
10
10
  pub fn new_opt_bool(name: String, obj: RArray, strict: bool) -> RbResult<RbSeries> {
@@ -137,6 +137,32 @@ impl RbSeries {
137
137
  Ok(Series::new(&name, &series_vec).into())
138
138
  }
139
139
 
140
+ pub fn new_array(
141
+ width: usize,
142
+ inner: Option<Wrap<DataType>>,
143
+ name: String,
144
+ val: RArray,
145
+ _strict: bool,
146
+ ) -> RbResult<Self> {
147
+ let val = vec_wrap_any_value(val)?;
148
+ let val = vec_extract_wrapped(val);
149
+ let out = Series::new(&name, &val);
150
+ match out.dtype() {
151
+ DataType::List(list_inner) => {
152
+ let out = out
153
+ .cast(&DataType::Array(
154
+ Box::new(inner.map(|dt| dt.0).unwrap_or(*list_inner.clone())),
155
+ width,
156
+ ))
157
+ .map_err(RbPolarsErr::from)?;
158
+ Ok(out.into())
159
+ }
160
+ _ => Err(RbValueError::new_err(
161
+ "could not create Array from input".to_string(),
162
+ )),
163
+ }
164
+ }
165
+
140
166
  pub fn new_decimal(name: String, val: RArray, strict: bool) -> RbResult<Self> {
141
167
  let val = vec_wrap_any_value(val)?;
142
168
  // TODO: do we have to respect 'strict' here? it's possible if we want to
@@ -303,7 +303,7 @@ impl RbSeries {
303
303
  pub fn to_a(&self) -> Value {
304
304
  let series = &self.series.borrow();
305
305
 
306
- fn to_list_recursive(series: &Series) -> Value {
306
+ fn to_a_recursive(series: &Series) -> Value {
307
307
  let rblist = match series.dtype() {
308
308
  DataType::Boolean => RArray::from_iter(series.bool().unwrap()).into_value(),
309
309
  DataType::UInt8 => RArray::from_iter(series.u8().unwrap()).into_value(),
@@ -339,26 +339,44 @@ impl RbSeries {
339
339
  v.push(QNIL).unwrap();
340
340
  }
341
341
  Some(s) => {
342
- let rblst = to_list_recursive(s.as_ref());
342
+ let rblst = to_a_recursive(s.as_ref());
343
343
  v.push(rblst).unwrap();
344
344
  }
345
345
  }
346
346
  }
347
347
  v.into_value()
348
348
  }
349
- DataType::Date => {
350
- let a = RArray::with_capacity(series.len());
351
- for v in series.iter() {
352
- a.push::<Value>(Wrap(v).into_value()).unwrap();
349
+ DataType::Array(_, _) => {
350
+ let v = RArray::new();
351
+ let ca = series.array().unwrap();
352
+ for opt_s in ca.amortized_iter() {
353
+ match opt_s {
354
+ None => {
355
+ v.push(QNIL).unwrap();
356
+ }
357
+ Some(s) => {
358
+ let rblst = to_a_recursive(s.as_ref());
359
+ v.push(rblst).unwrap();
360
+ }
361
+ }
353
362
  }
354
- return a.into_value();
363
+ v.into_value()
364
+ }
365
+ DataType::Date => {
366
+ let ca = series.date().unwrap();
367
+ return Wrap(ca).into_value();
368
+ }
369
+ DataType::Time => {
370
+ let ca = series.time().unwrap();
371
+ return Wrap(ca).into_value();
355
372
  }
356
373
  DataType::Datetime(_, _) => {
357
- let a = RArray::with_capacity(series.len());
358
- for v in series.iter() {
359
- a.push::<Value>(Wrap(v).into_value()).unwrap();
360
- }
361
- return a.into_value();
374
+ let ca = series.datetime().unwrap();
375
+ return Wrap(ca).into_value();
376
+ }
377
+ DataType::Decimal(_, _) => {
378
+ let ca = series.decimal().unwrap();
379
+ return Wrap(ca).into_value();
362
380
  }
363
381
  DataType::Utf8 => {
364
382
  let ca = series.utf8().unwrap();
@@ -376,15 +394,37 @@ impl RbSeries {
376
394
  let ca = series.binary().unwrap();
377
395
  return Wrap(ca).into_value();
378
396
  }
379
- DataType::Null | DataType::Unknown => {
397
+ DataType::Null => {
398
+ let null: Option<u8> = None;
399
+ let n = series.len();
400
+ let iter = std::iter::repeat(null).take(n);
401
+ use std::iter::{Repeat, Take};
402
+ struct NullIter {
403
+ iter: Take<Repeat<Option<u8>>>,
404
+ n: usize,
405
+ }
406
+ impl Iterator for NullIter {
407
+ type Item = Option<u8>;
408
+
409
+ fn next(&mut self) -> Option<Self::Item> {
410
+ self.iter.next()
411
+ }
412
+ fn size_hint(&self) -> (usize, Option<usize>) {
413
+ (self.n, Some(self.n))
414
+ }
415
+ }
416
+ impl ExactSizeIterator for NullIter {}
417
+
418
+ RArray::from_iter(NullIter { iter, n }).into_value()
419
+ }
420
+ DataType::Unknown => {
380
421
  panic!("to_a not implemented for null/unknown")
381
422
  }
382
- _ => todo!(),
383
423
  };
384
424
  rblist
385
425
  }
386
426
 
387
- to_list_recursive(series)
427
+ to_a_recursive(series)
388
428
  }
389
429
 
390
430
  pub fn clone(&self) -> Self {
@@ -594,11 +634,11 @@ impl RbSeries {
594
634
  Ok(RbSeries::new(s))
595
635
  }
596
636
 
597
- pub fn to_dummies(&self, sep: Option<String>) -> RbResult<RbDataFrame> {
637
+ pub fn to_dummies(&self, sep: Option<String>, drop_first: bool) -> RbResult<RbDataFrame> {
598
638
  let df = self
599
639
  .series
600
640
  .borrow()
601
- .to_dummies(sep.as_deref())
641
+ .to_dummies(sep.as_deref(), drop_first)
602
642
  .map_err(RbPolarsErr::from)?;
603
643
  Ok(df.into())
604
644
  }
@@ -0,0 +1,84 @@
1
+ module Polars
2
+ # Namespace for array related expressions.
3
+ class ArrayExpr
4
+ # @private
5
+ attr_accessor :_rbexpr
6
+
7
+ # @private
8
+ def initialize(expr)
9
+ self._rbexpr = expr._rbexpr
10
+ end
11
+
12
+ # Compute the min values of the sub-arrays.
13
+ #
14
+ # @return [Expr]
15
+ #
16
+ # @example
17
+ # df = Polars::DataFrame.new(
18
+ # {"a" => [[1, 2], [4, 3]]},
19
+ # schema: {"a" => Polars::Array.new(2, Polars::Int64)}
20
+ # )
21
+ # df.select(Polars.col("a").arr.min)
22
+ # # =>
23
+ # # shape: (2, 1)
24
+ # # ┌─────┐
25
+ # # │ a │
26
+ # # │ --- │
27
+ # # │ i64 │
28
+ # # ╞═════╡
29
+ # # │ 1 │
30
+ # # │ 3 │
31
+ # # └─────┘
32
+ def min
33
+ Utils.wrap_expr(_rbexpr.array_min)
34
+ end
35
+
36
+ # Compute the max values of the sub-arrays.
37
+ #
38
+ # @return [Expr]
39
+ #
40
+ # @example
41
+ # df = Polars::DataFrame.new(
42
+ # {"a" => [[1, 2], [4, 3]]},
43
+ # schema: {"a" => Polars::Array.new(2, Polars::Int64)}
44
+ # )
45
+ # df.select(Polars.col("a").arr.max)
46
+ # # =>
47
+ # # shape: (2, 1)
48
+ # # ┌─────┐
49
+ # # │ a │
50
+ # # │ --- │
51
+ # # │ i64 │
52
+ # # ╞═════╡
53
+ # # │ 2 │
54
+ # # │ 4 │
55
+ # # └─────┘
56
+ def max
57
+ Utils.wrap_expr(_rbexpr.array_max)
58
+ end
59
+
60
+ # Compute the sum values of the sub-arrays.
61
+ #
62
+ # @return [Expr]
63
+ #
64
+ # @example
65
+ # df = Polars::DataFrame.new(
66
+ # {"a" => [[1, 2], [4, 3]]},
67
+ # schema: {"a" => Polars::Array.new(2, Polars::Int64)}
68
+ # )
69
+ # df.select(Polars.col("a").arr.sum)
70
+ # # =>
71
+ # # shape: (2, 1)
72
+ # # ┌─────┐
73
+ # # │ a │
74
+ # # │ --- │
75
+ # # │ i64 │
76
+ # # ╞═════╡
77
+ # # │ 3 │
78
+ # # │ 7 │
79
+ # # └─────┘
80
+ def sum
81
+ Utils.wrap_expr(_rbexpr.array_sum)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,77 @@
1
+ module Polars
2
+ # Series.arr namespace.
3
+ class ArrayNameSpace
4
+ include ExprDispatch
5
+
6
+ self._accessor = "arr"
7
+
8
+ # @private
9
+ def initialize(series)
10
+ self._s = series._s
11
+ end
12
+
13
+ # Compute the min values of the sub-arrays.
14
+ #
15
+ # @return [Series]
16
+ #
17
+ # @example
18
+ # s = Polars::Series.new(
19
+ # "a", [[1, 2], [4, 3]], dtype: Polars::Array.new(2, Polars::Int64)
20
+ # )
21
+ # s.arr.min
22
+ # # =>
23
+ # # shape: (2,)
24
+ # # Series: 'a' [i64]
25
+ # # [
26
+ # # 1
27
+ # # 3
28
+ # # ]
29
+ def min
30
+ super
31
+ end
32
+
33
+ # Compute the max values of the sub-arrays.
34
+ #
35
+ # @return [Series]
36
+ #
37
+ # @example
38
+ # s = Polars::Series.new(
39
+ # "a", [[1, 2], [4, 3]], dtype: Polars::Array.new(2, Polars::Int64)
40
+ # )
41
+ # s.arr.max
42
+ # # =>
43
+ # # shape: (2,)
44
+ # # Series: 'a' [i64]
45
+ # # [
46
+ # # 2
47
+ # # 4
48
+ # # ]
49
+ def max
50
+ super
51
+ end
52
+
53
+ # Compute the sum values of the sub-arrays.
54
+ #
55
+ # @return [Series]
56
+ #
57
+ # @example
58
+ # df = Polars::DataFrame.new(
59
+ # {"a" => [[1, 2], [4, 3]]},
60
+ # schema: {"a" => Polars::Array.new(2, Polars::Int64)}
61
+ # )
62
+ # df.select(Polars.col("a").arr.sum)
63
+ # # =>
64
+ # # shape: (2, 1)
65
+ # # ┌─────┐
66
+ # # │ a │
67
+ # # │ --- │
68
+ # # │ i64 │
69
+ # # ╞═════╡
70
+ # # │ 3 │
71
+ # # │ 7 │
72
+ # # └─────┘
73
+ def sum
74
+ super
75
+ end
76
+ end
77
+ end
@@ -41,7 +41,7 @@ module Polars
41
41
  dtypes.each do|k, v|
42
42
  dtype_list << [k, Utils.rb_type_to_dtype(v)]
43
43
  end
44
- elsif dtypes.is_a?(Array)
44
+ elsif dtypes.is_a?(::Array)
45
45
  dtype_slice = dtypes
46
46
  else
47
47
  raise ArgumentError, "dtype arg should be list or dict"