Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-core/src/functions.rs
6939 views
1
//! # Functions
2
//!
3
//! Functions that might be useful.
4
//!
5
pub use crate::frame::horizontal::concat_df_horizontal;
6
#[cfg(feature = "diagonal_concat")]
7
use crate::prelude::*;
8
#[cfg(feature = "diagonal_concat")]
9
use crate::utils::concat_df;
10
11
/// Concat [`DataFrame`]s diagonally.
12
#[cfg(feature = "diagonal_concat")]
13
/// Concat diagonally thereby combining different schemas.
14
pub fn concat_df_diagonal(dfs: &[DataFrame]) -> PolarsResult<DataFrame> {
15
// TODO! replace with lazy only?
16
let upper_bound_width = dfs.iter().map(|df| df.width()).sum();
17
let mut column_names = PlHashSet::with_capacity(upper_bound_width);
18
let mut schema = Vec::with_capacity(upper_bound_width);
19
20
for df in dfs {
21
df.get_columns().iter().for_each(|s| {
22
let name = s.name().clone();
23
if column_names.insert(name.clone()) {
24
schema.push((name, s.dtype()))
25
}
26
});
27
}
28
29
let dfs = dfs
30
.iter()
31
.map(|df| {
32
let height = df.height();
33
let mut columns = Vec::with_capacity(schema.len());
34
35
for (name, dtype) in &schema {
36
match df.column(name.as_str()).ok() {
37
Some(s) => columns.push(s.clone()),
38
None => columns.push(Column::full_null(name.clone(), height, dtype)),
39
}
40
}
41
unsafe { DataFrame::new_no_checks(height, columns) }
42
})
43
.collect::<Vec<_>>();
44
45
concat_df(&dfs)
46
}
47
48