polars-df 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/Cargo.lock +90 -48
- data/README.md +6 -6
- data/ext/polars/Cargo.toml +7 -5
- data/ext/polars/src/batched_csv.rs +53 -52
- data/ext/polars/src/conversion/mod.rs +13 -60
- data/ext/polars/src/dataframe/construction.rs +186 -0
- data/ext/polars/src/dataframe/export.rs +48 -0
- data/ext/polars/src/dataframe/general.rs +607 -0
- data/ext/polars/src/dataframe/io.rs +463 -0
- data/ext/polars/src/dataframe/mod.rs +26 -0
- data/ext/polars/src/expr/datetime.rs +6 -2
- data/ext/polars/src/expr/general.rs +28 -6
- data/ext/polars/src/expr/rolling.rs +185 -69
- data/ext/polars/src/expr/string.rs +9 -30
- data/ext/polars/src/functions/lazy.rs +2 -0
- data/ext/polars/src/functions/range.rs +74 -0
- data/ext/polars/src/interop/mod.rs +1 -0
- data/ext/polars/src/interop/numo/mod.rs +2 -0
- data/ext/polars/src/interop/numo/to_numo_df.rs +23 -0
- data/ext/polars/src/interop/numo/to_numo_series.rs +60 -0
- data/ext/polars/src/lazyframe/mod.rs +54 -38
- data/ext/polars/src/lib.rs +46 -21
- data/ext/polars/src/map/lazy.rs +5 -25
- data/ext/polars/src/map/series.rs +7 -1
- data/ext/polars/src/series/aggregation.rs +47 -30
- data/ext/polars/src/series/export.rs +131 -49
- data/ext/polars/src/series/mod.rs +1 -131
- data/lib/polars/batched_csv_reader.rb +9 -3
- data/lib/polars/convert.rb +6 -1
- data/lib/polars/data_frame.rb +83 -302
- data/lib/polars/date_time_expr.rb +1 -0
- data/lib/polars/date_time_name_space.rb +5 -1
- data/lib/polars/dynamic_group_by.rb +2 -2
- data/lib/polars/exceptions.rb +4 -0
- data/lib/polars/expr.rb +1134 -20
- data/lib/polars/functions/range/date_range.rb +92 -0
- data/lib/polars/functions/range/datetime_range.rb +149 -0
- data/lib/polars/functions/range/time_range.rb +141 -0
- data/lib/polars/group_by.rb +88 -23
- data/lib/polars/io/avro.rb +24 -0
- data/lib/polars/{io.rb → io/csv.rb} +296 -490
- data/lib/polars/io/database.rb +73 -0
- data/lib/polars/io/ipc.rb +247 -0
- data/lib/polars/io/json.rb +18 -0
- data/lib/polars/io/ndjson.rb +69 -0
- data/lib/polars/io/parquet.rb +226 -0
- data/lib/polars/lazy_frame.rb +23 -166
- data/lib/polars/lazy_group_by.rb +100 -3
- data/lib/polars/rolling_group_by.rb +2 -2
- data/lib/polars/series.rb +2 -2
- data/lib/polars/string_expr.rb +37 -36
- data/lib/polars/utils.rb +35 -1
- data/lib/polars/version.rb +1 -1
- data/lib/polars.rb +9 -1
- metadata +21 -5
- data/ext/polars/src/dataframe.rs +0 -1208
@@ -0,0 +1,607 @@
|
|
1
|
+
use either::Either;
|
2
|
+
use magnus::{prelude::*, typed_data::Obj, IntoValue, RArray, Value};
|
3
|
+
use polars::frame::NullStrategy;
|
4
|
+
use polars::prelude::pivot::{pivot, pivot_stable};
|
5
|
+
use polars::prelude::*;
|
6
|
+
|
7
|
+
use crate::conversion::*;
|
8
|
+
use crate::map::dataframe::{
|
9
|
+
apply_lambda_unknown, apply_lambda_with_bool_out_type, apply_lambda_with_primitive_out_type,
|
10
|
+
apply_lambda_with_utf8_out_type,
|
11
|
+
};
|
12
|
+
use crate::series::{to_rbseries_collection, to_series_collection};
|
13
|
+
use crate::{RbDataFrame, RbExpr, RbLazyFrame, RbPolarsErr, RbResult, RbSeries};
|
14
|
+
|
15
|
+
impl RbDataFrame {
|
16
|
+
pub fn init(columns: RArray) -> RbResult<Self> {
|
17
|
+
let mut cols = Vec::new();
|
18
|
+
for i in columns.each() {
|
19
|
+
cols.push(<&RbSeries>::try_convert(i?)?.series.borrow().clone());
|
20
|
+
}
|
21
|
+
let df = DataFrame::new(cols).map_err(RbPolarsErr::from)?;
|
22
|
+
Ok(RbDataFrame::new(df))
|
23
|
+
}
|
24
|
+
|
25
|
+
pub fn estimated_size(&self) -> usize {
|
26
|
+
self.df.borrow().estimated_size()
|
27
|
+
}
|
28
|
+
|
29
|
+
pub fn dtype_strings(&self) -> Vec<String> {
|
30
|
+
self.df
|
31
|
+
.borrow()
|
32
|
+
.get_columns()
|
33
|
+
.iter()
|
34
|
+
.map(|s| format!("{}", s.dtype()))
|
35
|
+
.collect()
|
36
|
+
}
|
37
|
+
|
38
|
+
pub fn add(&self, s: &RbSeries) -> RbResult<Self> {
|
39
|
+
let df = (&*self.df.borrow() + &*s.series.borrow()).map_err(RbPolarsErr::from)?;
|
40
|
+
Ok(df.into())
|
41
|
+
}
|
42
|
+
|
43
|
+
pub fn sub(&self, s: &RbSeries) -> RbResult<Self> {
|
44
|
+
let df = (&*self.df.borrow() - &*s.series.borrow()).map_err(RbPolarsErr::from)?;
|
45
|
+
Ok(df.into())
|
46
|
+
}
|
47
|
+
|
48
|
+
pub fn div(&self, s: &RbSeries) -> RbResult<Self> {
|
49
|
+
let df = (&*self.df.borrow() / &*s.series.borrow()).map_err(RbPolarsErr::from)?;
|
50
|
+
Ok(df.into())
|
51
|
+
}
|
52
|
+
|
53
|
+
pub fn mul(&self, s: &RbSeries) -> RbResult<Self> {
|
54
|
+
let df = (&*self.df.borrow() * &*s.series.borrow()).map_err(RbPolarsErr::from)?;
|
55
|
+
Ok(df.into())
|
56
|
+
}
|
57
|
+
|
58
|
+
pub fn rem(&self, s: &RbSeries) -> RbResult<Self> {
|
59
|
+
let df = (&*self.df.borrow() % &*s.series.borrow()).map_err(RbPolarsErr::from)?;
|
60
|
+
Ok(df.into())
|
61
|
+
}
|
62
|
+
|
63
|
+
pub fn add_df(&self, s: &Self) -> RbResult<Self> {
|
64
|
+
let df = (&*self.df.borrow() + &*s.df.borrow()).map_err(RbPolarsErr::from)?;
|
65
|
+
Ok(df.into())
|
66
|
+
}
|
67
|
+
|
68
|
+
pub fn sub_df(&self, s: &Self) -> RbResult<Self> {
|
69
|
+
let df = (&*self.df.borrow() - &*s.df.borrow()).map_err(RbPolarsErr::from)?;
|
70
|
+
Ok(df.into())
|
71
|
+
}
|
72
|
+
|
73
|
+
pub fn div_df(&self, s: &Self) -> RbResult<Self> {
|
74
|
+
let df = (&*self.df.borrow() / &*s.df.borrow()).map_err(RbPolarsErr::from)?;
|
75
|
+
Ok(df.into())
|
76
|
+
}
|
77
|
+
|
78
|
+
pub fn mul_df(&self, s: &Self) -> RbResult<Self> {
|
79
|
+
let df = (&*self.df.borrow() * &*s.df.borrow()).map_err(RbPolarsErr::from)?;
|
80
|
+
Ok(df.into())
|
81
|
+
}
|
82
|
+
|
83
|
+
pub fn rem_df(&self, s: &Self) -> RbResult<Self> {
|
84
|
+
let df = (&*self.df.borrow() % &*s.df.borrow()).map_err(RbPolarsErr::from)?;
|
85
|
+
Ok(df.into())
|
86
|
+
}
|
87
|
+
|
88
|
+
pub fn sample_n(
|
89
|
+
&self,
|
90
|
+
n: &RbSeries,
|
91
|
+
with_replacement: bool,
|
92
|
+
shuffle: bool,
|
93
|
+
seed: Option<u64>,
|
94
|
+
) -> RbResult<Self> {
|
95
|
+
let df = self
|
96
|
+
.df
|
97
|
+
.borrow()
|
98
|
+
.sample_n(&n.series.borrow(), with_replacement, shuffle, seed)
|
99
|
+
.map_err(RbPolarsErr::from)?;
|
100
|
+
Ok(df.into())
|
101
|
+
}
|
102
|
+
|
103
|
+
pub fn sample_frac(
|
104
|
+
&self,
|
105
|
+
frac: &RbSeries,
|
106
|
+
with_replacement: bool,
|
107
|
+
shuffle: bool,
|
108
|
+
seed: Option<u64>,
|
109
|
+
) -> RbResult<Self> {
|
110
|
+
let df = self
|
111
|
+
.df
|
112
|
+
.borrow()
|
113
|
+
.sample_frac(&frac.series.borrow(), with_replacement, shuffle, seed)
|
114
|
+
.map_err(RbPolarsErr::from)?;
|
115
|
+
Ok(df.into())
|
116
|
+
}
|
117
|
+
|
118
|
+
pub fn rechunk(&self) -> Self {
|
119
|
+
let mut df = self.df.borrow_mut().clone();
|
120
|
+
df.as_single_chunk_par();
|
121
|
+
df.into()
|
122
|
+
}
|
123
|
+
|
124
|
+
pub fn as_str(&self) -> String {
|
125
|
+
format!("{}", self.df.borrow())
|
126
|
+
}
|
127
|
+
|
128
|
+
pub fn get_columns(&self) -> RArray {
|
129
|
+
let cols = self.df.borrow().get_columns().to_vec();
|
130
|
+
to_rbseries_collection(cols)
|
131
|
+
}
|
132
|
+
|
133
|
+
pub fn columns(&self) -> Vec<String> {
|
134
|
+
self.df
|
135
|
+
.borrow()
|
136
|
+
.get_column_names()
|
137
|
+
.iter()
|
138
|
+
.map(|v| v.to_string())
|
139
|
+
.collect()
|
140
|
+
}
|
141
|
+
|
142
|
+
pub fn set_column_names(&self, names: Vec<String>) -> RbResult<()> {
|
143
|
+
self.df
|
144
|
+
.borrow_mut()
|
145
|
+
.set_column_names(&names)
|
146
|
+
.map_err(RbPolarsErr::from)?;
|
147
|
+
Ok(())
|
148
|
+
}
|
149
|
+
|
150
|
+
pub fn dtypes(&self) -> RArray {
|
151
|
+
RArray::from_iter(
|
152
|
+
self.df
|
153
|
+
.borrow()
|
154
|
+
.iter()
|
155
|
+
.map(|s| Wrap(s.dtype().clone()).into_value()),
|
156
|
+
)
|
157
|
+
}
|
158
|
+
|
159
|
+
pub fn n_chunks(&self) -> usize {
|
160
|
+
self.df.borrow().n_chunks()
|
161
|
+
}
|
162
|
+
|
163
|
+
pub fn shape(&self) -> (usize, usize) {
|
164
|
+
self.df.borrow().shape()
|
165
|
+
}
|
166
|
+
|
167
|
+
pub fn height(&self) -> usize {
|
168
|
+
self.df.borrow().height()
|
169
|
+
}
|
170
|
+
|
171
|
+
pub fn width(&self) -> usize {
|
172
|
+
self.df.borrow().width()
|
173
|
+
}
|
174
|
+
|
175
|
+
pub fn hstack(&self, columns: RArray) -> RbResult<Self> {
|
176
|
+
let columns = to_series_collection(columns)?;
|
177
|
+
let df = self
|
178
|
+
.df
|
179
|
+
.borrow()
|
180
|
+
.hstack(&columns)
|
181
|
+
.map_err(RbPolarsErr::from)?;
|
182
|
+
Ok(df.into())
|
183
|
+
}
|
184
|
+
|
185
|
+
pub fn hstack_mut(&self, columns: RArray) -> RbResult<()> {
|
186
|
+
let columns = to_series_collection(columns)?;
|
187
|
+
self.df
|
188
|
+
.borrow_mut()
|
189
|
+
.hstack_mut(&columns)
|
190
|
+
.map_err(RbPolarsErr::from)?;
|
191
|
+
Ok(())
|
192
|
+
}
|
193
|
+
|
194
|
+
pub fn vstack(&self, df: &RbDataFrame) -> RbResult<Self> {
|
195
|
+
let df = self
|
196
|
+
.df
|
197
|
+
.borrow()
|
198
|
+
.vstack(&df.df.borrow())
|
199
|
+
.map_err(RbPolarsErr::from)?;
|
200
|
+
Ok(df.into())
|
201
|
+
}
|
202
|
+
|
203
|
+
pub fn vstack_mut(&self, df: &RbDataFrame) -> RbResult<()> {
|
204
|
+
self.df
|
205
|
+
.borrow_mut()
|
206
|
+
.vstack_mut(&df.df.borrow())
|
207
|
+
.map_err(RbPolarsErr::from)?;
|
208
|
+
Ok(())
|
209
|
+
}
|
210
|
+
|
211
|
+
pub fn extend(&self, df: &RbDataFrame) -> RbResult<()> {
|
212
|
+
self.df
|
213
|
+
.borrow_mut()
|
214
|
+
.extend(&df.df.borrow())
|
215
|
+
.map_err(RbPolarsErr::from)?;
|
216
|
+
Ok(())
|
217
|
+
}
|
218
|
+
|
219
|
+
pub fn drop_in_place(&self, name: String) -> RbResult<RbSeries> {
|
220
|
+
let s = self
|
221
|
+
.df
|
222
|
+
.borrow_mut()
|
223
|
+
.drop_in_place(&name)
|
224
|
+
.map_err(RbPolarsErr::from)?;
|
225
|
+
Ok(RbSeries::new(s))
|
226
|
+
}
|
227
|
+
|
228
|
+
pub fn select_at_idx(&self, idx: usize) -> Option<RbSeries> {
|
229
|
+
self.df
|
230
|
+
.borrow()
|
231
|
+
.select_at_idx(idx)
|
232
|
+
.map(|s| RbSeries::new(s.clone()))
|
233
|
+
}
|
234
|
+
|
235
|
+
pub fn get_column_index(&self, name: String) -> Option<usize> {
|
236
|
+
self.df.borrow().get_column_index(&name)
|
237
|
+
}
|
238
|
+
|
239
|
+
pub fn get_column(&self, name: String) -> RbResult<RbSeries> {
|
240
|
+
self.df
|
241
|
+
.borrow()
|
242
|
+
.column(&name)
|
243
|
+
.map(|s| RbSeries::new(s.clone()))
|
244
|
+
.map_err(RbPolarsErr::from)
|
245
|
+
}
|
246
|
+
|
247
|
+
pub fn select(&self, selection: Vec<String>) -> RbResult<Self> {
|
248
|
+
let df = self
|
249
|
+
.df
|
250
|
+
.borrow()
|
251
|
+
.select(selection)
|
252
|
+
.map_err(RbPolarsErr::from)?;
|
253
|
+
Ok(RbDataFrame::new(df))
|
254
|
+
}
|
255
|
+
|
256
|
+
pub fn gather(&self, indices: Vec<IdxSize>) -> RbResult<Self> {
|
257
|
+
let indices = IdxCa::from_vec("", indices);
|
258
|
+
let df = self.df.borrow().take(&indices).map_err(RbPolarsErr::from)?;
|
259
|
+
Ok(RbDataFrame::new(df))
|
260
|
+
}
|
261
|
+
|
262
|
+
pub fn take_with_series(&self, indices: &RbSeries) -> RbResult<Self> {
|
263
|
+
let binding = indices.series.borrow();
|
264
|
+
let idx = binding.idx().map_err(RbPolarsErr::from)?;
|
265
|
+
let df = self.df.borrow().take(idx).map_err(RbPolarsErr::from)?;
|
266
|
+
Ok(RbDataFrame::new(df))
|
267
|
+
}
|
268
|
+
|
269
|
+
pub fn replace(&self, column: String, new_col: &RbSeries) -> RbResult<()> {
|
270
|
+
self.df
|
271
|
+
.borrow_mut()
|
272
|
+
.replace(&column, new_col.series.borrow().clone())
|
273
|
+
.map_err(RbPolarsErr::from)?;
|
274
|
+
Ok(())
|
275
|
+
}
|
276
|
+
|
277
|
+
pub fn replace_column(&self, index: usize, new_col: &RbSeries) -> RbResult<()> {
|
278
|
+
self.df
|
279
|
+
.borrow_mut()
|
280
|
+
.replace_column(index, new_col.series.borrow().clone())
|
281
|
+
.map_err(RbPolarsErr::from)?;
|
282
|
+
Ok(())
|
283
|
+
}
|
284
|
+
|
285
|
+
pub fn insert_column(&self, index: usize, new_col: &RbSeries) -> RbResult<()> {
|
286
|
+
self.df
|
287
|
+
.borrow_mut()
|
288
|
+
.insert_column(index, new_col.series.borrow().clone())
|
289
|
+
.map_err(RbPolarsErr::from)?;
|
290
|
+
Ok(())
|
291
|
+
}
|
292
|
+
|
293
|
+
pub fn slice(&self, offset: usize, length: Option<usize>) -> Self {
|
294
|
+
let df = self.df.borrow().slice(
|
295
|
+
offset as i64,
|
296
|
+
length.unwrap_or_else(|| self.df.borrow().height()),
|
297
|
+
);
|
298
|
+
df.into()
|
299
|
+
}
|
300
|
+
|
301
|
+
pub fn head(&self, length: Option<usize>) -> Self {
|
302
|
+
self.df.borrow().head(length).into()
|
303
|
+
}
|
304
|
+
|
305
|
+
pub fn tail(&self, length: Option<usize>) -> Self {
|
306
|
+
self.df.borrow().tail(length).into()
|
307
|
+
}
|
308
|
+
|
309
|
+
pub fn is_unique(&self) -> RbResult<RbSeries> {
|
310
|
+
let mask = self.df.borrow().is_unique().map_err(RbPolarsErr::from)?;
|
311
|
+
Ok(mask.into_series().into())
|
312
|
+
}
|
313
|
+
|
314
|
+
pub fn is_duplicated(&self) -> RbResult<RbSeries> {
|
315
|
+
let mask = self
|
316
|
+
.df
|
317
|
+
.borrow()
|
318
|
+
.is_duplicated()
|
319
|
+
.map_err(RbPolarsErr::from)?;
|
320
|
+
Ok(mask.into_series().into())
|
321
|
+
}
|
322
|
+
|
323
|
+
pub fn equals(&self, other: &RbDataFrame, null_equal: bool) -> bool {
|
324
|
+
if null_equal {
|
325
|
+
self.df.borrow().equals_missing(&other.df.borrow())
|
326
|
+
} else {
|
327
|
+
self.df.borrow().equals(&other.df.borrow())
|
328
|
+
}
|
329
|
+
}
|
330
|
+
|
331
|
+
pub fn with_row_index(&self, name: String, offset: Option<IdxSize>) -> RbResult<Self> {
|
332
|
+
let df = self
|
333
|
+
.df
|
334
|
+
.borrow()
|
335
|
+
.with_row_index(&name, offset)
|
336
|
+
.map_err(RbPolarsErr::from)?;
|
337
|
+
Ok(df.into())
|
338
|
+
}
|
339
|
+
|
340
|
+
pub fn clone(&self) -> Self {
|
341
|
+
RbDataFrame::new(self.df.borrow().clone())
|
342
|
+
}
|
343
|
+
|
344
|
+
pub fn melt(
|
345
|
+
&self,
|
346
|
+
id_vars: Vec<String>,
|
347
|
+
value_vars: Vec<String>,
|
348
|
+
value_name: Option<String>,
|
349
|
+
variable_name: Option<String>,
|
350
|
+
) -> RbResult<Self> {
|
351
|
+
let args = MeltArgs {
|
352
|
+
id_vars: strings_to_smartstrings(id_vars),
|
353
|
+
value_vars: strings_to_smartstrings(value_vars),
|
354
|
+
value_name: value_name.map(|s| s.into()),
|
355
|
+
variable_name: variable_name.map(|s| s.into()),
|
356
|
+
streamable: false,
|
357
|
+
};
|
358
|
+
|
359
|
+
let df = self.df.borrow().melt2(args).map_err(RbPolarsErr::from)?;
|
360
|
+
Ok(RbDataFrame::new(df))
|
361
|
+
}
|
362
|
+
|
363
|
+
#[allow(clippy::too_many_arguments)]
|
364
|
+
pub fn pivot_expr(
|
365
|
+
&self,
|
366
|
+
index: Vec<String>,
|
367
|
+
columns: Vec<String>,
|
368
|
+
values: Option<Vec<String>>,
|
369
|
+
maintain_order: bool,
|
370
|
+
sort_columns: bool,
|
371
|
+
aggregate_expr: Option<&RbExpr>,
|
372
|
+
separator: Option<String>,
|
373
|
+
) -> RbResult<Self> {
|
374
|
+
let fun = match maintain_order {
|
375
|
+
true => pivot_stable,
|
376
|
+
false => pivot,
|
377
|
+
};
|
378
|
+
let agg_expr = aggregate_expr.map(|aggregate_expr| aggregate_expr.inner.clone());
|
379
|
+
let df = fun(
|
380
|
+
&self.df.borrow(),
|
381
|
+
index,
|
382
|
+
columns,
|
383
|
+
values,
|
384
|
+
sort_columns,
|
385
|
+
agg_expr,
|
386
|
+
separator.as_deref(),
|
387
|
+
)
|
388
|
+
.map_err(RbPolarsErr::from)?;
|
389
|
+
Ok(RbDataFrame::new(df))
|
390
|
+
}
|
391
|
+
|
392
|
+
pub fn partition_by(
|
393
|
+
&self,
|
394
|
+
by: Vec<String>,
|
395
|
+
maintain_order: bool,
|
396
|
+
include_key: bool,
|
397
|
+
) -> RbResult<RArray> {
|
398
|
+
let out = if maintain_order {
|
399
|
+
self.df.borrow().partition_by_stable(by, include_key)
|
400
|
+
} else {
|
401
|
+
self.df.borrow().partition_by(by, include_key)
|
402
|
+
}
|
403
|
+
.map_err(RbPolarsErr::from)?;
|
404
|
+
Ok(RArray::from_iter(out.into_iter().map(RbDataFrame::new)))
|
405
|
+
}
|
406
|
+
|
407
|
+
pub fn lazy(&self) -> RbLazyFrame {
|
408
|
+
self.df.borrow().clone().lazy().into()
|
409
|
+
}
|
410
|
+
|
411
|
+
pub fn max_horizontal(&self) -> RbResult<Option<RbSeries>> {
|
412
|
+
let s = self
|
413
|
+
.df
|
414
|
+
.borrow()
|
415
|
+
.max_horizontal()
|
416
|
+
.map_err(RbPolarsErr::from)?;
|
417
|
+
Ok(s.map(|s| s.into()))
|
418
|
+
}
|
419
|
+
|
420
|
+
pub fn min_horizontal(&self) -> RbResult<Option<RbSeries>> {
|
421
|
+
let s = self
|
422
|
+
.df
|
423
|
+
.borrow()
|
424
|
+
.min_horizontal()
|
425
|
+
.map_err(RbPolarsErr::from)?;
|
426
|
+
Ok(s.map(|s| s.into()))
|
427
|
+
}
|
428
|
+
|
429
|
+
pub fn sum_horizontal(&self, ignore_nulls: bool) -> RbResult<Option<RbSeries>> {
|
430
|
+
let null_strategy = if ignore_nulls {
|
431
|
+
NullStrategy::Ignore
|
432
|
+
} else {
|
433
|
+
NullStrategy::Propagate
|
434
|
+
};
|
435
|
+
let s = self
|
436
|
+
.df
|
437
|
+
.borrow()
|
438
|
+
.sum_horizontal(null_strategy)
|
439
|
+
.map_err(RbPolarsErr::from)?;
|
440
|
+
Ok(s.map(|s| s.into()))
|
441
|
+
}
|
442
|
+
|
443
|
+
pub fn mean_horizontal(&self, ignore_nulls: bool) -> RbResult<Option<RbSeries>> {
|
444
|
+
let null_strategy = if ignore_nulls {
|
445
|
+
NullStrategy::Ignore
|
446
|
+
} else {
|
447
|
+
NullStrategy::Propagate
|
448
|
+
};
|
449
|
+
let s = self
|
450
|
+
.df
|
451
|
+
.borrow()
|
452
|
+
.mean_horizontal(null_strategy)
|
453
|
+
.map_err(RbPolarsErr::from)?;
|
454
|
+
Ok(s.map(|s| s.into()))
|
455
|
+
}
|
456
|
+
|
457
|
+
pub fn to_dummies(
|
458
|
+
&self,
|
459
|
+
columns: Option<Vec<String>>,
|
460
|
+
separator: Option<String>,
|
461
|
+
drop_first: bool,
|
462
|
+
) -> RbResult<Self> {
|
463
|
+
let df = match columns {
|
464
|
+
Some(cols) => self.df.borrow().columns_to_dummies(
|
465
|
+
cols.iter().map(|x| x as &str).collect(),
|
466
|
+
separator.as_deref(),
|
467
|
+
drop_first,
|
468
|
+
),
|
469
|
+
None => self
|
470
|
+
.df
|
471
|
+
.borrow()
|
472
|
+
.to_dummies(separator.as_deref(), drop_first),
|
473
|
+
}
|
474
|
+
.map_err(RbPolarsErr::from)?;
|
475
|
+
Ok(df.into())
|
476
|
+
}
|
477
|
+
|
478
|
+
pub fn null_count(&self) -> Self {
|
479
|
+
let df = self.df.borrow().null_count();
|
480
|
+
df.into()
|
481
|
+
}
|
482
|
+
|
483
|
+
pub fn map_rows(
|
484
|
+
&self,
|
485
|
+
lambda: Value,
|
486
|
+
output_type: Option<Wrap<DataType>>,
|
487
|
+
inference_size: usize,
|
488
|
+
) -> RbResult<(Value, bool)> {
|
489
|
+
let df = &self.df.borrow();
|
490
|
+
|
491
|
+
let output_type = output_type.map(|dt| dt.0);
|
492
|
+
let out = match output_type {
|
493
|
+
Some(DataType::Int32) => {
|
494
|
+
apply_lambda_with_primitive_out_type::<Int32Type>(df, lambda, 0, None).into_series()
|
495
|
+
}
|
496
|
+
Some(DataType::Int64) => {
|
497
|
+
apply_lambda_with_primitive_out_type::<Int64Type>(df, lambda, 0, None).into_series()
|
498
|
+
}
|
499
|
+
Some(DataType::UInt32) => {
|
500
|
+
apply_lambda_with_primitive_out_type::<UInt32Type>(df, lambda, 0, None)
|
501
|
+
.into_series()
|
502
|
+
}
|
503
|
+
Some(DataType::UInt64) => {
|
504
|
+
apply_lambda_with_primitive_out_type::<UInt64Type>(df, lambda, 0, None)
|
505
|
+
.into_series()
|
506
|
+
}
|
507
|
+
Some(DataType::Float32) => {
|
508
|
+
apply_lambda_with_primitive_out_type::<Float32Type>(df, lambda, 0, None)
|
509
|
+
.into_series()
|
510
|
+
}
|
511
|
+
Some(DataType::Float64) => {
|
512
|
+
apply_lambda_with_primitive_out_type::<Float64Type>(df, lambda, 0, None)
|
513
|
+
.into_series()
|
514
|
+
}
|
515
|
+
Some(DataType::Boolean) => {
|
516
|
+
apply_lambda_with_bool_out_type(df, lambda, 0, None).into_series()
|
517
|
+
}
|
518
|
+
Some(DataType::Date) => {
|
519
|
+
apply_lambda_with_primitive_out_type::<Int32Type>(df, lambda, 0, None)
|
520
|
+
.into_date()
|
521
|
+
.into_series()
|
522
|
+
}
|
523
|
+
Some(DataType::Datetime(tu, tz)) => {
|
524
|
+
apply_lambda_with_primitive_out_type::<Int64Type>(df, lambda, 0, None)
|
525
|
+
.into_datetime(tu, tz)
|
526
|
+
.into_series()
|
527
|
+
}
|
528
|
+
Some(DataType::String) => {
|
529
|
+
apply_lambda_with_utf8_out_type(df, lambda, 0, None).into_series()
|
530
|
+
}
|
531
|
+
_ => return apply_lambda_unknown(df, lambda, inference_size),
|
532
|
+
};
|
533
|
+
|
534
|
+
Ok((Obj::wrap(RbSeries::from(out)).as_value(), false))
|
535
|
+
}
|
536
|
+
|
537
|
+
pub fn shrink_to_fit(&self) {
|
538
|
+
self.df.borrow_mut().shrink_to_fit();
|
539
|
+
}
|
540
|
+
|
541
|
+
pub fn hash_rows(&self, k0: u64, k1: u64, k2: u64, k3: u64) -> RbResult<RbSeries> {
|
542
|
+
let hb = ahash::RandomState::with_seeds(k0, k1, k2, k3);
|
543
|
+
let hash = self
|
544
|
+
.df
|
545
|
+
.borrow_mut()
|
546
|
+
.hash_rows(Some(hb))
|
547
|
+
.map_err(RbPolarsErr::from)?;
|
548
|
+
Ok(hash.into_series().into())
|
549
|
+
}
|
550
|
+
|
551
|
+
pub fn transpose(&self, keep_names_as: Option<String>, column_names: Value) -> RbResult<Self> {
|
552
|
+
let new_col_names = if let Ok(name) = <Vec<String>>::try_convert(column_names) {
|
553
|
+
Some(Either::Right(name))
|
554
|
+
} else if let Ok(name) = String::try_convert(column_names) {
|
555
|
+
Some(Either::Left(name))
|
556
|
+
} else {
|
557
|
+
None
|
558
|
+
};
|
559
|
+
Ok(self
|
560
|
+
.df
|
561
|
+
.borrow_mut()
|
562
|
+
.transpose(keep_names_as.as_deref(), new_col_names)
|
563
|
+
.map_err(RbPolarsErr::from)?
|
564
|
+
.into())
|
565
|
+
}
|
566
|
+
|
567
|
+
pub fn upsample(
|
568
|
+
&self,
|
569
|
+
by: Vec<String>,
|
570
|
+
index_column: String,
|
571
|
+
every: String,
|
572
|
+
offset: String,
|
573
|
+
stable: bool,
|
574
|
+
) -> RbResult<Self> {
|
575
|
+
let out = if stable {
|
576
|
+
self.df.borrow().upsample_stable(
|
577
|
+
by,
|
578
|
+
&index_column,
|
579
|
+
Duration::parse(&every),
|
580
|
+
Duration::parse(&offset),
|
581
|
+
)
|
582
|
+
} else {
|
583
|
+
self.df.borrow().upsample(
|
584
|
+
by,
|
585
|
+
&index_column,
|
586
|
+
Duration::parse(&every),
|
587
|
+
Duration::parse(&offset),
|
588
|
+
)
|
589
|
+
};
|
590
|
+
let out = out.map_err(RbPolarsErr::from)?;
|
591
|
+
Ok(out.into())
|
592
|
+
}
|
593
|
+
|
594
|
+
pub fn to_struct(&self, name: String) -> RbSeries {
|
595
|
+
let s = self.df.borrow().clone().into_struct(&name);
|
596
|
+
s.into_series().into()
|
597
|
+
}
|
598
|
+
|
599
|
+
pub fn unnest(&self, names: Vec<String>) -> RbResult<Self> {
|
600
|
+
let df = self.df.borrow().unnest(names).map_err(RbPolarsErr::from)?;
|
601
|
+
Ok(df.into())
|
602
|
+
}
|
603
|
+
|
604
|
+
pub fn clear(&self) -> Self {
|
605
|
+
self.df.borrow().clear().into()
|
606
|
+
}
|
607
|
+
}
|