Path: blob/main/crates/polars-core/src/series/implementations/boolean.rs
8416 views
use super::*;1use crate::chunked_array::comparison::*;2#[cfg(feature = "algorithm_group_by")]3use crate::frame::group_by::*;4use crate::prelude::*;56impl private::PrivateSeries for SeriesWrap<BooleanChunked> {7fn compute_len(&mut self) {8self.0.compute_len()9}10fn _field(&self) -> Cow<'_, Field> {11Cow::Borrowed(self.0.ref_field())12}13fn _dtype(&self) -> &DataType {14self.0.ref_field().dtype()15}16fn _get_flags(&self) -> StatisticsFlags {17self.0.get_flags()18}19fn _set_flags(&mut self, flags: StatisticsFlags) {20self.0.set_flags(flags)21}2223unsafe fn equal_element(&self, idx_self: usize, idx_other: usize, other: &Series) -> bool {24self.0.equal_element(idx_self, idx_other, other)25}2627#[cfg(feature = "zip_with")]28fn zip_with_same_type(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {29ChunkZip::zip_with(&self.0, mask, other.as_ref().as_ref()).map(|ca| ca.into_series())30}31fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {32(&self.0).into_total_eq_inner()33}34fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {35(&self.0).into_total_ord_inner()36}3738fn vec_hash(39&self,40random_state: PlSeedableRandomStateQuality,41buf: &mut Vec<u64>,42) -> PolarsResult<()> {43self.0.vec_hash(random_state, buf)?;44Ok(())45}4647fn vec_hash_combine(48&self,49build_hasher: PlSeedableRandomStateQuality,50hashes: &mut [u64],51) -> PolarsResult<()> {52self.0.vec_hash_combine(build_hasher, hashes)?;53Ok(())54}5556#[cfg(feature = "algorithm_group_by")]57unsafe fn agg_min(&self, groups: &GroupsType) -> Series {58self.0.agg_min(groups)59}6061#[cfg(feature = "algorithm_group_by")]62unsafe fn agg_max(&self, groups: &GroupsType) -> Series {63self.0.agg_max(groups)64}6566#[cfg(feature = "algorithm_group_by")]67unsafe fn agg_sum(&self, groups: &GroupsType) -> Series {68self.0.agg_sum(groups)69}7071#[cfg(feature = "algorithm_group_by")]72unsafe fn agg_list(&self, groups: &GroupsType) -> Series {73self.0.agg_list(groups)74}75#[cfg(feature = "algorithm_group_by")]76unsafe fn agg_std(&self, groups: &GroupsType, _ddof: u8) -> Series {77self.078.cast_with_options(&DataType::Float64, CastOptions::Overflowing)79.unwrap()80.agg_std(groups, _ddof)81}82#[cfg(feature = "algorithm_group_by")]83unsafe fn agg_var(&self, groups: &GroupsType, _ddof: u8) -> Series {84self.085.cast_with_options(&DataType::Float64, CastOptions::Overflowing)86.unwrap()87.agg_var(groups, _ddof)88}8990#[cfg(feature = "bitwise")]91unsafe fn agg_and(&self, groups: &GroupsType) -> Series {92self.0.agg_and(groups).into_series()93}94#[cfg(feature = "bitwise")]95unsafe fn agg_or(&self, groups: &GroupsType) -> Series {96self.0.agg_or(groups).into_series()97}98#[cfg(feature = "bitwise")]99unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {100self.0.agg_xor(groups).into_series()101}102103#[cfg(feature = "algorithm_group_by")]104fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {105IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)106}107108fn arg_sort_multiple(109&self,110by: &[Column],111options: &SortMultipleOptions,112) -> PolarsResult<IdxCa> {113self.0.arg_sort_multiple(by, options)114}115fn add_to(&self, rhs: &Series) -> PolarsResult<Series> {116NumOpsDispatch::add_to(&self.0, rhs)117}118}119120impl SeriesTrait for SeriesWrap<BooleanChunked> {121fn rename(&mut self, name: PlSmallStr) {122self.0.rename(name);123}124125fn chunk_lengths(&self) -> ChunkLenIter<'_> {126self.0.chunk_lengths()127}128fn name(&self) -> &PlSmallStr {129self.0.name()130}131132fn chunks(&self) -> &Vec<ArrayRef> {133self.0.chunks()134}135unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {136self.0.chunks_mut()137}138fn shrink_to_fit(&mut self) {139self.0.shrink_to_fit()140}141142fn slice(&self, offset: i64, length: usize) -> Series {143self.0.slice(offset, length).into_series()144}145fn split_at(&self, offset: i64) -> (Series, Series) {146let (a, b) = self.0.split_at(offset);147(a.into_series(), b.into_series())148}149150fn append(&mut self, other: &Series) -> PolarsResult<()> {151polars_ensure!(self.0.dtype() == other.dtype(), append);152self.0.append(other.as_ref().as_ref())?;153Ok(())154}155fn append_owned(&mut self, other: Series) -> PolarsResult<()> {156polars_ensure!(self.0.dtype() == other.dtype(), append);157self.0.append_owned(other.take_inner())158}159160fn extend(&mut self, other: &Series) -> PolarsResult<()> {161polars_ensure!(self.0.dtype() == other.dtype(), extend);162self.0.extend(other.as_ref().as_ref())?;163Ok(())164}165166fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {167ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())168}169170fn _sum_as_f64(&self) -> f64 {171self.0.sum().unwrap() as f64172}173174fn mean(&self) -> Option<f64> {175self.0.mean()176}177178fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {179Ok(self.0.take(indices)?.into_series())180}181182unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {183self.0.take_unchecked(indices).into_series()184}185186fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {187Ok(self.0.take(indices)?.into_series())188}189190unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {191self.0.take_unchecked(indices).into_series()192}193194fn deposit(&self, validity: &Bitmap) -> Series {195self.0.deposit(validity).into_series()196}197198fn len(&self) -> usize {199self.0.len()200}201202fn rechunk(&self) -> Series {203self.0.rechunk().into_owned().into_series()204}205206fn new_from_index(&self, index: usize, length: usize) -> Series {207ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()208}209210fn cast(&self, dtype: &DataType, options: CastOptions) -> PolarsResult<Series> {211self.0.cast_with_options(dtype, options)212}213214#[inline]215unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {216self.0.get_any_value_unchecked(index)217}218219fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {220Ok(ChunkSort::sort_with(&self.0, options).into_series())221}222223fn arg_sort(&self, options: SortOptions) -> IdxCa {224ChunkSort::arg_sort(&self.0, options)225}226227fn null_count(&self) -> usize {228self.0.null_count()229}230231fn has_nulls(&self) -> bool {232self.0.has_nulls()233}234235#[cfg(feature = "algorithm_group_by")]236fn unique(&self) -> PolarsResult<Series> {237ChunkUnique::unique(&self.0).map(|ca| ca.into_series())238}239240#[cfg(feature = "algorithm_group_by")]241fn n_unique(&self) -> PolarsResult<usize> {242ChunkUnique::n_unique(&self.0)243}244245#[cfg(feature = "algorithm_group_by")]246fn arg_unique(&self) -> PolarsResult<IdxCa> {247ChunkUnique::arg_unique(&self.0)248}249250fn unique_id(&self) -> PolarsResult<(IdxSize, Vec<IdxSize>)> {251ChunkUnique::unique_id(&self.0)252}253254fn is_null(&self) -> BooleanChunked {255self.0.is_null()256}257258fn is_not_null(&self) -> BooleanChunked {259self.0.is_not_null()260}261262fn reverse(&self) -> Series {263ChunkReverse::reverse(&self.0).into_series()264}265266fn as_single_ptr(&mut self) -> PolarsResult<usize> {267self.0.as_single_ptr()268}269270fn shift(&self, periods: i64) -> Series {271ChunkShift::shift(&self.0, periods).into_series()272}273274fn sum_reduce(&self) -> PolarsResult<Scalar> {275Ok(ChunkAggSeries::sum_reduce(&self.0))276}277fn max_reduce(&self) -> PolarsResult<Scalar> {278Ok(ChunkAggSeries::max_reduce(&self.0))279}280fn min_reduce(&self) -> PolarsResult<Scalar> {281Ok(ChunkAggSeries::min_reduce(&self.0))282}283fn mean_reduce(&self) -> PolarsResult<Scalar> {284Ok(Scalar::new(DataType::Float64, self.mean().into()))285}286fn median_reduce(&self) -> PolarsResult<Scalar> {287let ca = self288.0289.cast_with_options(&DataType::Int8, CastOptions::Overflowing)290.unwrap();291let sc = ca.median_reduce()?;292let v = sc.value().cast(&DataType::Float64);293Ok(Scalar::new(DataType::Float64, v))294}295/// Get the variance of the Series as a new Series of length 1.296fn var_reduce(&self, _ddof: u8) -> PolarsResult<Scalar> {297let ca = self298.0299.cast_with_options(&DataType::Int8, CastOptions::Overflowing)300.unwrap();301let sc = ca.var_reduce(_ddof)?;302let v = sc.value().cast(&DataType::Float64);303Ok(Scalar::new(DataType::Float64, v))304}305/// Get the standard deviation of the Series as a new Series of length 1.306fn std_reduce(&self, _ddof: u8) -> PolarsResult<Scalar> {307let ca = self308.0309.cast_with_options(&DataType::Int8, CastOptions::Overflowing)310.unwrap();311let sc = ca.std_reduce(_ddof)?;312let v = sc.value().cast(&DataType::Float64);313Ok(Scalar::new(DataType::Float64, v))314}315fn and_reduce(&self) -> PolarsResult<Scalar> {316let dt = DataType::Boolean;317318Ok(Scalar::new(319dt,320self.0321.downcast_iter()322.filter(|arr| !arr.is_empty())323.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_and)324.reduce(|a, b| a & b)325.map_or(AnyValue::Null, Into::into),326))327}328fn or_reduce(&self) -> PolarsResult<Scalar> {329let dt = DataType::Boolean;330331Ok(Scalar::new(332dt,333self.0334.downcast_iter()335.filter(|arr| !arr.is_empty())336.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_or)337.reduce(|a, b| a | b)338.map_or(AnyValue::Null, Into::into),339))340}341fn xor_reduce(&self) -> PolarsResult<Scalar> {342let dt = DataType::Boolean;343344Ok(Scalar::new(345dt,346self.0347.downcast_iter()348.filter(|arr| !arr.is_empty())349.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_xor)350.reduce(|a, b| a ^ b)351.map_or(AnyValue::Null, Into::into),352))353}354355#[cfg(feature = "approx_unique")]356fn approx_n_unique(&self) -> PolarsResult<IdxSize> {357Ok(ChunkApproxNUnique::approx_n_unique(&self.0))358}359360fn clone_inner(&self) -> Arc<dyn SeriesTrait> {361Arc::new(SeriesWrap(Clone::clone(&self.0)))362}363364fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {365self.0.find_validity_mismatch(other, idxs)366}367368fn as_any(&self) -> &dyn Any {369&self.0370}371372fn as_any_mut(&mut self) -> &mut dyn Any {373&mut self.0374}375376fn as_phys_any(&self) -> &dyn Any {377&self.0378}379380fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {381self as _382}383}384385386