-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(rust): Move ewma to polars-ops (#11794)
- Loading branch information
Showing
9 changed files
with
113 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,3 +113,4 @@ convert_index = [] | |
repeat_by = [] | ||
peaks = [] | ||
cum_agg = [] | ||
ewma = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters