-
Notifications
You must be signed in to change notification settings - Fork 112
/
dataframe.rs
489 lines (419 loc) · 14 KB
/
dataframe.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use polars::prelude::*;
use polars_ops::pivot::{pivot_stable, PivotAgg};
use polars::export::{arrow, arrow::ffi};
use std::collections::HashMap;
use crate::datatypes::ExSeriesDtype;
use crate::ex_expr_to_exprs;
use crate::{ExDataFrame, ExExpr, ExLazyFrame, ExSeries, ExplorerError};
use either::Either;
use smartstring::alias::String as SmartString;
// Loads the IO functions for read/writing CSV, NDJSON, Parquet, etc.
pub mod io;
fn to_string_names(names: Vec<&str>) -> Vec<String> {
names.into_iter().map(|s| s.to_string()).collect()
}
pub fn to_smart_strings(slices: Vec<&str>) -> Vec<SmartString> {
slices.into_iter().map(|s| s.into()).collect()
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_transpose(
df: ExDataFrame,
keep_names_as: Option<&str>,
new_col_names: Option<Vec<String>>,
) -> Result<ExDataFrame, ExplorerError> {
let column_names = new_col_names.map(Either::Right);
let new_df = df.clone_inner().transpose(keep_names_as, column_names)?;
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif]
pub fn df_names(df: ExDataFrame) -> Result<Vec<String>, ExplorerError> {
let names = to_string_names(df.get_column_names());
Ok(names)
}
#[rustler::nif]
pub fn df_dtypes(df: ExDataFrame) -> Result<Vec<ExSeriesDtype>, ExplorerError> {
let mut dtypes: Vec<ExSeriesDtype> = vec![];
for dtype in df.dtypes().iter() {
dtypes.push(ExSeriesDtype::try_from(dtype)?)
}
Ok(dtypes)
}
#[rustler::nif]
pub fn df_shape(df: ExDataFrame) -> Result<(usize, usize), ExplorerError> {
Ok(df.shape())
}
#[rustler::nif]
pub fn df_n_rows(df: ExDataFrame) -> Result<usize, ExplorerError> {
Ok(df.height())
}
#[rustler::nif]
pub fn df_width(df: ExDataFrame) -> Result<usize, ExplorerError> {
Ok(df.width())
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_concat_columns(
data: ExDataFrame,
others: Vec<ExDataFrame>,
) -> Result<ExDataFrame, ExplorerError> {
let id_column = "__row_count_id__";
let first = data.clone_inner().lazy().with_row_index(id_column, None);
// We need to be able to handle arbitrary column name overlap.
// This builds up a join and suffixes conflicting names with _N where
// N is the index of the df in the join array.
let (out_df, _) = others
.iter()
.map(|data| data.clone_inner().lazy().with_row_index(id_column, None))
.fold((first, 1), |(acc_df, count), lazy_df| {
let suffix = format!("_{count}");
let new_df = acc_df
.join_builder()
.with(lazy_df)
.how(JoinType::Inner)
.left_on([col(id_column)])
.right_on([col(id_column)])
.suffix(suffix)
.finish();
(new_df, count + 1)
});
Ok(ExDataFrame::new(out_df.drop([id_column]).collect()?))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_drop(df: ExDataFrame, name: &str) -> Result<ExDataFrame, ExplorerError> {
let new_df = df.drop(name)?;
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_select_at_idx(df: ExDataFrame, idx: usize) -> Result<Option<ExSeries>, ExplorerError> {
let result = df.select_at_idx(idx).map(|s| ExSeries::new(s.clone()));
Ok(result)
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_pull(df: ExDataFrame, name: &str) -> Result<ExSeries, ExplorerError> {
let series = df.column(name).map(|s| ExSeries::new(s.clone()))?;
Ok(series)
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_mask(df: ExDataFrame, mask: ExSeries) -> Result<ExDataFrame, ExplorerError> {
if let Ok(ca) = mask.bool() {
let new_df = df.filter(ca)?;
Ok(ExDataFrame::new(new_df))
} else {
Err(ExplorerError::Other("Expected a boolean mask".into()))
}
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_slice_by_indices(
df: ExDataFrame,
indices: Vec<u32>,
groups: Vec<&str>,
) -> Result<ExDataFrame, ExplorerError> {
let idx = UInt32Chunked::from_vec("idx", indices);
let new_df = if groups.is_empty() {
df.take(&idx)?
} else {
df.group_by_stable(groups)?.apply(|df| df.take(&idx))?
};
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_slice_by_series(
df: ExDataFrame,
series: ExSeries,
groups: Vec<&str>,
) -> Result<ExDataFrame, ExplorerError> {
match series.strict_cast(&DataType::UInt32) {
Ok(casted) => {
let idx = casted.u32()?;
let new_df = if groups.is_empty() {
df.take(idx)?
} else {
df.group_by_stable(groups)?.apply(|df| df.take(idx))?
};
Ok(ExDataFrame::new(new_df))
}
Err(_) => Err(ExplorerError::Other(
"slice/2 expects a series of positive integers".into(),
)),
}
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_sample_n(
df: ExDataFrame,
n: u64,
replace: bool,
shuffle: bool,
seed: Option<u64>,
groups: Vec<String>,
) -> Result<ExDataFrame, ExplorerError> {
let n_s = Series::new("n", &[n]);
let new_df = if groups.is_empty() {
df.sample_n(&n_s, replace, shuffle, seed)?
} else {
df.group_by_stable(groups)?
.apply(|df| df.sample_n(&n_s, replace, shuffle, seed))?
};
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_sample_frac(
df: ExDataFrame,
frac: f64,
replace: bool,
shuffle: bool,
seed: Option<u64>,
groups: Vec<String>,
) -> Result<ExDataFrame, ExplorerError> {
let frac_s = Series::new("frac", &[frac]);
let new_df = if groups.is_empty() {
df.sample_frac(&frac_s, replace, shuffle, seed)?
} else {
df.group_by_stable(groups)?
.apply(|df| df.sample_frac(&frac_s, replace, shuffle, seed))?
};
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif]
fn df_from_arrow_stream_pointer(stream_ptr: u64) -> Result<ExDataFrame, ExplorerError> {
let stream_ptr = stream_ptr as *mut ffi::ArrowArrayStream;
let stream_ref = unsafe { stream_ptr.as_mut() }
.ok_or(ExplorerError::Other("Incorrect stream pointer".into()))?;
let mut res = unsafe { ffi::ArrowArrayStreamReader::try_new(stream_ref) }
.map_err(arrow_to_explorer_error)?;
let df = match unsafe { res.next() } {
None => DataFrame::empty(),
Some(maybe) => {
let mut acc = array_to_dataframe(maybe)?;
while let Some(maybe) = unsafe { res.next() } {
let df = array_to_dataframe(maybe)?;
acc.vstack_mut(&df)?;
}
acc.align_chunks();
acc
}
};
Ok(ExDataFrame::new(df))
}
fn array_to_dataframe(
stream_chunk: PolarsResult<Box<dyn arrow::array::Array>>,
) -> Result<DataFrame, ExplorerError> {
let dyn_array = stream_chunk.map_err(arrow_to_explorer_error)?;
let struct_array = dyn_array
.as_any()
.downcast_ref::<crate::dataframe::arrow::array::StructArray>()
.ok_or(ExplorerError::Other(
"Unable to downcast to StructArray in ArrowArrayStreamReader chunk".into(),
))?
.clone();
DataFrame::try_from(struct_array).map_err(ExplorerError::Polars)
}
fn arrow_to_explorer_error(error: impl std::fmt::Debug) -> ExplorerError {
ExplorerError::Other(format!("Internal Arrow error: #{error:?}"))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_sort_by(
df: ExDataFrame,
by_columns: Vec<String>,
reverse: Vec<bool>,
maintain_order: bool,
multithreaded: bool,
nulls_last: bool,
groups: Vec<String>,
) -> Result<ExDataFrame, ExplorerError> {
let new_df = if groups.is_empty() {
// Note: we cannot use either df.sort or df.sort_with_options.
// df.sort does not allow a nulls_last option.
// df.sort_with_options only allows a single column.
let by_columns = df.select_series(by_columns)?;
df.sort_impl(
by_columns,
reverse,
nulls_last,
maintain_order,
None,
multithreaded,
)?
} else {
df.group_by_stable(groups)?.apply(|df| {
let by_columns = df.select_series(&by_columns)?;
df.sort_impl(
by_columns,
reverse.clone(),
nulls_last,
maintain_order,
None,
multithreaded,
)
})?
};
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_sort_with(
data: ExDataFrame,
expressions: Vec<ExExpr>,
directions: Vec<bool>,
maintain_order: bool,
nulls_last: bool,
groups: Vec<String>,
) -> Result<ExDataFrame, ExplorerError> {
let df = data.clone_inner();
let exprs = ex_expr_to_exprs(expressions);
let new_df = if groups.is_empty() {
df.lazy()
.sort_by_exprs(exprs, directions, nulls_last, maintain_order)
.collect()?
} else {
df.group_by_stable(groups)?.apply(|df| {
df.lazy()
.sort_by_exprs(&exprs, &directions, nulls_last, maintain_order)
.collect()
})?
};
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_slice(
df: ExDataFrame,
offset: i64,
length: usize,
groups: Vec<&str>,
) -> Result<ExDataFrame, ExplorerError> {
let new_df = if groups.is_empty() {
df.slice(offset, length)
} else {
df.group_by_stable(groups)?
.apply(|df| Ok(df.slice(offset, length)))?
};
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_to_dummies(df: ExDataFrame, selection: Vec<&str>) -> Result<ExDataFrame, ExplorerError> {
let drop_first = false;
let dummies = df
.select(selection)
.and_then(|df| df.to_dummies(None, drop_first))?;
Ok(ExDataFrame::new(dummies))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_put_column(df: ExDataFrame, series: ExSeries) -> Result<ExDataFrame, ExplorerError> {
let mut df = df.clone();
let s = series.clone_inner();
let new_df = df.with_column(s)?.clone();
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_nil_count(df: ExDataFrame) -> Result<ExDataFrame, ExplorerError> {
let new_df = df.null_count();
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif]
pub fn df_from_series(columns: Vec<ExSeries>) -> Result<ExDataFrame, ExplorerError> {
let columns = columns.into_iter().map(|c| c.clone_inner()).collect();
let df = DataFrame::new(columns)?;
Ok(ExDataFrame::new(df))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_groups(df: ExDataFrame, groups: Vec<&str>) -> Result<ExDataFrame, ExplorerError> {
let groups = df.group_by(groups)?.groups()?;
Ok(ExDataFrame::new(groups))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_group_indices(
df: ExDataFrame,
groups: Vec<&str>,
) -> Result<Vec<ExSeries>, ExplorerError> {
let series = df
.group_by_stable(groups)?
.groups()?
.column("groups")?
.list()?
.into_iter()
.map(|series| ExSeries::new(series.unwrap()))
.collect();
Ok(series)
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_pivot_wider(
df: ExDataFrame,
id_columns: Vec<&str>,
pivot_column: &str,
values_column: Vec<&str>,
names_prefix: Option<&str>,
) -> Result<ExDataFrame, ExplorerError> {
// We need to preserve the original ID columns with a prefix,
// so if there is any "new column name" coming from a "value column"
// conflicting with some ID column, we can keep that ID column and
// the new column names.
let mut df = df.clone_inner();
let explorer_prefix = "__explorer_column_id__";
let temp_id_names: Vec<String> = id_columns
.iter()
.map(|id_name| format!("{explorer_prefix}{id_name}"))
.collect();
for (id_name, new_name) in id_columns.iter().zip(&temp_id_names) {
df.rename(id_name, new_name)?;
}
let mut new_df = pivot_stable(
&df,
&temp_id_names,
[pivot_column],
Some(values_column),
false,
Some(PivotAgg::First),
None,
)?;
// Instead of using the names from the pivoted DF, we go back
// and restore the original ID column names, so we can use our
// algo below to preserve all columns.
let clean_names = new_df
.get_column_names()
.iter()
.map(|name| name.trim_start_matches(explorer_prefix))
.collect();
let mut new_names = to_string_names(clean_names);
let mut counter: HashMap<String, u16> = HashMap::new();
for name in new_names.iter_mut() {
let original_name = name.clone();
if let Some(count) = counter.get(name) {
if let Some(prefix) = names_prefix {
*name = format!("{prefix}{name}");
}
if original_name == name.clone() {
*name = format!("{name}_{count}");
}
counter
.entry(name.clone())
.and_modify(|c| *c += 1)
.or_insert(1);
} else {
if !id_columns.contains(&original_name.as_str()) {
if name == "null" {
*name = "nil".to_string();
}
if let Some(prefix) = names_prefix {
*name = format!("{prefix}{name}");
}
}
counter.insert(name.to_string(), 1);
}
}
new_df.set_column_names(&new_names)?;
Ok(ExDataFrame::new(new_df))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_lazy(df: ExDataFrame) -> Result<ExLazyFrame, ExplorerError> {
let new_lf = df.clone_inner().lazy();
Ok(ExLazyFrame::new(new_lf))
}
#[rustler::nif(schedule = "DirtyCpu")]
pub fn df_re_dtype(pattern: &str) -> Result<ExSeriesDtype, ExplorerError> {
let s = Series::new("dummy", [""])
.into_frame()
.lazy()
.with_column(col("dummy").str().extract_groups(pattern)?.alias("dummy"))
.collect()?
.column("dummy")?
.clone();
let ex_dtype = ExSeriesDtype::try_from(s.dtype())?;
Ok(ex_dtype)
}