Path: blob/main/crates/polars-core/src/series/implementations/boolean.rs
6940 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)93}94#[cfg(feature = "bitwise")]95unsafe fn agg_or(&self, groups: &GroupsType) -> Series {96self.0.agg_or(groups)97}98#[cfg(feature = "bitwise")]99unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {100self.0.agg_xor(groups)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 len(&self) -> usize {195self.0.len()196}197198fn rechunk(&self) -> Series {199self.0.rechunk().into_owned().into_series()200}201202fn new_from_index(&self, index: usize, length: usize) -> Series {203ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()204}205206fn cast(&self, dtype: &DataType, options: CastOptions) -> PolarsResult<Series> {207self.0.cast_with_options(dtype, options)208}209210#[inline]211unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {212self.0.get_any_value_unchecked(index)213}214215fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {216Ok(ChunkSort::sort_with(&self.0, options).into_series())217}218219fn arg_sort(&self, options: SortOptions) -> IdxCa {220ChunkSort::arg_sort(&self.0, options)221}222223fn null_count(&self) -> usize {224self.0.null_count()225}226227fn has_nulls(&self) -> bool {228self.0.has_nulls()229}230231#[cfg(feature = "algorithm_group_by")]232fn unique(&self) -> PolarsResult<Series> {233ChunkUnique::unique(&self.0).map(|ca| ca.into_series())234}235236#[cfg(feature = "algorithm_group_by")]237fn n_unique(&self) -> PolarsResult<usize> {238ChunkUnique::n_unique(&self.0)239}240241#[cfg(feature = "algorithm_group_by")]242fn arg_unique(&self) -> PolarsResult<IdxCa> {243ChunkUnique::arg_unique(&self.0)244}245246fn is_null(&self) -> BooleanChunked {247self.0.is_null()248}249250fn is_not_null(&self) -> BooleanChunked {251self.0.is_not_null()252}253254fn reverse(&self) -> Series {255ChunkReverse::reverse(&self.0).into_series()256}257258fn as_single_ptr(&mut self) -> PolarsResult<usize> {259self.0.as_single_ptr()260}261262fn shift(&self, periods: i64) -> Series {263ChunkShift::shift(&self.0, periods).into_series()264}265266fn sum_reduce(&self) -> PolarsResult<Scalar> {267Ok(ChunkAggSeries::sum_reduce(&self.0))268}269fn max_reduce(&self) -> PolarsResult<Scalar> {270Ok(ChunkAggSeries::max_reduce(&self.0))271}272fn min_reduce(&self) -> PolarsResult<Scalar> {273Ok(ChunkAggSeries::min_reduce(&self.0))274}275fn median_reduce(&self) -> PolarsResult<Scalar> {276let ca = self277.0278.cast_with_options(&DataType::Int8, CastOptions::Overflowing)279.unwrap();280let sc = ca.median_reduce()?;281let v = sc.value().cast(&DataType::Float64);282Ok(Scalar::new(DataType::Float64, v))283}284/// Get the variance of the Series as a new Series of length 1.285fn var_reduce(&self, _ddof: u8) -> PolarsResult<Scalar> {286let ca = self287.0288.cast_with_options(&DataType::Int8, CastOptions::Overflowing)289.unwrap();290let sc = ca.var_reduce(_ddof)?;291let v = sc.value().cast(&DataType::Float64);292Ok(Scalar::new(DataType::Float64, v))293}294/// Get the standard deviation of the Series as a new Series of length 1.295fn std_reduce(&self, _ddof: u8) -> PolarsResult<Scalar> {296let ca = self297.0298.cast_with_options(&DataType::Int8, CastOptions::Overflowing)299.unwrap();300let sc = ca.std_reduce(_ddof)?;301let v = sc.value().cast(&DataType::Float64);302Ok(Scalar::new(DataType::Float64, v))303}304fn and_reduce(&self) -> PolarsResult<Scalar> {305let dt = DataType::Boolean;306307Ok(Scalar::new(308dt,309self.0310.downcast_iter()311.filter(|arr| !arr.is_empty())312.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_and)313.reduce(|a, b| a & b)314.map_or(AnyValue::Null, Into::into),315))316}317fn or_reduce(&self) -> PolarsResult<Scalar> {318let dt = DataType::Boolean;319320Ok(Scalar::new(321dt,322self.0323.downcast_iter()324.filter(|arr| !arr.is_empty())325.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_or)326.reduce(|a, b| a | b)327.map_or(AnyValue::Null, Into::into),328))329}330fn xor_reduce(&self) -> PolarsResult<Scalar> {331let dt = DataType::Boolean;332333Ok(Scalar::new(334dt,335self.0336.downcast_iter()337.filter(|arr| !arr.is_empty())338.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_xor)339.reduce(|a, b| a ^ b)340.map_or(AnyValue::Null, Into::into),341))342}343344#[cfg(feature = "approx_unique")]345fn approx_n_unique(&self) -> PolarsResult<IdxSize> {346Ok(ChunkApproxNUnique::approx_n_unique(&self.0))347}348349fn clone_inner(&self) -> Arc<dyn SeriesTrait> {350Arc::new(SeriesWrap(Clone::clone(&self.0)))351}352353fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {354self.0.find_validity_mismatch(other, idxs)355}356357fn as_any(&self) -> &dyn Any {358&self.0359}360361fn as_any_mut(&mut self) -> &mut dyn Any {362&mut self.0363}364365fn as_phys_any(&self) -> &dyn Any {366&self.0367}368369fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {370self as _371}372}373374375