Skip to content

Commit

Permalink
chore(rust): Move ewma to polars-ops (#11794)
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa authored Oct 18, 2023
1 parent 45009eb commit d24c508
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 112 deletions.
1 change: 0 additions & 1 deletion crates/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ moment = []
diagonal_concat = []
horizontal_concat = []
abs = []
ewma = []
dataframe_arithmetic = []
product = []
unique_counts = []
Expand Down
104 changes: 0 additions & 104 deletions crates/polars-core/src/series/ops/ewm.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/polars-core/src/series/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#[cfg(feature = "diff")]
pub mod diff;
mod downcast;
#[cfg(feature = "ewma")]
mod ewm;
mod extend;
#[cfg(feature = "moment")]
pub mod moment;
Expand Down
1 change: 1 addition & 0 deletions crates/polars-ops/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,4 @@ convert_index = []
repeat_by = []
peaks = []
cum_agg = []
ewma = []
103 changes: 103 additions & 0 deletions crates/polars-ops/src/series/ops/ewm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use std::convert::TryFrom;

pub use arrow::legacy::kernels::ewm::EWMOptions;
use arrow::legacy::kernels::ewm::{
ewm_mean as kernel_ewm_mean, ewm_std as kernel_ewm_std, ewm_var as kernel_ewm_var,
};
use polars_core::prelude::*;

fn check_alpha(alpha: f64) -> PolarsResult<()> {
polars_ensure!((0.0..=1.0).contains(&alpha), ComputeError: "alpha must be in [0; 1]");
Ok(())
}

pub fn ewm_mean(s: &Series, options: EWMOptions) -> PolarsResult<Series> {
check_alpha(options.alpha)?;
match s.dtype() {
DataType::Float32 => {
let xs = s.f32().unwrap();
let result = kernel_ewm_mean(
xs,
options.alpha as f32,
options.adjust,
options.min_periods,
options.ignore_nulls,
);
Series::try_from((s.name(), Box::new(result) as ArrayRef))
},
DataType::Float64 => {
let xs = s.f64().unwrap();
let result = kernel_ewm_mean(
xs,
options.alpha,
options.adjust,
options.min_periods,
options.ignore_nulls,
);
Series::try_from((s.name(), Box::new(result) as ArrayRef))
},
_ => ewm_mean(&s.cast(&DataType::Float64)?, options),
}
}

pub fn ewm_std(s: &Series, options: EWMOptions) -> PolarsResult<Series> {
check_alpha(options.alpha)?;
match s.dtype() {
DataType::Float32 => {
let xs = s.f32().unwrap();
let result = kernel_ewm_std(
xs,
options.alpha as f32,
options.adjust,
options.bias,
options.min_periods,
options.ignore_nulls,
);
Series::try_from((s.name(), Box::new(result) as ArrayRef))
},
DataType::Float64 => {
let xs = s.f64().unwrap();
let result = kernel_ewm_std(
xs,
options.alpha,
options.adjust,
options.bias,
options.min_periods,
options.ignore_nulls,
);
Series::try_from((s.name(), Box::new(result) as ArrayRef))
},
_ => ewm_std(&s.cast(&DataType::Float64)?, options),
}
}

pub fn ewm_var(s: &Series, options: EWMOptions) -> PolarsResult<Series> {
check_alpha(options.alpha)?;
match s.dtype() {
DataType::Float32 => {
let xs = s.f32().unwrap();
let result = kernel_ewm_var(
xs,
options.alpha as f32,
options.adjust,
options.bias,
options.min_periods,
options.ignore_nulls,
);
Series::try_from((s.name(), Box::new(result) as ArrayRef))
},
DataType::Float64 => {
let xs = s.f64().unwrap();
let result = kernel_ewm_var(
xs,
options.alpha,
options.adjust,
options.bias,
options.min_periods,
options.ignore_nulls,
);
Series::try_from((s.name(), Box::new(result) as ArrayRef))
},
_ => ewm_var(&s.cast(&DataType::Float64)?, options),
}
}
4 changes: 4 additions & 0 deletions crates/polars-ops/src/series/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mod clip;
mod cum_agg;
#[cfg(feature = "cutqcut")]
mod cut;
#[cfg(feature = "ewma")]
mod ewm;
#[cfg(feature = "round_series")]
mod floor_divide;
#[cfg(feature = "fused")]
Expand Down Expand Up @@ -47,6 +49,8 @@ pub use clip::*;
pub use cum_agg::*;
#[cfg(feature = "cutqcut")]
pub use cut::*;
#[cfg(feature = "ewma")]
pub use ewm::*;
#[cfg(feature = "round_series")]
pub use floor_divide::*;
#[cfg(feature = "fused")]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-plan/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ moment = ["polars-core/moment", "polars-ops/moment"]
abs = ["polars-core/abs"]
random = ["polars-core/random"]
dynamic_group_by = ["polars-core/dynamic_group_by"]
ewma = ["polars-core/ewma"]
ewma = ["polars-ops/ewma"]
dot_diagram = []
unique_counts = ["polars-core/unique_counts"]
log = ["polars-ops/log"]
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-plan/src/dsl/function_expr/ewm.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::*;

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

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

pub(super) fn ewm_var(s: &Series, options: EWMOptions) -> PolarsResult<Series> {
s.ewm_var(options)
polars_ops::prelude::ewm_var(s, options)
}
2 changes: 1 addition & 1 deletion crates/polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ diagonal_concat = ["polars-core/diagonal_concat", "polars-lazy?/diagonal_concat"
horizontal_concat = ["polars-core/horizontal_concat"]
abs = ["polars-core/abs", "polars-lazy?/abs"]
dynamic_group_by = ["polars-core/dynamic_group_by", "polars-lazy?/dynamic_group_by"]
ewma = ["polars-core/ewma", "polars-lazy?/ewma"]
ewma = ["polars-ops/ewma", "polars-lazy?/ewma"]
dot_diagram = ["polars-lazy?/dot_diagram"]
dataframe_arithmetic = ["polars-core/dataframe_arithmetic"]
product = ["polars-core/product"]
Expand Down

0 comments on commit d24c508

Please sign in to comment.