-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
155 additions
and
53 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 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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use crate::prelude::*; | ||
|
||
impl Series { | ||
/// Round underlying floating point array to given decimal. | ||
#[cfg_attr(docsrs, doc(cfg(feature = "round_series")))] | ||
pub fn round(&self, decimals: u32) -> Result<Self> { | ||
use num::traits::Pow; | ||
if let Ok(ca) = self.f32() { | ||
let multiplier = 10.0.pow(decimals as f32) as f32; | ||
let s = ca | ||
.apply(|val| (val * multiplier).round() / multiplier) | ||
.into_series(); | ||
return Ok(s); | ||
} | ||
if let Ok(ca) = self.f64() { | ||
let multiplier = 10.0.pow(decimals as f32) as f64; | ||
let s = ca | ||
.apply(|val| (val * multiplier).round() / multiplier) | ||
.into_series(); | ||
return Ok(s); | ||
} | ||
Err(PolarsError::SchemaMisMatch( | ||
format!("{:?} is not a floating point datatype", self.dtype()).into(), | ||
)) | ||
} | ||
|
||
#[cfg_attr(docsrs, doc(cfg(feature = "round_series")))] | ||
/// Floor underlying floating point array to the lowest integers smaller or equal to the float value. | ||
pub fn floor(&self) -> Result<Self> { | ||
if let Ok(ca) = self.f32() { | ||
let s = ca.apply(|val| val.floor()).into_series(); | ||
return Ok(s); | ||
} | ||
if let Ok(ca) = self.f64() { | ||
let s = ca.apply(|val| val.floor()).into_series(); | ||
return Ok(s); | ||
} | ||
Err(PolarsError::SchemaMisMatch( | ||
format!("{:?} is not a floating point datatype", self.dtype()).into(), | ||
)) | ||
} | ||
|
||
#[cfg_attr(docsrs, doc(cfg(feature = "round_series")))] | ||
/// Ceil underlying floating point array to the heighest integers smaller or equal to the float value. | ||
pub fn ceil(&self) -> Result<Self> { | ||
if let Ok(ca) = self.f32() { | ||
let s = ca.apply(|val| val.ceil()).into_series(); | ||
return Ok(s); | ||
} | ||
if let Ok(ca) = self.f64() { | ||
let s = ca.apply(|val| val.ceil()).into_series(); | ||
return Ok(s); | ||
} | ||
Err(PolarsError::SchemaMisMatch( | ||
format!("{:?} is not a floating point datatype", self.dtype()).into(), | ||
)) | ||
} | ||
|
||
#[cfg_attr(docsrs, doc(cfg(feature = "round_series")))] | ||
/// Ceil underlying floating point array to the heighest integers smaller or equal to the float value. | ||
pub fn clip(&self, min: f64, max: f64) -> Result<Self> { | ||
if let Ok(ca) = self.f32() { | ||
let min = min as f32; | ||
let max = max as f32; | ||
let s = ca.apply(|val| val.clamp(min, max)).into_series(); | ||
return Ok(s); | ||
} | ||
if let Ok(ca) = self.f64() { | ||
let s = ca.apply(|val| val.clamp(min, max)).into_series(); | ||
return Ok(s); | ||
} | ||
if let Ok(ca) = self.i64() { | ||
let min = min as i64; | ||
let max = max as i64; | ||
let s = ca.apply(|val| val.clamp(min, max)).into_series(); | ||
return Ok(s); | ||
} | ||
if let Ok(ca) = self.i32() { | ||
let min = min as i32; | ||
let max = max as i32; | ||
let s = ca.apply(|val| val.clamp(min, max)).into_series(); | ||
return Ok(s); | ||
} | ||
if let Ok(ca) = self.u32() { | ||
let min = min as u32; | ||
let max = max as u32; | ||
let s = ca.apply(|val| val.clamp(min, max)).into_series(); | ||
return Ok(s); | ||
} | ||
Err(PolarsError::SchemaMisMatch( | ||
format!("{:?} is not one of {{Float32, Float64, Int32, Int64, UInt32}} consider using a when -> then -> otherwise", self.dtype()).into(), | ||
)) | ||
} | ||
} |
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
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
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