polars-df 0.13.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/Cargo.lock +1368 -319
  4. data/LICENSE.txt +1 -0
  5. data/README.md +1 -2
  6. data/ext/polars/Cargo.toml +15 -6
  7. data/ext/polars/src/batched_csv.rs +10 -13
  8. data/ext/polars/src/conversion/any_value.rs +37 -21
  9. data/ext/polars/src/conversion/chunked_array.rs +3 -3
  10. data/ext/polars/src/conversion/mod.rs +159 -46
  11. data/ext/polars/src/dataframe/construction.rs +4 -7
  12. data/ext/polars/src/dataframe/export.rs +9 -2
  13. data/ext/polars/src/dataframe/general.rs +22 -16
  14. data/ext/polars/src/dataframe/io.rs +78 -174
  15. data/ext/polars/src/dataframe/mod.rs +1 -0
  16. data/ext/polars/src/dataframe/serde.rs +15 -0
  17. data/ext/polars/src/error.rs +31 -48
  18. data/ext/polars/src/exceptions.rs +24 -0
  19. data/ext/polars/src/expr/binary.rs +4 -42
  20. data/ext/polars/src/expr/datetime.rs +16 -7
  21. data/ext/polars/src/expr/general.rs +14 -23
  22. data/ext/polars/src/expr/list.rs +18 -11
  23. data/ext/polars/src/expr/name.rs +3 -2
  24. data/ext/polars/src/expr/rolling.rs +6 -7
  25. data/ext/polars/src/expr/string.rs +17 -37
  26. data/ext/polars/src/file.rs +59 -22
  27. data/ext/polars/src/functions/business.rs +15 -0
  28. data/ext/polars/src/functions/io.rs +6 -6
  29. data/ext/polars/src/functions/lazy.rs +17 -8
  30. data/ext/polars/src/functions/mod.rs +1 -0
  31. data/ext/polars/src/functions/range.rs +4 -2
  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 +877 -0
  36. data/ext/polars/src/lazyframe/mod.rs +3 -825
  37. data/ext/polars/src/lazyframe/serde.rs +31 -0
  38. data/ext/polars/src/lib.rs +44 -13
  39. data/ext/polars/src/map/dataframe.rs +46 -14
  40. data/ext/polars/src/map/lazy.rs +65 -4
  41. data/ext/polars/src/map/mod.rs +17 -16
  42. data/ext/polars/src/map/series.rs +106 -64
  43. data/ext/polars/src/on_startup.rs +2 -2
  44. data/ext/polars/src/series/aggregation.rs +1 -5
  45. data/ext/polars/src/series/arithmetic.rs +10 -10
  46. data/ext/polars/src/series/construction.rs +52 -25
  47. data/ext/polars/src/series/export.rs +1 -1
  48. data/ext/polars/src/series/general.rs +643 -0
  49. data/ext/polars/src/series/import.rs +55 -0
  50. data/ext/polars/src/series/mod.rs +11 -638
  51. data/ext/polars/src/series/scatter.rs +2 -2
  52. data/ext/polars/src/utils.rs +0 -20
  53. data/lib/polars/batched_csv_reader.rb +0 -2
  54. data/lib/polars/binary_expr.rb +133 -9
  55. data/lib/polars/binary_name_space.rb +101 -6
  56. data/lib/polars/config.rb +4 -0
  57. data/lib/polars/data_frame.rb +285 -62
  58. data/lib/polars/data_type_group.rb +28 -0
  59. data/lib/polars/data_types.rb +2 -0
  60. data/lib/polars/date_time_expr.rb +244 -0
  61. data/lib/polars/date_time_name_space.rb +87 -0
  62. data/lib/polars/expr.rb +109 -8
  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 +88 -10
  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/ipc.rb +14 -12
  71. data/lib/polars/io/ndjson.rb +10 -0
  72. data/lib/polars/io/parquet.rb +168 -111
  73. data/lib/polars/lazy_frame.rb +649 -15
  74. data/lib/polars/list_name_space.rb +169 -0
  75. data/lib/polars/selectors.rb +1144 -0
  76. data/lib/polars/series.rb +470 -40
  77. data/lib/polars/string_cache.rb +27 -1
  78. data/lib/polars/string_expr.rb +0 -1
  79. data/lib/polars/string_name_space.rb +73 -3
  80. data/lib/polars/struct_name_space.rb +31 -7
  81. data/lib/polars/utils/various.rb +5 -1
  82. data/lib/polars/utils.rb +45 -10
  83. data/lib/polars/version.rb +1 -1
  84. data/lib/polars.rb +2 -1
  85. metadata +14 -4
  86. data/lib/polars/functions.rb +0 -57
@@ -1,16 +1,16 @@
1
- use magnus::{prelude::*, RArray};
1
+ use magnus::{prelude::*, RArray, RString};
2
2
  use polars_core::prelude::*;
3
3
 
4
4
  use crate::any_value::rb_object_to_any_value;
5
5
  use crate::conversion::{slice_extract_wrapped, vec_extract_wrapped, Wrap};
6
6
  use crate::prelude::ObjectValue;
7
- use crate::series::to_series_collection;
7
+ use crate::series::to_series;
8
8
  use crate::{RbPolarsErr, RbResult, RbSeries, RbTypeError, RbValueError};
9
9
 
10
10
  impl RbSeries {
11
11
  pub fn new_opt_bool(name: String, obj: RArray, strict: bool) -> RbResult<RbSeries> {
12
12
  let len = obj.len();
13
- let mut builder = BooleanChunkedBuilder::new(&name, len);
13
+ let mut builder = BooleanChunkedBuilder::new(name.into(), len);
14
14
 
15
15
  unsafe {
16
16
  for item in obj.as_slice().iter() {
@@ -43,7 +43,7 @@ where
43
43
  T::Native: magnus::TryConvert,
44
44
  {
45
45
  let len = values.len();
46
- let mut builder = PrimitiveChunkedBuilder::<T>::new(name, len);
46
+ let mut builder = PrimitiveChunkedBuilder::<T>::new(name.into(), len);
47
47
 
48
48
  for res in values.into_iter() {
49
49
  let value = res;
@@ -96,7 +96,7 @@ impl RbSeries {
96
96
  // from anyvalues is fallible
97
97
  let result = any_values_result.and_then(|avs| {
98
98
  let avs = slice_extract_wrapped(&avs);
99
- let s = Series::from_any_values(&name, avs, strict).map_err(|e| {
99
+ let s = Series::from_any_values(name.clone().into(), avs, strict).map_err(|e| {
100
100
  RbTypeError::new_err(format!(
101
101
  "{e}\n\nHint: Try setting `strict: false` to allow passing data with mixed types."
102
102
  ))
@@ -122,44 +122,71 @@ impl RbSeries {
122
122
  .into_iter()
123
123
  .map(|v| rb_object_to_any_value(v, strict))
124
124
  .collect::<RbResult<Vec<AnyValue>>>()?;
125
- let s = Series::from_any_values_and_dtype(&name, any_values.as_slice(), &dtype.0, strict)
126
- .map_err(|e| {
127
- RbTypeError::new_err(format!(
125
+ let s =
126
+ Series::from_any_values_and_dtype(name.into(), any_values.as_slice(), &dtype.0, strict)
127
+ .map_err(|e| {
128
+ RbTypeError::new_err(format!(
128
129
  "{e}\n\nHint: Try setting `strict: false` to allow passing data with mixed types."
129
130
  ))
130
- })?;
131
+ })?;
131
132
  Ok(s.into())
132
133
  }
133
134
 
134
- pub fn new_str(name: String, val: Wrap<StringChunked>, _strict: bool) -> Self {
135
- let mut s = val.0.into_series();
136
- s.rename(&name);
137
- RbSeries::new(s)
138
- }
135
+ pub fn new_str(name: String, values: RArray, _strict: bool) -> RbResult<Self> {
136
+ let len = values.len();
137
+ let mut builder = StringChunkedBuilder::new(name.into(), len);
138
+
139
+ for res in values.into_iter() {
140
+ let value = res;
141
+ if value.is_nil() {
142
+ builder.append_null()
143
+ } else {
144
+ let v = String::try_convert(value)?;
145
+ builder.append_value(v)
146
+ }
147
+ }
139
148
 
140
- pub fn new_binary(name: String, val: Wrap<BinaryChunked>, _strict: bool) -> Self {
141
- let mut s = val.0.into_series();
142
- s.rename(&name);
143
- RbSeries::new(s)
149
+ let ca = builder.finish();
150
+ let s = ca.into_series();
151
+ Ok(s.into())
144
152
  }
145
153
 
146
- pub fn new_null(name: String, val: RArray, _strict: bool) -> RbResult<Self> {
147
- let s = Series::new_null(&name, val.len());
154
+ pub fn new_binary(name: String, values: RArray, _strict: bool) -> RbResult<Self> {
155
+ let len = values.len();
156
+ let mut builder = BinaryChunkedBuilder::new(name.into(), len);
157
+
158
+ for res in values.into_iter() {
159
+ let value = res;
160
+ if value.is_nil() {
161
+ builder.append_null()
162
+ } else {
163
+ let v = RString::try_convert(value)?;
164
+ builder.append_value(unsafe { v.as_slice() })
165
+ }
166
+ }
167
+
168
+ let ca = builder.finish();
169
+ let s = ca.into_series();
148
170
  Ok(s.into())
149
171
  }
150
172
 
173
+ pub fn new_null(name: String, values: RArray, _strict: bool) -> RbResult<Self> {
174
+ let len = values.len();
175
+ Ok(Series::new_null(name.into(), len).into())
176
+ }
177
+
151
178
  pub fn new_object(name: String, val: RArray, _strict: bool) -> RbResult<Self> {
152
179
  let val = val
153
180
  .into_iter()
154
181
  .map(ObjectValue::from)
155
182
  .collect::<Vec<ObjectValue>>();
156
- let s = ObjectChunked::<ObjectValue>::new_from_vec(&name, val).into_series();
183
+ let s = ObjectChunked::<ObjectValue>::new_from_vec(name.into(), val).into_series();
157
184
  Ok(s.into())
158
185
  }
159
186
 
160
187
  pub fn new_series_list(name: String, val: RArray, _strict: bool) -> RbResult<Self> {
161
- let series_vec = to_series_collection(val)?;
162
- Ok(Series::new(&name, &series_vec).into())
188
+ let series_vec = to_series(val)?;
189
+ Ok(Series::new(name.into(), &series_vec).into())
163
190
  }
164
191
 
165
192
  pub fn new_array(
@@ -171,7 +198,7 @@ impl RbSeries {
171
198
  ) -> RbResult<Self> {
172
199
  let val = vec_wrap_any_value(val)?;
173
200
  let val = vec_extract_wrapped(val);
174
- let out = Series::new(&name, &val);
201
+ let out = Series::new(name.into(), &val);
175
202
  match out.dtype() {
176
203
  DataType::List(list_inner) => {
177
204
  let out = out
@@ -199,7 +226,7 @@ impl RbSeries {
199
226
  dtype: Wrap<DataType>,
200
227
  ) -> RbResult<Self> {
201
228
  let av = val.0;
202
- Ok(Series::new(&name, &[av])
229
+ Ok(Series::new(name.into(), &[av])
203
230
  .cast(&dtype.0)
204
231
  .map_err(RbPolarsErr::from)?
205
232
  .new_from_index(0, n)
@@ -31,7 +31,7 @@ impl RbSeries {
31
31
  for i in 0..series.len() {
32
32
  let obj: Option<&ObjectValue> = series.get_object(i).map(|any| any.into());
33
33
  match obj {
34
- Some(val) => v.push(val.to_object()).unwrap(),
34
+ Some(val) => v.push(val.to_value()).unwrap(),
35
35
  None => v.push(qnil()).unwrap(),
36
36
  };
37
37
  }