polars-df 0.15.0 → 0.26.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 (206) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +274 -0
  3. data/Cargo.lock +1465 -867
  4. data/Cargo.toml +3 -0
  5. data/LICENSE.txt +2 -2
  6. data/README.md +87 -37
  7. data/ext/polars/Cargo.toml +47 -16
  8. data/ext/polars/src/c_api/allocator.rs +7 -0
  9. data/ext/polars/src/c_api/mod.rs +1 -0
  10. data/ext/polars/src/catalog/mod.rs +1 -0
  11. data/ext/polars/src/catalog/unity.rs +470 -0
  12. data/ext/polars/src/conversion/any_value.rs +99 -84
  13. data/ext/polars/src/conversion/categorical.rs +30 -0
  14. data/ext/polars/src/conversion/chunked_array.rs +71 -62
  15. data/ext/polars/src/conversion/datetime.rs +63 -0
  16. data/ext/polars/src/conversion/mod.rs +796 -312
  17. data/ext/polars/src/dataframe/construction.rs +6 -18
  18. data/ext/polars/src/dataframe/export.rs +30 -39
  19. data/ext/polars/src/dataframe/general.rs +294 -362
  20. data/ext/polars/src/dataframe/io.rs +33 -150
  21. data/ext/polars/src/dataframe/map.rs +175 -0
  22. data/ext/polars/src/dataframe/mod.rs +37 -5
  23. data/ext/polars/src/dataframe/serde.rs +23 -8
  24. data/ext/polars/src/error.rs +44 -7
  25. data/ext/polars/src/exceptions.rs +21 -8
  26. data/ext/polars/src/expr/array.rs +86 -22
  27. data/ext/polars/src/expr/binary.rs +50 -1
  28. data/ext/polars/src/expr/bitwise.rs +39 -0
  29. data/ext/polars/src/expr/categorical.rs +20 -0
  30. data/ext/polars/src/expr/datatype.rs +51 -0
  31. data/ext/polars/src/expr/datetime.rs +99 -41
  32. data/ext/polars/src/expr/extension.rs +12 -0
  33. data/ext/polars/src/expr/general.rs +252 -128
  34. data/ext/polars/src/expr/list.rs +56 -60
  35. data/ext/polars/src/expr/meta.rs +30 -35
  36. data/ext/polars/src/expr/mod.rs +28 -6
  37. data/ext/polars/src/expr/name.rs +29 -14
  38. data/ext/polars/src/expr/rolling.rs +111 -3
  39. data/ext/polars/src/expr/selector.rs +219 -0
  40. data/ext/polars/src/expr/serde.rs +28 -0
  41. data/ext/polars/src/expr/string.rs +118 -20
  42. data/ext/polars/src/expr/struct.rs +14 -1
  43. data/ext/polars/src/file.rs +194 -86
  44. data/ext/polars/src/functions/aggregation.rs +13 -12
  45. data/ext/polars/src/functions/business.rs +2 -3
  46. data/ext/polars/src/functions/eager.rs +3 -2
  47. data/ext/polars/src/functions/io.rs +90 -18
  48. data/ext/polars/src/functions/lazy.rs +267 -118
  49. data/ext/polars/src/functions/meta.rs +8 -7
  50. data/ext/polars/src/functions/misc.rs +1 -1
  51. data/ext/polars/src/functions/mod.rs +2 -1
  52. data/ext/polars/src/functions/range.rs +88 -31
  53. data/ext/polars/src/functions/strings.rs +6 -0
  54. data/ext/polars/src/functions/utils.rs +8 -0
  55. data/ext/polars/src/interop/arrow/mod.rs +52 -1
  56. data/ext/polars/src/interop/arrow/{to_ruby.rs → to_rb.rs} +37 -7
  57. data/ext/polars/src/interop/arrow/to_rust.rs +43 -0
  58. data/ext/polars/src/interop/numo/to_numo_df.rs +1 -1
  59. data/ext/polars/src/interop/numo/to_numo_series.rs +72 -50
  60. data/ext/polars/src/io/cloud_options.rs +107 -0
  61. data/ext/polars/src/io/mod.rs +4 -0
  62. data/ext/polars/src/io/scan_options.rs +113 -0
  63. data/ext/polars/src/io/sink_options.rs +46 -0
  64. data/ext/polars/src/io/sink_output.rs +21 -0
  65. data/ext/polars/src/lazyframe/exitable.rs +39 -0
  66. data/ext/polars/src/lazyframe/general.rs +846 -368
  67. data/ext/polars/src/lazyframe/mod.rs +58 -5
  68. data/ext/polars/src/lazyframe/optflags.rs +59 -0
  69. data/ext/polars/src/lazyframe/serde.rs +36 -4
  70. data/ext/polars/src/lazyframe/sink.rs +46 -0
  71. data/ext/polars/src/lazygroupby.rs +38 -9
  72. data/ext/polars/src/lib.rs +574 -165
  73. data/ext/polars/src/map/lazy.rs +44 -74
  74. data/ext/polars/src/map/mod.rs +18 -254
  75. data/ext/polars/src/map/series.rs +241 -1087
  76. data/ext/polars/src/on_startup.rs +192 -9
  77. data/ext/polars/src/prelude.rs +1 -0
  78. data/ext/polars/src/rb_modules.rs +10 -57
  79. data/ext/polars/src/ruby/exceptions.rs +26 -0
  80. data/ext/polars/src/ruby/gvl.rs +104 -0
  81. data/ext/polars/src/ruby/lazy.rs +46 -0
  82. data/ext/polars/src/ruby/mod.rs +11 -0
  83. data/ext/polars/src/ruby/numo.rs +52 -0
  84. data/ext/polars/src/ruby/plan_callback.rs +198 -0
  85. data/ext/polars/src/ruby/rb_modules.rs +16 -0
  86. data/ext/polars/src/ruby/ruby_convert_registry.rs +51 -0
  87. data/ext/polars/src/ruby/ruby_function.rs +11 -0
  88. data/ext/polars/src/ruby/ruby_udf.rs +164 -0
  89. data/ext/polars/src/ruby/thread.rs +65 -0
  90. data/ext/polars/src/ruby/utils.rs +39 -0
  91. data/ext/polars/src/series/aggregation.rs +116 -91
  92. data/ext/polars/src/series/arithmetic.rs +16 -22
  93. data/ext/polars/src/series/comparison.rs +101 -222
  94. data/ext/polars/src/series/construction.rs +80 -70
  95. data/ext/polars/src/series/export.rs +98 -56
  96. data/ext/polars/src/series/general.rs +323 -440
  97. data/ext/polars/src/series/import.rs +22 -5
  98. data/ext/polars/src/series/map.rs +103 -0
  99. data/ext/polars/src/series/mod.rs +57 -15
  100. data/ext/polars/src/series/scatter.rs +139 -82
  101. data/ext/polars/src/sql.rs +16 -9
  102. data/ext/polars/src/testing/frame.rs +31 -0
  103. data/ext/polars/src/testing/mod.rs +5 -0
  104. data/ext/polars/src/testing/series.rs +31 -0
  105. data/ext/polars/src/timeout.rs +105 -0
  106. data/ext/polars/src/utils.rs +105 -4
  107. data/lib/polars/array_expr.rb +500 -22
  108. data/lib/polars/array_name_space.rb +384 -10
  109. data/lib/polars/batched_csv_reader.rb +48 -66
  110. data/lib/polars/binary_expr.rb +217 -0
  111. data/lib/polars/binary_name_space.rb +155 -1
  112. data/lib/polars/cat_expr.rb +224 -0
  113. data/lib/polars/cat_name_space.rb +132 -32
  114. data/lib/polars/catalog/unity/catalog_info.rb +20 -0
  115. data/lib/polars/catalog/unity/column_info.rb +31 -0
  116. data/lib/polars/catalog/unity/namespace_info.rb +21 -0
  117. data/lib/polars/catalog/unity/table_info.rb +50 -0
  118. data/lib/polars/catalog.rb +448 -0
  119. data/lib/polars/collect_batches.rb +22 -0
  120. data/lib/polars/config.rb +3 -3
  121. data/lib/polars/convert.rb +201 -36
  122. data/lib/polars/data_frame.rb +2851 -1017
  123. data/lib/polars/data_frame_plot.rb +173 -0
  124. data/lib/polars/data_type_expr.rb +52 -0
  125. data/lib/polars/data_type_group.rb +6 -0
  126. data/lib/polars/data_types.rb +118 -18
  127. data/lib/polars/date_time_expr.rb +426 -84
  128. data/lib/polars/date_time_name_space.rb +384 -111
  129. data/lib/polars/dynamic_group_by.rb +102 -10
  130. data/lib/polars/exceptions.rb +50 -5
  131. data/lib/polars/expr.rb +2159 -915
  132. data/lib/polars/extension_expr.rb +39 -0
  133. data/lib/polars/extension_name_space.rb +39 -0
  134. data/lib/polars/functions/aggregation/horizontal.rb +11 -6
  135. data/lib/polars/functions/aggregation/vertical.rb +2 -3
  136. data/lib/polars/functions/as_datatype.rb +290 -8
  137. data/lib/polars/functions/business.rb +95 -0
  138. data/lib/polars/functions/col.rb +6 -5
  139. data/lib/polars/functions/datatype.rb +62 -0
  140. data/lib/polars/functions/eager.rb +426 -24
  141. data/lib/polars/functions/escape_regex.rb +21 -0
  142. data/lib/polars/functions/lazy.rb +813 -195
  143. data/lib/polars/functions/lit.rb +21 -10
  144. data/lib/polars/functions/range/int_range.rb +74 -2
  145. data/lib/polars/functions/range/linear_space.rb +195 -0
  146. data/lib/polars/functions/range/time_range.rb +1 -1
  147. data/lib/polars/functions/repeat.rb +7 -12
  148. data/lib/polars/functions/whenthen.rb +2 -2
  149. data/lib/polars/group_by.rb +188 -58
  150. data/lib/polars/iceberg_dataset.rb +108 -0
  151. data/lib/polars/in_process_query.rb +37 -0
  152. data/lib/polars/io/cloud.rb +18 -0
  153. data/lib/polars/io/csv.rb +336 -128
  154. data/lib/polars/io/database.rb +19 -4
  155. data/lib/polars/io/delta.rb +134 -0
  156. data/lib/polars/io/iceberg.rb +34 -0
  157. data/lib/polars/io/ipc.rb +63 -63
  158. data/lib/polars/io/json.rb +16 -0
  159. data/lib/polars/io/lines.rb +172 -0
  160. data/lib/polars/io/ndjson.rb +176 -20
  161. data/lib/polars/io/parquet.rb +173 -95
  162. data/lib/polars/io/scan_options.rb +55 -0
  163. data/lib/polars/io/sink_options.rb +27 -0
  164. data/lib/polars/io/utils.rb +17 -0
  165. data/lib/polars/lazy_frame.rb +3017 -622
  166. data/lib/polars/lazy_group_by.rb +436 -2
  167. data/lib/polars/list_expr.rb +551 -59
  168. data/lib/polars/list_name_space.rb +465 -51
  169. data/lib/polars/meta_expr.rb +146 -24
  170. data/lib/polars/name_expr.rb +87 -2
  171. data/lib/polars/query_opt_flags.rb +264 -0
  172. data/lib/polars/rolling_group_by.rb +90 -5
  173. data/lib/polars/scan_cast_options.rb +86 -0
  174. data/lib/polars/schema.rb +128 -0
  175. data/lib/polars/selector.rb +245 -0
  176. data/lib/polars/selectors.rb +1048 -201
  177. data/lib/polars/series.rb +2522 -774
  178. data/lib/polars/series_plot.rb +72 -0
  179. data/lib/polars/slice.rb +1 -1
  180. data/lib/polars/sql_context.rb +13 -6
  181. data/lib/polars/string_cache.rb +19 -72
  182. data/lib/polars/string_expr.rb +561 -107
  183. data/lib/polars/string_name_space.rb +781 -109
  184. data/lib/polars/struct_expr.rb +139 -18
  185. data/lib/polars/struct_name_space.rb +19 -1
  186. data/lib/polars/testing.rb +24 -273
  187. data/lib/polars/utils/constants.rb +2 -0
  188. data/lib/polars/utils/construction/data_frame.rb +410 -0
  189. data/lib/polars/utils/construction/series.rb +350 -0
  190. data/lib/polars/utils/construction/utils.rb +9 -0
  191. data/lib/polars/utils/convert.rb +18 -8
  192. data/lib/polars/utils/deprecation.rb +11 -0
  193. data/lib/polars/utils/parse.rb +62 -9
  194. data/lib/polars/utils/reduce_balanced.rb +43 -0
  195. data/lib/polars/utils/serde.rb +22 -0
  196. data/lib/polars/utils/unstable.rb +19 -0
  197. data/lib/polars/utils/various.rb +86 -1
  198. data/lib/polars/utils.rb +63 -48
  199. data/lib/polars/version.rb +1 -1
  200. data/lib/polars.rb +85 -2
  201. metadata +80 -28
  202. data/ext/polars/src/allocator.rs +0 -13
  203. data/ext/polars/src/batched_csv.rs +0 -138
  204. data/ext/polars/src/functions/string_cache.rs +0 -25
  205. data/ext/polars/src/map/dataframe.rs +0 -338
  206. data/lib/polars/plot.rb +0 -109
@@ -1,79 +1,49 @@
1
- use magnus::{prelude::*, RArray, Value};
1
+ use magnus::{KwArgs, RArray, Ruby, Value, prelude::*, value::Opaque};
2
2
  use polars::prelude::*;
3
3
 
4
- use crate::rb_modules::*;
5
- use crate::{RbExpr, RbSeries, Wrap};
6
-
7
- fn to_series(v: Value, name: &str) -> PolarsResult<Series> {
8
- let rb_rbseries = match v.funcall("_s", ()) {
9
- Ok(s) => s,
10
- // the lambda did not return a series, we try to create a new Ruby Series
11
- _ => {
12
- let data = RArray::new();
13
- data.push(v).unwrap();
14
- let res = series().funcall::<_, _, Value>("new", (name.to_string(), data));
15
-
16
- match res {
17
- Ok(ruby_s) => ruby_s.funcall::<_, _, &RbSeries>("_s", ()).unwrap(),
18
- Err(_) => {
19
- polars_bail!(ComputeError:
20
- "expected a something that could convert to a `Series` but got: {}",
21
- unsafe { v.classname() }
22
- )
23
- }
24
- }
25
- }
26
- };
27
- // Finally get the actual Series
28
- Ok(rb_rbseries.series.borrow().clone())
29
- }
30
-
31
- pub fn binary_lambda(lambda: Value, a: Series, b: Series) -> PolarsResult<Option<Series>> {
32
- // create a RbSeries struct/object for Ruby
33
- let rbseries_a = RbSeries::new(a);
34
- let rbseries_b = RbSeries::new(b);
35
-
36
- // Wrap this RbSeries object in the Ruby side Series wrapper
37
- let ruby_series_wrapper_a: Value = utils().funcall("wrap_s", (rbseries_a,)).unwrap();
38
- let ruby_series_wrapper_b: Value = utils().funcall("wrap_s", (rbseries_b,)).unwrap();
39
-
40
- // call the lambda and get a Ruby side Series wrapper
41
- let result_series_wrapper: Value =
42
- match lambda.funcall("call", (ruby_series_wrapper_a, ruby_series_wrapper_b)) {
43
- Ok(rbobj) => rbobj,
44
- Err(e) => polars_bail!(
45
- ComputeError: "custom Ruby function failed: {}", e,
46
- ),
47
- };
48
- let rbseries = if let Ok(rbexpr) = result_series_wrapper.funcall::<_, _, &RbExpr>("_rbexpr", ())
49
- {
50
- let expr = rbexpr.inner.clone();
51
- let df = DataFrame::empty();
52
- let out = df
53
- .lazy()
54
- .select([expr])
55
- .with_predicate_pushdown(false)
56
- .with_projection_pushdown(false)
57
- .collect()?;
58
-
59
- let s = out.select_at_idx(0).unwrap().clone();
60
- RbSeries::new(s.take_materialized_series())
61
- } else {
62
- return Some(to_series(result_series_wrapper, "")).transpose();
63
- };
64
-
65
- // Finally get the actual Series
66
- let binding = rbseries.series.borrow();
67
- Ok(Some(binding.clone()))
4
+ use crate::expr::ToExprs;
5
+ use crate::expr::datatype::RbDataTypeExpr;
6
+ use crate::ruby::ruby_udf::{RubyUdfExpression, RubyUdfExt};
7
+ use crate::ruby::utils::{TryIntoValue, to_pl_err};
8
+ use crate::{RbExpr, RbResult, RbSeries, Wrap};
9
+
10
+ pub(crate) fn call_lambda_with_series(
11
+ rb: &Ruby,
12
+ s: &[Column],
13
+ output_dtype: Option<DataType>,
14
+ lambda: Opaque<Value>,
15
+ ) -> PolarsResult<Column> {
16
+ let lambda = rb.get_inner(lambda);
17
+
18
+ // Set return_dtype in kwargs
19
+ let dict = rb.hash_new();
20
+ let output_dtype = output_dtype
21
+ .map(|v| Wrap(v).try_into_value_with(rb))
22
+ .transpose()
23
+ .map_err(to_pl_err)?;
24
+ dict.aset(rb.sym_new("return_dtype"), output_dtype)
25
+ .map_err(to_pl_err)?;
26
+
27
+ let series_objects = rb.ary_from_iter(
28
+ s.iter()
29
+ .map(|c| RbSeries::new(c.as_materialized_series().clone())),
30
+ );
31
+
32
+ let result = lambda.funcall::<_, _, &RbSeries>("call", (series_objects, KwArgs(dict)));
33
+ result
34
+ .map_err(to_pl_err)
35
+ .map(|s| s.clone().series.into_inner().into_column())
68
36
  }
69
37
 
70
- pub fn map_single(
71
- _rbexpr: &RbExpr,
72
- _lambda: Value,
73
- _output_type: Option<Wrap<DataType>>,
74
- _agg_list: bool,
75
- _is_elementwise: bool,
76
- _returns_scalar: bool,
77
- ) -> RbExpr {
78
- todo!();
38
+ pub fn map_expr(
39
+ rbexpr: RArray,
40
+ lambda: Value,
41
+ output_type: Option<&RbDataTypeExpr>,
42
+ is_elementwise: bool,
43
+ returns_scalar: bool,
44
+ ) -> RbResult<RbExpr> {
45
+ let output_type = output_type.map(|v| v.inner.clone());
46
+ let func = RubyUdfExpression::new(lambda, output_type, is_elementwise, returns_scalar);
47
+ let exprs = rbexpr.to_exprs()?;
48
+ Ok(Expr::map_many_ruby(exprs, func).into())
79
49
  }
@@ -1,259 +1,23 @@
1
- pub mod dataframe;
2
1
  pub mod lazy;
3
2
  pub mod series;
4
3
 
5
- use magnus::{prelude::*, RHash, Value};
6
- use polars::chunked_array::builder::get_list_builder;
4
+ use magnus::{Ruby, Value, prelude::*};
7
5
  use polars::prelude::*;
8
- use polars_core::export::rayon::prelude::*;
9
- use polars_core::utils::CustomIterTools;
10
- use polars_core::POOL;
11
6
 
12
- use crate::{ObjectValue, RbPolarsErr, RbResult, RbSeries, Wrap};
13
-
14
- pub trait RbArrowPrimitiveType: PolarsNumericType {}
15
-
16
- impl RbArrowPrimitiveType for UInt8Type {}
17
- impl RbArrowPrimitiveType for UInt16Type {}
18
- impl RbArrowPrimitiveType for UInt32Type {}
19
- impl RbArrowPrimitiveType for UInt64Type {}
20
- impl RbArrowPrimitiveType for Int8Type {}
21
- impl RbArrowPrimitiveType for Int16Type {}
22
- impl RbArrowPrimitiveType for Int32Type {}
23
- impl RbArrowPrimitiveType for Int64Type {}
24
- impl RbArrowPrimitiveType for Float32Type {}
25
- impl RbArrowPrimitiveType for Float64Type {}
26
-
27
- fn iterator_to_struct(
28
- it: impl Iterator<Item = Option<Value>>,
29
- init_null_count: usize,
30
- first_value: AnyValue,
31
- name: PlSmallStr,
32
- capacity: usize,
33
- ) -> RbResult<RbSeries> {
34
- let (vals, flds) = match &first_value {
35
- av @ AnyValue::Struct(_, _, flds) => (av._iter_struct_av().collect::<Vec<_>>(), &**flds),
36
- AnyValue::StructOwned(payload) => (payload.0.clone(), &*payload.1),
37
- _ => {
38
- return Err(crate::exceptions::ComputeError::new_err(format!(
39
- "expected struct got {first_value:?}",
40
- )))
41
- }
42
- };
43
-
44
- let struct_width = vals.len();
45
-
46
- // every item in the struct is kept as its own buffer of anyvalues
47
- // so as struct with 2 items: {a, b}
48
- // will have
49
- // [
50
- // [ a values ]
51
- // [ b values ]
52
- // ]
53
- let mut items = Vec::with_capacity(vals.len());
54
- for item in vals {
55
- let mut buf = Vec::with_capacity(capacity);
56
- for _ in 0..init_null_count {
57
- buf.push(AnyValue::Null);
58
- }
59
- buf.push(item.clone());
60
- items.push(buf);
61
- }
62
-
63
- for dict in it {
64
- match dict {
65
- None => {
66
- for field_items in &mut items {
67
- field_items.push(AnyValue::Null);
68
- }
69
- }
70
- Some(dict) => {
71
- let dict = RHash::try_convert(dict)?;
72
- if dict.len() != struct_width {
73
- return Err(crate::exceptions::ComputeError::new_err(
74
- format!("Cannot create struct type.\n> The struct dtype expects {} fields, but it got a dict with {} fields.", struct_width, dict.len())
75
- ));
76
- }
77
- // we ignore the keys of the rest of the dicts
78
- // the first item determines the output name
79
- todo!()
80
- // for ((_, val), field_items) in dict.iter().zip(&mut items) {
81
- // let item = Wrap::<AnyValue>::try_convert(val)?;
82
- // field_items.push(item.0)
83
- // }
84
- }
85
- }
86
- }
87
-
88
- let fields = POOL.install(|| {
89
- items
90
- .par_iter()
91
- .zip(flds)
92
- .map(|(av, fld)| Series::new(fld.name().clone(), av))
93
- .collect::<Vec<_>>()
94
- });
95
-
96
- Ok(
97
- StructChunked::from_series(name, fields[0].len(), fields.iter())
98
- .unwrap()
99
- .into_series()
100
- .into(),
101
- )
102
- }
103
-
104
- fn iterator_to_primitive<T>(
105
- it: impl Iterator<Item = Option<T::Native>>,
106
- init_null_count: usize,
107
- first_value: Option<T::Native>,
108
- name: PlSmallStr,
109
- capacity: usize,
110
- ) -> ChunkedArray<T>
111
- where
112
- T: RbArrowPrimitiveType,
113
- {
114
- // safety: we know the iterators len
115
- let mut ca: ChunkedArray<T> = unsafe {
116
- if init_null_count > 0 {
117
- (0..init_null_count)
118
- .map(|_| None)
119
- .chain(std::iter::once(first_value))
120
- .chain(it)
121
- .trust_my_length(capacity)
122
- .collect_trusted()
123
- } else if first_value.is_some() {
124
- std::iter::once(first_value)
125
- .chain(it)
126
- .trust_my_length(capacity)
127
- .collect_trusted()
128
- } else {
129
- it.collect()
130
- }
131
- };
132
- debug_assert_eq!(ca.len(), capacity);
133
- ca.rename(name);
134
- ca
135
- }
136
-
137
- fn iterator_to_bool(
138
- it: impl Iterator<Item = Option<bool>>,
139
- init_null_count: usize,
140
- first_value: Option<bool>,
141
- name: PlSmallStr,
142
- capacity: usize,
143
- ) -> ChunkedArray<BooleanType> {
144
- // safety: we know the iterators len
145
- let mut ca: BooleanChunked = unsafe {
146
- if init_null_count > 0 {
147
- (0..init_null_count)
148
- .map(|_| None)
149
- .chain(std::iter::once(first_value))
150
- .chain(it)
151
- .trust_my_length(capacity)
152
- .collect_trusted()
153
- } else if first_value.is_some() {
154
- std::iter::once(first_value)
155
- .chain(it)
156
- .trust_my_length(capacity)
157
- .collect_trusted()
158
- } else {
159
- it.collect()
160
- }
161
- };
162
- debug_assert_eq!(ca.len(), capacity);
163
- ca.rename(name);
164
- ca
165
- }
166
-
167
- fn iterator_to_object(
168
- it: impl Iterator<Item = Option<ObjectValue>>,
169
- init_null_count: usize,
170
- first_value: Option<ObjectValue>,
171
- name: PlSmallStr,
172
- capacity: usize,
173
- ) -> ObjectChunked<ObjectValue> {
174
- // safety: we know the iterators len
175
- let mut ca: ObjectChunked<ObjectValue> = unsafe {
176
- if init_null_count > 0 {
177
- (0..init_null_count)
178
- .map(|_| None)
179
- .chain(std::iter::once(first_value))
180
- .chain(it)
181
- .trust_my_length(capacity)
182
- .collect_trusted()
183
- } else if first_value.is_some() {
184
- std::iter::once(first_value)
185
- .chain(it)
186
- .trust_my_length(capacity)
187
- .collect_trusted()
188
- } else {
189
- it.collect()
190
- }
191
- };
192
- debug_assert_eq!(ca.len(), capacity);
193
- ca.rename(name);
194
- ca
195
- }
196
-
197
- fn iterator_to_utf8(
198
- it: impl Iterator<Item = Option<String>>,
199
- init_null_count: usize,
200
- first_value: Option<&str>,
201
- name: PlSmallStr,
202
- capacity: usize,
203
- ) -> StringChunked {
204
- let first_value = first_value.map(|v| v.to_string());
205
-
206
- // safety: we know the iterators len
207
- let mut ca: StringChunked = unsafe {
208
- if init_null_count > 0 {
209
- (0..init_null_count)
210
- .map(|_| None)
211
- .chain(std::iter::once(first_value))
212
- .chain(it)
213
- .trust_my_length(capacity)
214
- .collect_trusted()
215
- } else if first_value.is_some() {
216
- std::iter::once(first_value)
217
- .chain(it)
218
- .trust_my_length(capacity)
219
- .collect_trusted()
220
- } else {
221
- it.collect()
222
- }
223
- };
224
- debug_assert_eq!(ca.len(), capacity);
225
- ca.rename(name);
226
- ca
227
- }
228
-
229
- fn iterator_to_list(
230
- dt: &DataType,
231
- it: impl Iterator<Item = Option<Series>>,
232
- init_null_count: usize,
233
- first_value: Option<&Series>,
234
- name: PlSmallStr,
235
- capacity: usize,
236
- ) -> RbResult<ListChunked> {
237
- let mut builder = get_list_builder(dt, capacity * 5, capacity, name);
238
- for _ in 0..init_null_count {
239
- builder.append_null()
240
- }
241
- builder
242
- .append_opt_series(first_value)
243
- .map_err(RbPolarsErr::from)?;
244
- for opt_val in it {
245
- match opt_val {
246
- None => builder.append_null(),
247
- Some(s) => {
248
- if s.len() == 0 && s.dtype() != dt {
249
- builder
250
- .append_series(&Series::full_null(PlSmallStr::EMPTY, 0, dt))
251
- .unwrap()
252
- } else {
253
- builder.append_series(&s).map_err(RbPolarsErr::from)?
254
- }
255
- }
256
- }
257
- }
258
- Ok(builder.finish())
259
- }
7
+ use crate::{RbResult, RbSeries};
8
+
9
+ pub trait RbPolarsNumericType: PolarsNumericType {}
10
+
11
+ impl RbPolarsNumericType for UInt8Type {}
12
+ impl RbPolarsNumericType for UInt16Type {}
13
+ impl RbPolarsNumericType for UInt32Type {}
14
+ impl RbPolarsNumericType for UInt64Type {}
15
+ impl RbPolarsNumericType for UInt128Type {}
16
+ impl RbPolarsNumericType for Int8Type {}
17
+ impl RbPolarsNumericType for Int16Type {}
18
+ impl RbPolarsNumericType for Int32Type {}
19
+ impl RbPolarsNumericType for Int64Type {}
20
+ impl RbPolarsNumericType for Int128Type {}
21
+ impl RbPolarsNumericType for Float16Type {}
22
+ impl RbPolarsNumericType for Float32Type {}
23
+ impl RbPolarsNumericType for Float64Type {}