Path: blob/main/crates/polars-core/src/chunked_array/collect.rs
6940 views
//! Methods for collecting into a ChunkedArray.1//!2//! For types that don't have dtype parameters:3//! iter.(try_)collect_ca(_trusted) (name)4//!5//! For all types:6//! iter.(try_)collect_ca(_trusted)_like (other_df) Copies name/dtype from other_df7//! iter.(try_)collect_ca(_trusted)_with_dtype (name, df)8//!9//! The try variants work on iterators of Results, the trusted variants do not10//! check the length of the iterator.1112use std::sync::Arc;1314use arrow::trusted_len::TrustedLen;15use polars_utils::pl_str::PlSmallStr;1617use crate::chunked_array::ChunkedArray;18use crate::datatypes::{19ArrayCollectIterExt, ArrayFromIter, ArrayFromIterDtype, DataType, Field, PolarsDataType,20};21use crate::prelude::CompatLevel;2223pub trait ChunkedCollectIterExt<T: PolarsDataType>: Iterator + Sized {24#[inline]25fn collect_ca_with_dtype(self, name: PlSmallStr, dtype: DataType) -> ChunkedArray<T>26where27T::Array: ArrayFromIterDtype<Self::Item>,28{29let field = Arc::new(Field::new(name, dtype));30let arr = self.collect_arr_with_dtype(field.dtype.to_arrow(CompatLevel::newest()));31ChunkedArray::from_chunk_iter_and_field(field, [arr])32}3334#[inline]35fn collect_ca_like(self, name_dtype_src: &ChunkedArray<T>) -> ChunkedArray<T>36where37T::Array: ArrayFromIterDtype<Self::Item>,38{39let field = Arc::clone(&name_dtype_src.field);40let arr = self.collect_arr_with_dtype(field.dtype.to_arrow(CompatLevel::newest()));41ChunkedArray::from_chunk_iter_and_field(field, [arr])42}4344#[inline]45fn collect_ca_trusted_with_dtype(self, name: PlSmallStr, dtype: DataType) -> ChunkedArray<T>46where47T::Array: ArrayFromIterDtype<Self::Item>,48Self: TrustedLen,49{50let field = Arc::new(Field::new(name, dtype));51let arr = self.collect_arr_trusted_with_dtype(field.dtype.to_arrow(CompatLevel::newest()));52ChunkedArray::from_chunk_iter_and_field(field, [arr])53}5455#[inline]56fn collect_ca_trusted_like(self, name_dtype_src: &ChunkedArray<T>) -> ChunkedArray<T>57where58T::Array: ArrayFromIterDtype<Self::Item>,59Self: TrustedLen,60{61let field = Arc::clone(&name_dtype_src.field);62let arr = self.collect_arr_trusted_with_dtype(field.dtype.to_arrow(CompatLevel::newest()));63ChunkedArray::from_chunk_iter_and_field(field, [arr])64}6566#[inline]67fn try_collect_ca_with_dtype<U, E>(68self,69name: PlSmallStr,70dtype: DataType,71) -> Result<ChunkedArray<T>, E>72where73T::Array: ArrayFromIterDtype<U>,74Self: Iterator<Item = Result<U, E>>,75{76let field = Arc::new(Field::new(name, dtype));77let arr = self.try_collect_arr_with_dtype(field.dtype.to_arrow(CompatLevel::newest()))?;78Ok(ChunkedArray::from_chunk_iter_and_field(field, [arr]))79}8081#[inline]82fn try_collect_ca_like<U, E>(83self,84name_dtype_src: &ChunkedArray<T>,85) -> Result<ChunkedArray<T>, E>86where87T::Array: ArrayFromIterDtype<U>,88Self: Iterator<Item = Result<U, E>>,89{90let field = Arc::clone(&name_dtype_src.field);91let arr = self.try_collect_arr_with_dtype(field.dtype.to_arrow(CompatLevel::newest()))?;92Ok(ChunkedArray::from_chunk_iter_and_field(field, [arr]))93}9495#[inline]96fn try_collect_ca_trusted_with_dtype<U, E>(97self,98name: PlSmallStr,99dtype: DataType,100) -> Result<ChunkedArray<T>, E>101where102T::Array: ArrayFromIterDtype<U>,103Self: Iterator<Item = Result<U, E>> + TrustedLen,104{105let field = Arc::new(Field::new(name, dtype));106let arr =107self.try_collect_arr_trusted_with_dtype(field.dtype.to_arrow(CompatLevel::newest()))?;108Ok(ChunkedArray::from_chunk_iter_and_field(field, [arr]))109}110111#[inline]112fn try_collect_ca_trusted_like<U, E>(113self,114name_dtype_src: &ChunkedArray<T>,115) -> Result<ChunkedArray<T>, E>116where117T::Array: ArrayFromIterDtype<U>,118Self: Iterator<Item = Result<U, E>> + TrustedLen,119{120let field = Arc::clone(&name_dtype_src.field);121let arr =122self.try_collect_arr_trusted_with_dtype(field.dtype.to_arrow(CompatLevel::newest()))?;123Ok(ChunkedArray::from_chunk_iter_and_field(field, [arr]))124}125}126127impl<T: PolarsDataType, I: Iterator> ChunkedCollectIterExt<T> for I {}128129pub trait ChunkedCollectInferIterExt<T: PolarsDataType>: Iterator + Sized {130#[inline]131fn collect_ca(self, name: PlSmallStr) -> ChunkedArray<T>132where133T::Array: ArrayFromIter<Self::Item>,134{135let field = Arc::new(Field::new(name, T::get_static_dtype()));136let arr = self.collect_arr();137ChunkedArray::from_chunk_iter_and_field(field, [arr])138}139140#[inline]141fn collect_ca_trusted(self, name: PlSmallStr) -> ChunkedArray<T>142where143T::Array: ArrayFromIter<Self::Item>,144Self: TrustedLen,145{146let field = Arc::new(Field::new(name, T::get_static_dtype()));147let arr = self.collect_arr_trusted();148ChunkedArray::from_chunk_iter_and_field(field, [arr])149}150151#[inline]152fn try_collect_ca<U, E>(self, name: PlSmallStr) -> Result<ChunkedArray<T>, E>153where154T::Array: ArrayFromIter<U>,155Self: Iterator<Item = Result<U, E>>,156{157let field = Arc::new(Field::new(name, T::get_static_dtype()));158let arr = self.try_collect_arr()?;159Ok(ChunkedArray::from_chunk_iter_and_field(field, [arr]))160}161162#[inline]163fn try_collect_ca_trusted<U, E>(self, name: PlSmallStr) -> Result<ChunkedArray<T>, E>164where165T::Array: ArrayFromIter<U>,166Self: Iterator<Item = Result<U, E>> + TrustedLen,167{168let field = Arc::new(Field::new(name, T::get_static_dtype()));169let arr = self.try_collect_arr_trusted()?;170Ok(ChunkedArray::from_chunk_iter_and_field(field, [arr]))171}172}173174impl<T: PolarsDataType, I: Iterator> ChunkedCollectInferIterExt<T> for I {}175176177