Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-core/src/utils/any_value.rs
6940 views
1
use crate::prelude::*;
2
use crate::utils::dtypes_to_supertype;
3
4
/// Determine the supertype of a collection of [`AnyValue`].
5
///
6
/// [`AnyValue`]: crate::datatypes::AnyValue
7
pub fn any_values_to_supertype<'a, I>(values: I) -> PolarsResult<DataType>
8
where
9
I: IntoIterator<Item = &'a AnyValue<'a>>,
10
{
11
let dtypes = any_values_to_dtype_set(values);
12
dtypes_to_supertype(&dtypes)
13
}
14
15
/// Determine the supertype and the number of unique data types of a collection of [`AnyValue`].
16
///
17
/// [`AnyValue`]: crate::datatypes::AnyValue
18
pub fn any_values_to_supertype_and_n_dtypes<'a, I>(values: I) -> PolarsResult<(DataType, usize)>
19
where
20
I: IntoIterator<Item = &'a AnyValue<'a>>,
21
{
22
let dtypes = any_values_to_dtype_set(values);
23
let supertype = dtypes_to_supertype(&dtypes)?;
24
let n_dtypes = dtypes.len();
25
Ok((supertype, n_dtypes))
26
}
27
28
/// Extract the ordered set of data types from a collection of AnyValues
29
///
30
/// Retaining the order is important if the set is used to determine a supertype,
31
/// as this can influence how Struct fields are constructed.
32
fn any_values_to_dtype_set<'a, I>(values: I) -> PlIndexSet<DataType>
33
where
34
I: IntoIterator<Item = &'a AnyValue<'a>>,
35
{
36
values.into_iter().map(|av| av.into()).collect()
37
}
38
39