Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(rust): Make all emw function expr non-anonymous #11638

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/polars-arrow/src/legacy/kernels/ewm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ mod average;
mod variance;

pub use average::*;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub use variance::*;

#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq)]
#[must_use]
pub struct EWMOptions {
pub alpha: f64,
Expand Down
13 changes: 13 additions & 0 deletions crates/polars-plan/src/dsl/function_expr/ewm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::*;

pub(super) fn ewm_mean(s: &Series, options: EWMOptions) -> PolarsResult<Series> {
s.ewm_mean(options)
}

pub(super) fn ewm_std(s: &Series, options: EWMOptions) -> PolarsResult<Series> {
s.ewm_std(options)
}

pub(super) fn ewm_var(s: &Series, options: EWMOptions) -> PolarsResult<Series> {
s.ewm_var(options)
}
26 changes: 26 additions & 0 deletions crates/polars-plan/src/dsl/function_expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ mod cum;
#[cfg(feature = "temporal")]
mod datetime;
mod dispatch;
#[cfg(feature = "ewma")]
mod ewm;
romanovacca marked this conversation as resolved.
Show resolved Hide resolved
mod fill_null;
#[cfg(feature = "fused")]
mod fused;
Expand Down Expand Up @@ -259,6 +261,18 @@ pub enum FunctionExpr {
SumHorizontal,
MaxHorizontal,
MinHorizontal,
#[cfg(feature = "ewma")]
EwmMean {
options: EWMOptions,
},
#[cfg(feature = "ewma")]
EwmStd {
options: EWMOptions,
},
#[cfg(feature = "ewma")]
EwmVar {
options: EWMOptions,
},
}

impl Hash for FunctionExpr {
Expand Down Expand Up @@ -433,6 +447,12 @@ impl Display for FunctionExpr {
SumHorizontal => "sum_horizontal",
MaxHorizontal => "max_horizontal",
MinHorizontal => "min_horizontal",
#[cfg(feature = "ewma")]
EwmMean { .. } => "ewm_mean",
romanovacca marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "ewma")]
EwmStd { .. } => "ewm_std",
#[cfg(feature = "ewma")]
EwmVar { .. } => "ewm_var",
};
write!(f, "{s}")
}
Expand Down Expand Up @@ -755,6 +775,12 @@ impl From<FunctionExpr> for SpecialEq<Arc<dyn SeriesUdf>> {
SumHorizontal => map_as_slice!(dispatch::sum_horizontal),
MaxHorizontal => wrap!(dispatch::max_horizontal),
MinHorizontal => wrap!(dispatch::min_horizontal),
#[cfg(feature = "ewma")]
EwmMean { options } => map!(ewm::ewm_mean, options),
#[cfg(feature = "ewma")]
EwmStd { options } => map!(ewm::ewm_std, options),
#[cfg(feature = "ewma")]
EwmVar { options } => map!(ewm::ewm_var, options),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/polars-plan/src/dsl/function_expr/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ impl FunctionExpr {
SumHorizontal => mapper.map_to_supertype(),
MaxHorizontal => mapper.map_to_supertype(),
MinHorizontal => mapper.map_to_supertype(),
#[cfg(feature = "ewma")]
EwmMean { .. } => mapper.map_to_float_dtype(),
#[cfg(feature = "ewma")]
EwmStd { .. } => mapper.map_to_float_dtype(),
#[cfg(feature = "ewma")]
EwmVar { .. } => mapper.map_to_float_dtype(),
}
}
}
Expand Down
30 changes: 3 additions & 27 deletions crates/polars-plan/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,43 +1666,19 @@ impl Expr {
#[cfg(feature = "ewma")]
/// Calculate the exponentially-weighted moving average.
pub fn ewm_mean(self, options: EWMOptions) -> Self {
use DataType::*;
self.apply(
move |s| s.ewm_mean(options).map(Some),
GetOutput::map_dtype(|dt| match dt {
Float64 | Float32 => dt.clone(),
_ => Float64,
}),
)
.with_fmt("ewm_mean")
self.apply_private(FunctionExpr::EwmMean { options })
}

#[cfg(feature = "ewma")]
/// Calculate the exponentially-weighted moving standard deviation.
pub fn ewm_std(self, options: EWMOptions) -> Self {
use DataType::*;
self.apply(
move |s| s.ewm_std(options).map(Some),
GetOutput::map_dtype(|dt| match dt {
Float64 | Float32 => dt.clone(),
_ => Float64,
}),
)
.with_fmt("ewm_std")
self.apply_private(FunctionExpr::EwmStd { options })
}

#[cfg(feature = "ewma")]
/// Calculate the exponentially-weighted moving variance.
pub fn ewm_var(self, options: EWMOptions) -> Self {
use DataType::*;
self.apply(
move |s| s.ewm_var(options).map(Some),
GetOutput::map_dtype(|dt| match dt {
Float64 | Float32 => dt.clone(),
_ => Float64,
}),
)
.with_fmt("ewm_var")
self.apply_private(FunctionExpr::EwmVar { options })
}

/// Returns whether any of the values in the column are `true`.
Expand Down