polars-df 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/Cargo.lock +272 -191
  4. data/Cargo.toml +0 -1
  5. data/README.md +2 -2
  6. data/ext/polars/Cargo.toml +8 -4
  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 +1 -0
  10. data/ext/polars/src/batched_csv.rs +7 -5
  11. data/ext/polars/src/conversion.rs +106 -4
  12. data/ext/polars/src/dataframe.rs +19 -17
  13. data/ext/polars/src/error.rs +0 -4
  14. data/ext/polars/src/expr/binary.rs +69 -0
  15. data/ext/polars/src/expr/categorical.rs +10 -0
  16. data/ext/polars/src/expr/datetime.rs +223 -0
  17. data/ext/polars/src/expr/general.rs +933 -0
  18. data/ext/polars/src/expr/list.rs +146 -0
  19. data/ext/polars/src/{lazy → expr}/meta.rs +16 -6
  20. data/ext/polars/src/expr/string.rs +313 -0
  21. data/ext/polars/src/expr/struct.rs +15 -0
  22. data/ext/polars/src/expr.rs +33 -0
  23. data/ext/polars/src/functions/eager.rs +93 -0
  24. data/ext/polars/src/functions/io.rs +34 -0
  25. data/ext/polars/src/functions/lazy.rs +209 -0
  26. data/ext/polars/src/functions/meta.rs +8 -0
  27. data/ext/polars/src/functions/mod.rs +5 -0
  28. data/ext/polars/src/functions/whenthen.rs +43 -0
  29. data/ext/polars/src/{lazy/dataframe.rs → lazyframe.rs} +12 -33
  30. data/ext/polars/src/lazygroupby.rs +29 -0
  31. data/ext/polars/src/lib.rs +205 -303
  32. data/ext/polars/src/rb_modules.rs +8 -0
  33. data/ext/polars/src/series/aggregation.rs +83 -0
  34. data/ext/polars/src/series/arithmetic.rs +88 -0
  35. data/ext/polars/src/series/comparison.rs +251 -0
  36. data/ext/polars/src/series/construction.rs +164 -0
  37. data/ext/polars/src/series.rs +99 -539
  38. data/lib/polars/convert.rb +2 -2
  39. data/lib/polars/data_frame.rb +201 -50
  40. data/lib/polars/data_types.rb +6 -4
  41. data/lib/polars/date_time_expr.rb +142 -2
  42. data/lib/polars/expr.rb +70 -10
  43. data/lib/polars/lazy_frame.rb +4 -3
  44. data/lib/polars/lazy_functions.rb +4 -1
  45. data/lib/polars/list_expr.rb +68 -19
  46. data/lib/polars/series.rb +181 -73
  47. data/lib/polars/string_expr.rb +149 -43
  48. data/lib/polars/string_name_space.rb +4 -4
  49. data/lib/polars/struct_name_space.rb +32 -0
  50. data/lib/polars/utils.rb +41 -7
  51. data/lib/polars/version.rb +1 -1
  52. data/lib/polars.rb +2 -2
  53. metadata +26 -11
  54. data/ext/polars/src/lazy/dsl.rs +0 -1775
  55. data/ext/polars/src/lazy/mod.rs +0 -5
  56. data/ext/polars/src/lazy/utils.rs +0 -13
  57. data/ext/polars/src/list_construction.rs +0 -100
  58. /data/ext/polars/src/{numo.rs → series/export.rs} +0 -0
  59. /data/ext/polars/src/{set.rs → series/set_at_idx.rs} +0 -0
@@ -8,6 +8,14 @@ pub(crate) fn series() -> RClass {
8
8
  *memoize!(RClass: polars().const_get("Series").unwrap())
9
9
  }
10
10
 
11
+ pub(crate) fn utils() -> RModule {
12
+ *memoize!(RModule: polars().const_get("Utils").unwrap())
13
+ }
14
+
11
15
  pub(crate) fn date() -> RClass {
12
16
  *memoize!(RClass: class::object().const_get("Date").unwrap())
13
17
  }
18
+
19
+ pub(crate) fn datetime() -> RClass {
20
+ *memoize!(RClass: class::object().const_get("DateTime").unwrap())
21
+ }
@@ -0,0 +1,83 @@
1
+ use crate::error::RbPolarsErr;
2
+ use crate::prelude::*;
3
+ use crate::{RbResult, RbSeries, RbValueError};
4
+ use magnus::{IntoValue, Value};
5
+
6
+ impl RbSeries {
7
+ pub fn arg_max(&self) -> Option<usize> {
8
+ self.series.borrow().arg_max()
9
+ }
10
+
11
+ pub fn arg_min(&self) -> Option<usize> {
12
+ self.series.borrow().arg_min()
13
+ }
14
+
15
+ pub fn max(&self) -> RbResult<Value> {
16
+ Ok(Wrap(
17
+ self.series
18
+ .borrow()
19
+ .max_as_series()
20
+ .get(0)
21
+ .map_err(RbPolarsErr::from)?,
22
+ )
23
+ .into_value())
24
+ }
25
+
26
+ pub fn mean(&self) -> Option<f64> {
27
+ match self.series.borrow().dtype() {
28
+ DataType::Boolean => {
29
+ let s = self.series.borrow().cast(&DataType::UInt8).unwrap();
30
+ s.mean()
31
+ }
32
+ _ => self.series.borrow().mean(),
33
+ }
34
+ }
35
+
36
+ pub fn median(&self) -> Option<f64> {
37
+ match self.series.borrow().dtype() {
38
+ DataType::Boolean => {
39
+ let s = self.series.borrow().cast(&DataType::UInt8).unwrap();
40
+ s.median()
41
+ }
42
+ _ => self.series.borrow().median(),
43
+ }
44
+ }
45
+
46
+ pub fn min(&self) -> RbResult<Value> {
47
+ Ok(Wrap(
48
+ self.series
49
+ .borrow()
50
+ .min_as_series()
51
+ .get(0)
52
+ .map_err(RbPolarsErr::from)?,
53
+ )
54
+ .into_value())
55
+ }
56
+
57
+ pub fn quantile(
58
+ &self,
59
+ quantile: f64,
60
+ interpolation: Wrap<QuantileInterpolOptions>,
61
+ ) -> RbResult<Value> {
62
+ Ok(Wrap(
63
+ self.series
64
+ .borrow()
65
+ .quantile_as_series(quantile, interpolation.0)
66
+ .map_err(|_| RbValueError::new_err("invalid quantile".into()))?
67
+ .get(0)
68
+ .unwrap_or(AnyValue::Null),
69
+ )
70
+ .into_value())
71
+ }
72
+
73
+ pub fn sum(&self) -> RbResult<Value> {
74
+ Ok(Wrap(
75
+ self.series
76
+ .borrow()
77
+ .sum_as_series()
78
+ .get(0)
79
+ .map_err(RbPolarsErr::from)?,
80
+ )
81
+ .into_value())
82
+ }
83
+ }
@@ -0,0 +1,88 @@
1
+ use crate::{RbResult, RbSeries};
2
+
3
+ impl RbSeries {
4
+ pub fn add(&self, other: &RbSeries) -> Self {
5
+ (&*self.series.borrow() + &*other.series.borrow()).into()
6
+ }
7
+
8
+ pub fn sub(&self, other: &RbSeries) -> Self {
9
+ (&*self.series.borrow() - &*other.series.borrow()).into()
10
+ }
11
+
12
+ pub fn mul(&self, other: &RbSeries) -> Self {
13
+ (&*self.series.borrow() * &*other.series.borrow()).into()
14
+ }
15
+
16
+ pub fn div(&self, other: &RbSeries) -> Self {
17
+ (&*self.series.borrow() / &*other.series.borrow()).into()
18
+ }
19
+
20
+ pub fn rem(&self, other: &RbSeries) -> Self {
21
+ (&*self.series.borrow() % &*other.series.borrow()).into()
22
+ }
23
+ }
24
+
25
+ macro_rules! impl_arithmetic {
26
+ ($name:ident, $type:ty, $operand:tt) => {
27
+ impl RbSeries {
28
+ pub fn $name(&self, other: $type) -> RbResult<Self> {
29
+ Ok(RbSeries::new(&*self.series.borrow() $operand other))
30
+ }
31
+ }
32
+ };
33
+ }
34
+
35
+ impl_arithmetic!(add_u8, u8, +);
36
+ impl_arithmetic!(add_u16, u16, +);
37
+ impl_arithmetic!(add_u32, u32, +);
38
+ impl_arithmetic!(add_u64, u64, +);
39
+ impl_arithmetic!(add_i8, i8, +);
40
+ impl_arithmetic!(add_i16, i16, +);
41
+ impl_arithmetic!(add_i32, i32, +);
42
+ impl_arithmetic!(add_i64, i64, +);
43
+ impl_arithmetic!(add_datetime, i64, +);
44
+ impl_arithmetic!(add_duration, i64, +);
45
+ impl_arithmetic!(add_f32, f32, +);
46
+ impl_arithmetic!(add_f64, f64, +);
47
+ impl_arithmetic!(sub_u8, u8, -);
48
+ impl_arithmetic!(sub_u16, u16, -);
49
+ impl_arithmetic!(sub_u32, u32, -);
50
+ impl_arithmetic!(sub_u64, u64, -);
51
+ impl_arithmetic!(sub_i8, i8, -);
52
+ impl_arithmetic!(sub_i16, i16, -);
53
+ impl_arithmetic!(sub_i32, i32, -);
54
+ impl_arithmetic!(sub_i64, i64, -);
55
+ impl_arithmetic!(sub_datetime, i64, -);
56
+ impl_arithmetic!(sub_duration, i64, -);
57
+ impl_arithmetic!(sub_f32, f32, -);
58
+ impl_arithmetic!(sub_f64, f64, -);
59
+ impl_arithmetic!(div_u8, u8, /);
60
+ impl_arithmetic!(div_u16, u16, /);
61
+ impl_arithmetic!(div_u32, u32, /);
62
+ impl_arithmetic!(div_u64, u64, /);
63
+ impl_arithmetic!(div_i8, i8, /);
64
+ impl_arithmetic!(div_i16, i16, /);
65
+ impl_arithmetic!(div_i32, i32, /);
66
+ impl_arithmetic!(div_i64, i64, /);
67
+ impl_arithmetic!(div_f32, f32, /);
68
+ impl_arithmetic!(div_f64, f64, /);
69
+ impl_arithmetic!(mul_u8, u8, *);
70
+ impl_arithmetic!(mul_u16, u16, *);
71
+ impl_arithmetic!(mul_u32, u32, *);
72
+ impl_arithmetic!(mul_u64, u64, *);
73
+ impl_arithmetic!(mul_i8, i8, *);
74
+ impl_arithmetic!(mul_i16, i16, *);
75
+ impl_arithmetic!(mul_i32, i32, *);
76
+ impl_arithmetic!(mul_i64, i64, *);
77
+ impl_arithmetic!(mul_f32, f32, *);
78
+ impl_arithmetic!(mul_f64, f64, *);
79
+ impl_arithmetic!(rem_u8, u8, %);
80
+ impl_arithmetic!(rem_u16, u16, %);
81
+ impl_arithmetic!(rem_u32, u32, %);
82
+ impl_arithmetic!(rem_u64, u64, %);
83
+ impl_arithmetic!(rem_i8, i8, %);
84
+ impl_arithmetic!(rem_i16, i16, %);
85
+ impl_arithmetic!(rem_i32, i32, %);
86
+ impl_arithmetic!(rem_i64, i64, %);
87
+ impl_arithmetic!(rem_f32, f32, %);
88
+ impl_arithmetic!(rem_f64, f64, %);
@@ -0,0 +1,251 @@
1
+ use crate::error::RbPolarsErr;
2
+ use crate::prelude::*;
3
+ use crate::{RbResult, RbSeries};
4
+
5
+ impl RbSeries {
6
+ pub fn eq(&self, rhs: &RbSeries) -> RbResult<Self> {
7
+ let s = self
8
+ .series
9
+ .borrow()
10
+ .equal(&*rhs.series.borrow())
11
+ .map_err(RbPolarsErr::from)?;
12
+ Ok(Self::new(s.into_series()))
13
+ }
14
+
15
+ pub fn neq(&self, rhs: &RbSeries) -> RbResult<Self> {
16
+ let s = self
17
+ .series
18
+ .borrow()
19
+ .not_equal(&*rhs.series.borrow())
20
+ .map_err(RbPolarsErr::from)?;
21
+ Ok(Self::new(s.into_series()))
22
+ }
23
+
24
+ pub fn gt(&self, rhs: &RbSeries) -> RbResult<Self> {
25
+ let s = self
26
+ .series
27
+ .borrow()
28
+ .gt(&*rhs.series.borrow())
29
+ .map_err(RbPolarsErr::from)?;
30
+ Ok(Self::new(s.into_series()))
31
+ }
32
+
33
+ pub fn gt_eq(&self, rhs: &RbSeries) -> RbResult<Self> {
34
+ let s = self
35
+ .series
36
+ .borrow()
37
+ .gt_eq(&*rhs.series.borrow())
38
+ .map_err(RbPolarsErr::from)?;
39
+ Ok(Self::new(s.into_series()))
40
+ }
41
+
42
+ pub fn lt(&self, rhs: &RbSeries) -> RbResult<Self> {
43
+ let s = self
44
+ .series
45
+ .borrow()
46
+ .lt(&*rhs.series.borrow())
47
+ .map_err(RbPolarsErr::from)?;
48
+ Ok(Self::new(s.into_series()))
49
+ }
50
+
51
+ pub fn lt_eq(&self, rhs: &RbSeries) -> RbResult<Self> {
52
+ let s = self
53
+ .series
54
+ .borrow()
55
+ .lt_eq(&*rhs.series.borrow())
56
+ .map_err(RbPolarsErr::from)?;
57
+ Ok(Self::new(s.into_series()))
58
+ }
59
+ }
60
+
61
+ macro_rules! impl_eq_num {
62
+ ($name:ident, $type:ty) => {
63
+ impl RbSeries {
64
+ pub fn $name(&self, rhs: $type) -> RbResult<Self> {
65
+ let s = self.series.borrow().equal(rhs).map_err(RbPolarsErr::from)?;
66
+ Ok(RbSeries::new(s.into_series()))
67
+ }
68
+ }
69
+ };
70
+ }
71
+
72
+ impl_eq_num!(eq_u8, u8);
73
+ impl_eq_num!(eq_u16, u16);
74
+ impl_eq_num!(eq_u32, u32);
75
+ impl_eq_num!(eq_u64, u64);
76
+ impl_eq_num!(eq_i8, i8);
77
+ impl_eq_num!(eq_i16, i16);
78
+ impl_eq_num!(eq_i32, i32);
79
+ impl_eq_num!(eq_i64, i64);
80
+ impl_eq_num!(eq_f32, f32);
81
+ impl_eq_num!(eq_f64, f64);
82
+
83
+ macro_rules! impl_neq_num {
84
+ ($name:ident, $type:ty) => {
85
+ impl RbSeries {
86
+ pub fn $name(&self, rhs: $type) -> RbResult<Self> {
87
+ let s = self
88
+ .series
89
+ .borrow()
90
+ .not_equal(rhs)
91
+ .map_err(RbPolarsErr::from)?;
92
+ Ok(RbSeries::new(s.into_series()))
93
+ }
94
+ }
95
+ };
96
+ }
97
+
98
+ impl_neq_num!(neq_u8, u8);
99
+ impl_neq_num!(neq_u16, u16);
100
+ impl_neq_num!(neq_u32, u32);
101
+ impl_neq_num!(neq_u64, u64);
102
+ impl_neq_num!(neq_i8, i8);
103
+ impl_neq_num!(neq_i16, i16);
104
+ impl_neq_num!(neq_i32, i32);
105
+ impl_neq_num!(neq_i64, i64);
106
+ impl_neq_num!(neq_f32, f32);
107
+ impl_neq_num!(neq_f64, f64);
108
+
109
+ macro_rules! impl_gt_num {
110
+ ($name:ident, $type:ty) => {
111
+ impl RbSeries {
112
+ pub fn $name(&self, rhs: $type) -> RbResult<Self> {
113
+ let s = self.series.borrow().gt(rhs).map_err(RbPolarsErr::from)?;
114
+ Ok(RbSeries::new(s.into_series()))
115
+ }
116
+ }
117
+ };
118
+ }
119
+
120
+ impl_gt_num!(gt_u8, u8);
121
+ impl_gt_num!(gt_u16, u16);
122
+ impl_gt_num!(gt_u32, u32);
123
+ impl_gt_num!(gt_u64, u64);
124
+ impl_gt_num!(gt_i8, i8);
125
+ impl_gt_num!(gt_i16, i16);
126
+ impl_gt_num!(gt_i32, i32);
127
+ impl_gt_num!(gt_i64, i64);
128
+ impl_gt_num!(gt_f32, f32);
129
+ impl_gt_num!(gt_f64, f64);
130
+
131
+ macro_rules! impl_gt_eq_num {
132
+ ($name:ident, $type:ty) => {
133
+ impl RbSeries {
134
+ pub fn $name(&self, rhs: $type) -> RbResult<Self> {
135
+ let s = self.series.borrow().gt_eq(rhs).map_err(RbPolarsErr::from)?;
136
+ Ok(RbSeries::new(s.into_series()))
137
+ }
138
+ }
139
+ };
140
+ }
141
+
142
+ impl_gt_eq_num!(gt_eq_u8, u8);
143
+ impl_gt_eq_num!(gt_eq_u16, u16);
144
+ impl_gt_eq_num!(gt_eq_u32, u32);
145
+ impl_gt_eq_num!(gt_eq_u64, u64);
146
+ impl_gt_eq_num!(gt_eq_i8, i8);
147
+ impl_gt_eq_num!(gt_eq_i16, i16);
148
+ impl_gt_eq_num!(gt_eq_i32, i32);
149
+ impl_gt_eq_num!(gt_eq_i64, i64);
150
+ impl_gt_eq_num!(gt_eq_f32, f32);
151
+ impl_gt_eq_num!(gt_eq_f64, f64);
152
+
153
+ macro_rules! impl_lt_num {
154
+ ($name:ident, $type:ty) => {
155
+ impl RbSeries {
156
+ pub fn $name(&self, rhs: $type) -> RbResult<RbSeries> {
157
+ let s = self.series.borrow().lt(rhs).map_err(RbPolarsErr::from)?;
158
+ Ok(RbSeries::new(s.into_series()))
159
+ }
160
+ }
161
+ };
162
+ }
163
+
164
+ impl_lt_num!(lt_u8, u8);
165
+ impl_lt_num!(lt_u16, u16);
166
+ impl_lt_num!(lt_u32, u32);
167
+ impl_lt_num!(lt_u64, u64);
168
+ impl_lt_num!(lt_i8, i8);
169
+ impl_lt_num!(lt_i16, i16);
170
+ impl_lt_num!(lt_i32, i32);
171
+ impl_lt_num!(lt_i64, i64);
172
+ impl_lt_num!(lt_f32, f32);
173
+ impl_lt_num!(lt_f64, f64);
174
+
175
+ macro_rules! impl_lt_eq_num {
176
+ ($name:ident, $type:ty) => {
177
+ impl RbSeries {
178
+ pub fn $name(&self, rhs: $type) -> RbResult<Self> {
179
+ let s = self.series.borrow().lt_eq(rhs).map_err(RbPolarsErr::from)?;
180
+ Ok(RbSeries::new(s.into_series()))
181
+ }
182
+ }
183
+ };
184
+ }
185
+
186
+ impl_lt_eq_num!(lt_eq_u8, u8);
187
+ impl_lt_eq_num!(lt_eq_u16, u16);
188
+ impl_lt_eq_num!(lt_eq_u32, u32);
189
+ impl_lt_eq_num!(lt_eq_u64, u64);
190
+ impl_lt_eq_num!(lt_eq_i8, i8);
191
+ impl_lt_eq_num!(lt_eq_i16, i16);
192
+ impl_lt_eq_num!(lt_eq_i32, i32);
193
+ impl_lt_eq_num!(lt_eq_i64, i64);
194
+ impl_lt_eq_num!(lt_eq_f32, f32);
195
+ impl_lt_eq_num!(lt_eq_f64, f64);
196
+
197
+ impl RbSeries {
198
+ pub fn eq_str(&self, rhs: String) -> RbResult<Self> {
199
+ let s = self
200
+ .series
201
+ .borrow()
202
+ .equal(rhs.as_str())
203
+ .map_err(RbPolarsErr::from)?;
204
+ Ok(RbSeries::new(s.into_series()))
205
+ }
206
+
207
+ pub fn neq_str(&self, rhs: String) -> RbResult<Self> {
208
+ let s = self
209
+ .series
210
+ .borrow()
211
+ .not_equal(rhs.as_str())
212
+ .map_err(RbPolarsErr::from)?;
213
+ Ok(RbSeries::new(s.into_series()))
214
+ }
215
+
216
+ pub fn gt_str(&self, rhs: String) -> RbResult<Self> {
217
+ let s = self
218
+ .series
219
+ .borrow()
220
+ .gt(rhs.as_str())
221
+ .map_err(RbPolarsErr::from)?;
222
+ Ok(RbSeries::new(s.into_series()))
223
+ }
224
+
225
+ pub fn gt_eq_str(&self, rhs: String) -> RbResult<Self> {
226
+ let s = self
227
+ .series
228
+ .borrow()
229
+ .gt_eq(rhs.as_str())
230
+ .map_err(RbPolarsErr::from)?;
231
+ Ok(RbSeries::new(s.into_series()))
232
+ }
233
+
234
+ pub fn lt_str(&self, rhs: String) -> RbResult<Self> {
235
+ let s = self
236
+ .series
237
+ .borrow()
238
+ .lt(rhs.as_str())
239
+ .map_err(RbPolarsErr::from)?;
240
+ Ok(RbSeries::new(s.into_series()))
241
+ }
242
+
243
+ pub fn lt_eq_str(&self, rhs: String) -> RbResult<Self> {
244
+ let s = self
245
+ .series
246
+ .borrow()
247
+ .lt_eq(rhs.as_str())
248
+ .map_err(RbPolarsErr::from)?;
249
+ Ok(RbSeries::new(s.into_series()))
250
+ }
251
+ }
@@ -0,0 +1,164 @@
1
+ use magnus::RArray;
2
+ use polars_core::prelude::*;
3
+
4
+ use crate::conversion::{slice_extract_wrapped, Wrap};
5
+ use crate::prelude::ObjectValue;
6
+ use crate::series::to_series_collection;
7
+ use crate::{RbPolarsErr, RbResult, RbSeries};
8
+
9
+ impl RbSeries {
10
+ pub fn new_opt_bool(name: String, obj: RArray, strict: bool) -> RbResult<RbSeries> {
11
+ let len = obj.len();
12
+ let mut builder = BooleanChunkedBuilder::new(&name, len);
13
+
14
+ unsafe {
15
+ for item in obj.as_slice().iter() {
16
+ if item.is_nil() {
17
+ builder.append_null()
18
+ } else {
19
+ match item.try_convert::<bool>() {
20
+ Ok(val) => builder.append_value(val),
21
+ Err(e) => {
22
+ if strict {
23
+ return Err(e);
24
+ }
25
+ builder.append_null()
26
+ }
27
+ }
28
+ }
29
+ }
30
+ }
31
+ let ca = builder.finish();
32
+
33
+ let s = ca.into_series();
34
+ Ok(RbSeries::new(s))
35
+ }
36
+ }
37
+
38
+ fn new_primitive<T>(name: &str, obj: RArray, strict: bool) -> RbResult<RbSeries>
39
+ where
40
+ T: PolarsNumericType,
41
+ ChunkedArray<T>: IntoSeries,
42
+ T::Native: magnus::TryConvert,
43
+ {
44
+ let len = obj.len();
45
+ let mut builder = PrimitiveChunkedBuilder::<T>::new(name, len);
46
+
47
+ unsafe {
48
+ for item in obj.as_slice().iter() {
49
+ if item.is_nil() {
50
+ builder.append_null()
51
+ } else {
52
+ match item.try_convert::<T::Native>() {
53
+ Ok(val) => builder.append_value(val),
54
+ Err(e) => {
55
+ if strict {
56
+ return Err(e);
57
+ }
58
+ builder.append_null()
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
64
+ let ca = builder.finish();
65
+
66
+ let s = ca.into_series();
67
+ Ok(RbSeries::new(s))
68
+ }
69
+
70
+ // Init with lists that can contain Nones
71
+ macro_rules! init_method_opt {
72
+ ($name:ident, $type:ty, $native: ty) => {
73
+ impl RbSeries {
74
+ pub fn $name(name: String, obj: RArray, strict: bool) -> RbResult<Self> {
75
+ new_primitive::<$type>(&name, obj, strict)
76
+ }
77
+ }
78
+ };
79
+ }
80
+
81
+ init_method_opt!(new_opt_u8, UInt8Type, u8);
82
+ init_method_opt!(new_opt_u16, UInt16Type, u16);
83
+ init_method_opt!(new_opt_u32, UInt32Type, u32);
84
+ init_method_opt!(new_opt_u64, UInt64Type, u64);
85
+ init_method_opt!(new_opt_i8, Int8Type, i8);
86
+ init_method_opt!(new_opt_i16, Int16Type, i16);
87
+ init_method_opt!(new_opt_i32, Int32Type, i32);
88
+ init_method_opt!(new_opt_i64, Int64Type, i64);
89
+ init_method_opt!(new_opt_f32, Float32Type, f32);
90
+ init_method_opt!(new_opt_f64, Float64Type, f64);
91
+
92
+ fn vec_wrap_any_value<'s>(arr: RArray) -> RbResult<Vec<Wrap<AnyValue<'s>>>> {
93
+ let mut val = Vec::with_capacity(arr.len());
94
+ for v in arr.each() {
95
+ val.push(v?.try_convert()?);
96
+ }
97
+ Ok(val)
98
+ }
99
+
100
+ impl RbSeries {
101
+ pub fn new_from_anyvalues(name: String, val: RArray, strict: bool) -> RbResult<Self> {
102
+ let val = vec_wrap_any_value(val)?;
103
+ let avs = slice_extract_wrapped(&val);
104
+ // from anyvalues is fallible
105
+ let s = Series::from_any_values(&name, avs, strict).map_err(RbPolarsErr::from)?;
106
+ Ok(s.into())
107
+ }
108
+
109
+ pub fn new_str(name: String, val: Wrap<Utf8Chunked>, _strict: bool) -> Self {
110
+ let mut s = val.0.into_series();
111
+ s.rename(&name);
112
+ RbSeries::new(s)
113
+ }
114
+
115
+ pub fn new_binary(name: String, val: Wrap<BinaryChunked>, _strict: bool) -> Self {
116
+ let mut s = val.0.into_series();
117
+ s.rename(&name);
118
+ RbSeries::new(s)
119
+ }
120
+
121
+ pub fn new_null(name: String, val: RArray, _strict: bool) -> RbResult<Self> {
122
+ let s = Series::new_null(&name, val.len());
123
+ Ok(s.into())
124
+ }
125
+
126
+ pub fn new_object(name: String, val: RArray, _strict: bool) -> RbResult<Self> {
127
+ let val = val
128
+ .each()
129
+ .map(|v| v.map(ObjectValue::from))
130
+ .collect::<RbResult<Vec<ObjectValue>>>()?;
131
+ let s = ObjectChunked::<ObjectValue>::new_from_vec(&name, val).into_series();
132
+ Ok(s.into())
133
+ }
134
+
135
+ pub fn new_series_list(name: String, val: RArray, _strict: bool) -> RbResult<Self> {
136
+ let series_vec = to_series_collection(val)?;
137
+ Ok(Series::new(&name, &series_vec).into())
138
+ }
139
+
140
+ pub fn new_decimal(name: String, val: RArray, strict: bool) -> RbResult<Self> {
141
+ let val = vec_wrap_any_value(val)?;
142
+ // TODO: do we have to respect 'strict' here? it's possible if we want to
143
+ let avs = slice_extract_wrapped(&val);
144
+ // create a fake dtype with a placeholder "none" scale, to be inferred later
145
+ let dtype = DataType::Decimal(None, None);
146
+ let s = Series::from_any_values_and_dtype(&name, avs, &dtype, strict)
147
+ .map_err(RbPolarsErr::from)?;
148
+ Ok(s.into())
149
+ }
150
+
151
+ pub fn repeat(
152
+ name: String,
153
+ val: Wrap<AnyValue>,
154
+ n: usize,
155
+ dtype: Wrap<DataType>,
156
+ ) -> RbResult<Self> {
157
+ let av = val.0;
158
+ Ok(Series::new(&name, &[av])
159
+ .cast(&dtype.0)
160
+ .map_err(RbPolarsErr::from)?
161
+ .new_from_index(0, n)
162
+ .into())
163
+ }
164
+ }