Path: blob/main/crates/polars-core/src/utils/any_value.rs
6940 views
use crate::prelude::*;1use crate::utils::dtypes_to_supertype;23/// Determine the supertype of a collection of [`AnyValue`].4///5/// [`AnyValue`]: crate::datatypes::AnyValue6pub fn any_values_to_supertype<'a, I>(values: I) -> PolarsResult<DataType>7where8I: IntoIterator<Item = &'a AnyValue<'a>>,9{10let dtypes = any_values_to_dtype_set(values);11dtypes_to_supertype(&dtypes)12}1314/// Determine the supertype and the number of unique data types of a collection of [`AnyValue`].15///16/// [`AnyValue`]: crate::datatypes::AnyValue17pub fn any_values_to_supertype_and_n_dtypes<'a, I>(values: I) -> PolarsResult<(DataType, usize)>18where19I: IntoIterator<Item = &'a AnyValue<'a>>,20{21let dtypes = any_values_to_dtype_set(values);22let supertype = dtypes_to_supertype(&dtypes)?;23let n_dtypes = dtypes.len();24Ok((supertype, n_dtypes))25}2627/// Extract the ordered set of data types from a collection of AnyValues28///29/// Retaining the order is important if the set is used to determine a supertype,30/// as this can influence how Struct fields are constructed.31fn any_values_to_dtype_set<'a, I>(values: I) -> PlIndexSet<DataType>32where33I: IntoIterator<Item = &'a AnyValue<'a>>,34{35values.into_iter().map(|av| av.into()).collect()36}373839