From 395fdb0efe4255d31f08e891455240b0c38034a4 Mon Sep 17 00:00:00 2001 From: coastalwhite Date: Tue, 24 Sep 2024 16:47:13 +0200 Subject: [PATCH] add missing file --- .../polars-core/src/frame/column/compare.rs | 86 +++++++++++++++++++ crates/polars-core/src/frame/column/mod.rs | 2 +- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 crates/polars-core/src/frame/column/compare.rs diff --git a/crates/polars-core/src/frame/column/compare.rs b/crates/polars-core/src/frame/column/compare.rs new file mode 100644 index 000000000000..fdb792d60074 --- /dev/null +++ b/crates/polars-core/src/frame/column/compare.rs @@ -0,0 +1,86 @@ +use polars_error::PolarsResult; + +use super::{BooleanChunked, ChunkCompareEq, ChunkCompareIneq, ChunkExpandAtIndex, Column, Series}; + +macro_rules! column_element_wise_broadcasting { + ($lhs:expr, $rhs:expr, $op:expr) => { + match ($lhs, $rhs) { + (Column::Series(lhs), Column::Series(rhs)) => $op(lhs, rhs), + (Column::Series(lhs), Column::Scalar(rhs)) => $op(lhs, &rhs.as_single_value_series()), + (Column::Scalar(lhs), Column::Series(rhs)) => $op(&lhs.as_single_value_series(), rhs), + (Column::Scalar(lhs), Column::Scalar(rhs)) => { + $op(&lhs.as_single_value_series(), &rhs.as_single_value_series()).map(|ca| { + if ca.len() == 0 { + ca + } else { + ca.new_from_index(0, lhs.len()) + } + }) + }, + } + }; +} + +impl ChunkCompareEq<&Column> for Column { + type Item = PolarsResult; + + /// Create a boolean mask by checking for equality. + #[inline] + fn equal(&self, rhs: &Column) -> PolarsResult { + column_element_wise_broadcasting!(self, rhs, >::equal) + } + + /// Create a boolean mask by checking for equality. + #[inline] + fn equal_missing(&self, rhs: &Column) -> PolarsResult { + column_element_wise_broadcasting!( + self, + rhs, + >::equal_missing + ) + } + + /// Create a boolean mask by checking for inequality. + #[inline] + fn not_equal(&self, rhs: &Column) -> PolarsResult { + column_element_wise_broadcasting!(self, rhs, >::not_equal) + } + + /// Create a boolean mask by checking for inequality. + #[inline] + fn not_equal_missing(&self, rhs: &Column) -> PolarsResult { + column_element_wise_broadcasting!( + self, + rhs, + >::not_equal_missing + ) + } +} + +impl ChunkCompareIneq<&Column> for Column { + type Item = PolarsResult; + + /// Create a boolean mask by checking if self > rhs. + #[inline] + fn gt(&self, rhs: &Column) -> PolarsResult { + column_element_wise_broadcasting!(self, rhs, >::gt) + } + + /// Create a boolean mask by checking if self >= rhs. + #[inline] + fn gt_eq(&self, rhs: &Column) -> PolarsResult { + column_element_wise_broadcasting!(self, rhs, >::gt_eq) + } + + /// Create a boolean mask by checking if self < rhs. + #[inline] + fn lt(&self, rhs: &Column) -> PolarsResult { + column_element_wise_broadcasting!(self, rhs, >::lt) + } + + /// Create a boolean mask by checking if self <= rhs. + #[inline] + fn lt_eq(&self, rhs: &Column) -> PolarsResult { + column_element_wise_broadcasting!(self, rhs, >::lt_eq) + } +} diff --git a/crates/polars-core/src/frame/column/mod.rs b/crates/polars-core/src/frame/column/mod.rs index 491f2ea4ed49..3a6343415a6a 100644 --- a/crates/polars-core/src/frame/column/mod.rs +++ b/crates/polars-core/src/frame/column/mod.rs @@ -16,8 +16,8 @@ use crate::utils::{slice_offsets, Container}; use crate::{HEAD_DEFAULT_LENGTH, TAIL_DEFAULT_LENGTH}; mod arithmetic; -mod scalar; mod compare; +mod scalar; /// A column within a [`DataFrame`]. ///