Path: blob/main/crates/polars-core/src/series/implementations/extension.rs
7884 views
use super::*;1use crate::prelude::*;23unsafe impl IntoSeries for ExtensionChunked {4fn into_series(self) -> Series {5Series(Arc::new(SeriesWrap(self)))6}7}89impl SeriesWrap<ExtensionChunked> {10fn apply_on_storage<F>(&self, apply: F) -> Series11where12F: Fn(&Series) -> Series,13{14apply(self.0.storage()).into_extension(self.0.extension_type().clone())15}1617fn try_apply_on_storage<F>(&self, apply: F) -> PolarsResult<Series>18where19F: Fn(&Series) -> PolarsResult<Series>,20{21Ok(apply(self.0.storage())?.into_extension(self.0.extension_type().clone()))22}23}2425impl private::PrivateSeries for SeriesWrap<ExtensionChunked> {26fn _field(&self) -> Cow<'_, Field> {27Cow::Owned(self.0.field())28}2930fn _dtype(&self) -> &DataType {31self.0.dtype()32}3334fn compute_len(&mut self) {35self.0.storage_mut().compute_len();36}3738fn _get_flags(&self) -> StatisticsFlags {39self.0.storage().get_flags()40}4142fn _set_flags(&mut self, flags: StatisticsFlags) {43self.0.storage_mut().set_flags(flags)44}4546fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {47self.0.storage().into_total_eq_inner()48}4950fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {51self.0.storage().into_total_ord_inner()52}5354fn vec_hash(55&self,56build_hasher: PlSeedableRandomStateQuality,57buf: &mut Vec<u64>,58) -> PolarsResult<()> {59self.0.storage().vec_hash(build_hasher, buf)60}6162fn vec_hash_combine(63&self,64build_hasher: PlSeedableRandomStateQuality,65hashes: &mut [u64],66) -> PolarsResult<()> {67self.0.storage().vec_hash_combine(build_hasher, hashes)68}6970fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {71self.0.storage().group_tuples(multithreaded, sorted)72}7374fn zip_with_same_type(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {75assert!(self._dtype() == other.dtype());76self.try_apply_on_storage(|s| s.zip_with_same_type(mask, other.ext()?.storage()))77}7879#[cfg(feature = "algorithm_group_by")]80unsafe fn agg_list(&self, groups: &GroupsType) -> Series {81let list = self.0.storage().agg_list(groups);82let mut list = list.list().unwrap().clone();83unsafe { list.to_logical(self.dtype().clone()) };84list.into_series()85}8687fn arg_sort_multiple(88&self,89by: &[Column],90options: &SortMultipleOptions,91) -> PolarsResult<IdxCa> {92self.0.storage().arg_sort_multiple(by, options)93}94}9596impl private::PrivateSeriesNumeric for SeriesWrap<ExtensionChunked> {97fn bit_repr(&self) -> Option<BitRepr> {98self.0.storage().bit_repr()99}100}101102impl SeriesTrait for SeriesWrap<ExtensionChunked> {103fn rename(&mut self, name: PlSmallStr) {104self.0.rename(name);105}106107fn chunk_lengths(&self) -> ChunkLenIter<'_> {108self.0.storage().chunk_lengths()109}110111fn name(&self) -> &PlSmallStr {112self.0.name()113}114115fn chunks(&self) -> &Vec<ArrayRef> {116self.0.storage().chunks()117}118119unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {120self.0.storage_mut().chunks_mut()121}122123fn slice(&self, offset: i64, length: usize) -> Series {124self.0125.storage()126.slice(offset, length)127.into_extension(self.0.extension_type().clone())128}129130fn split_at(&self, offset: i64) -> (Series, Series) {131let (left, right) = self.0.storage().split_at(offset);132(133left.into_extension(self.0.extension_type().clone()),134right.into_extension(self.0.extension_type().clone()),135)136}137138fn append(&mut self, other: &Series) -> PolarsResult<()> {139assert!(self.0.dtype() == other.dtype());140self.0.storage_mut().append(other.ext()?.storage())?;141Ok(())142}143144fn append_owned(&mut self, mut other: Series) -> PolarsResult<()> {145assert!(self.0.dtype() == other.dtype());146self.0.storage_mut().append_owned(std::mem::take(147other148._get_inner_mut()149.as_any_mut()150.downcast_mut::<ExtensionChunked>()151.unwrap()152.storage_mut(),153))?;154Ok(())155}156157fn extend(&mut self, other: &Series) -> PolarsResult<()> {158assert!(self.0.dtype() == other.dtype());159self.0.storage_mut().extend(other.ext()?.storage())?;160Ok(())161}162163fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {164self.try_apply_on_storage(|s| s.filter(filter))165}166167fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {168self.try_apply_on_storage(|s| s.take(indices))169}170171unsafe fn take_unchecked(&self, idx: &IdxCa) -> Series {172self.apply_on_storage(|s| s.take_unchecked(idx))173}174175fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {176self.try_apply_on_storage(|s| s.take_slice(indices))177}178179unsafe fn take_slice_unchecked(&self, idx: &[IdxSize]) -> Series {180self.apply_on_storage(|s| s.take_slice_unchecked(idx))181}182183fn len(&self) -> usize {184self.0.storage().len()185}186187fn rechunk(&self) -> Series {188self.apply_on_storage(|s| s.rechunk())189}190191fn new_from_index(&self, index: usize, length: usize) -> Series {192self.apply_on_storage(|s| s.new_from_index(index, length))193}194195fn deposit(&self, validity: &Bitmap) -> Series {196self.apply_on_storage(|s| s.deposit(validity))197}198199fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {200assert!(self.0.dtype() == other.dtype());201self.0202.storage()203.find_validity_mismatch(other.ext().unwrap().storage(), idxs)204}205206fn cast(&self, dtype: &DataType, options: CastOptions) -> PolarsResult<Series> {207self.0.cast_with_options(dtype, options)208}209210unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {211self.0.storage().get_unchecked(index)212}213214fn null_count(&self) -> usize {215self.0.storage().null_count()216}217218fn has_nulls(&self) -> bool {219self.0.storage().has_nulls()220}221222fn is_null(&self) -> BooleanChunked {223self.0.storage().is_null()224}225226fn is_not_null(&self) -> BooleanChunked {227self.0.storage().is_not_null()228}229230fn reverse(&self) -> Series {231self.apply_on_storage(|s| s.reverse())232}233234fn shift(&self, periods: i64) -> Series {235self.apply_on_storage(|s| s.shift(periods))236}237238fn clone_inner(&self) -> Arc<dyn SeriesTrait> {239Arc::new(SeriesWrap(Clone::clone(&self.0)))240}241242fn as_any(&self) -> &dyn Any {243&self.0244}245246fn as_any_mut(&mut self) -> &mut dyn Any {247&mut self.0248}249250fn as_phys_any(&self) -> &dyn Any {251self.0.storage().as_phys_any()252}253254fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {255self256}257258fn field(&self) -> Cow<'_, Field> {259Cow::Owned(self.0.field())260}261262fn dtype(&self) -> &DataType {263self.0.dtype()264}265266fn n_chunks(&self) -> usize {267self.0.storage().n_chunks()268}269270fn shrink_to_fit(&mut self) {271// no-op272}273274fn trim_lists_to_normalized_offsets(&self) -> Option<Series> {275let trimmed = self.0.storage().trim_lists_to_normalized_offsets()?;276Some(trimmed.into_extension(self.0.extension_type().clone()))277}278279fn propagate_nulls(&self) -> Option<Series> {280let propagated = self.0.storage().propagate_nulls()?;281Some(propagated.into_extension(self.0.extension_type().clone()))282}283284fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {285self.try_apply_on_storage(|s| s.sort_with(options))286}287288fn arg_sort(&self, options: SortOptions) -> IdxCa {289self.0.storage().arg_sort(options)290}291292fn unique(&self) -> PolarsResult<Series> {293self.try_apply_on_storage(|s| s.unique())294}295296fn n_unique(&self) -> PolarsResult<usize> {297self.0.storage().n_unique()298}299300fn arg_unique(&self) -> PolarsResult<IdxCa> {301self.0.storage().arg_unique()302}303304fn unique_id(&self) -> PolarsResult<(IdxSize, Vec<IdxSize>)> {305self.0.storage().unique_id()306}307308fn as_single_ptr(&mut self) -> PolarsResult<usize> {309self.0.storage_mut().as_single_ptr()310}311312#[cfg(feature = "approx_unique")]313fn approx_n_unique(&self) -> PolarsResult<IdxSize> {314self.0.storage().approx_n_unique()315}316}317318319