Path: blob/main/crates/polars-core/src/frame/column/compare.rs
6940 views
use polars_error::PolarsResult;12use super::{BooleanChunked, ChunkCompareEq, ChunkCompareIneq, ChunkExpandAtIndex, Column, Series};34macro_rules! column_element_wise_broadcasting {5($lhs:expr, $rhs:expr, $op:expr) => {6match ($lhs, $rhs) {7(Column::Series(lhs), Column::Scalar(rhs)) => $op(lhs, &rhs.as_single_value_series()),8(Column::Scalar(lhs), Column::Series(rhs)) => $op(&lhs.as_single_value_series(), rhs),9(Column::Scalar(lhs), Column::Scalar(rhs)) => {10$op(&lhs.as_single_value_series(), &rhs.as_single_value_series()).map(|ca| {11if ca.len() == 0 {12ca13} else {14ca.new_from_index(0, lhs.len())15}16})17},18(lhs, rhs) => $op(lhs.as_materialized_series(), rhs.as_materialized_series()),19}20};21}2223impl ChunkCompareEq<&Column> for Column {24type Item = PolarsResult<BooleanChunked>;2526/// Create a boolean mask by checking for equality.27#[inline]28fn equal(&self, rhs: &Column) -> PolarsResult<BooleanChunked> {29column_element_wise_broadcasting!(self, rhs, <Series as ChunkCompareEq<&Series>>::equal)30}3132/// Create a boolean mask by checking for equality.33#[inline]34fn equal_missing(&self, rhs: &Column) -> PolarsResult<BooleanChunked> {35column_element_wise_broadcasting!(36self,37rhs,38<Series as ChunkCompareEq<&Series>>::equal_missing39)40}4142/// Create a boolean mask by checking for inequality.43#[inline]44fn not_equal(&self, rhs: &Column) -> PolarsResult<BooleanChunked> {45column_element_wise_broadcasting!(self, rhs, <Series as ChunkCompareEq<&Series>>::not_equal)46}4748/// Create a boolean mask by checking for inequality.49#[inline]50fn not_equal_missing(&self, rhs: &Column) -> PolarsResult<BooleanChunked> {51column_element_wise_broadcasting!(52self,53rhs,54<Series as ChunkCompareEq<&Series>>::not_equal_missing55)56}57}5859impl ChunkCompareIneq<&Column> for Column {60type Item = PolarsResult<BooleanChunked>;6162/// Create a boolean mask by checking if self > rhs.63#[inline]64fn gt(&self, rhs: &Column) -> PolarsResult<BooleanChunked> {65column_element_wise_broadcasting!(self, rhs, <Series as ChunkCompareIneq<&Series>>::gt)66}6768/// Create a boolean mask by checking if self >= rhs.69#[inline]70fn gt_eq(&self, rhs: &Column) -> PolarsResult<BooleanChunked> {71column_element_wise_broadcasting!(self, rhs, <Series as ChunkCompareIneq<&Series>>::gt_eq)72}7374/// Create a boolean mask by checking if self < rhs.75#[inline]76fn lt(&self, rhs: &Column) -> PolarsResult<BooleanChunked> {77column_element_wise_broadcasting!(self, rhs, <Series as ChunkCompareIneq<&Series>>::lt)78}7980/// Create a boolean mask by checking if self <= rhs.81#[inline]82fn lt_eq(&self, rhs: &Column) -> PolarsResult<BooleanChunked> {83column_element_wise_broadcasting!(self, rhs, <Series as ChunkCompareIneq<&Series>>::lt_eq)84}85}868788