Path: blob/main/crates/polars-core/src/frame/validation.rs
6940 views
use polars_error::{PolarsResult, polars_bail};1use polars_utils::aliases::{InitHashMaps, PlHashSet};23use super::DataFrame;4use super::column::Column;56impl DataFrame {7/// Ensure all equal height and names are unique.8///9/// An Ok() result indicates `columns` is a valid state for a DataFrame.10pub fn validate_columns_slice(columns: &[Column]) -> PolarsResult<()> {11if columns.len() <= 1 {12return Ok(());13}1415if columns.len() <= 4 {16// Too small to be worth spawning a hashmap for, this is at most 6 comparisons.17for i in 0..columns.len() - 1 {18let name = columns[i].name();19let height = columns[i].len();2021for other in columns.iter().skip(i + 1) {22if other.name() == name {23polars_bail!(duplicate = name);24}2526if other.len() != height {27polars_bail!(28ShapeMismatch:29"height of column '{}' ({}) does not match height of column '{}' ({})",30other.name(), other.len(), name, height31)32}33}34}35} else {36let first = &columns[0];3738let first_len = first.len();39let first_name = first.name();4041let mut names = PlHashSet::with_capacity(columns.len());42names.insert(first_name);4344for col in &columns[1..] {45let col_name = col.name();46let col_len = col.len();4748if col_len != first_len {49polars_bail!(50ShapeMismatch:51"height of column '{}' ({}) does not match height of column '{}' ({})",52col_name, col_len, first_name, first_len53)54}5556if names.contains(col_name) {57polars_bail!(duplicate = col_name)58}5960names.insert(col_name);61}62}6364Ok(())65}66}676869