Skip to content

Commit

Permalink
move cumsum/cumprod from series trait to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jan 8, 2022
1 parent 92cf2d2 commit 0465717
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 50 deletions.
5 changes: 0 additions & 5 deletions polars/polars-core/src/series/implementations/dates_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ macro_rules! impl_dyn_series {
self.0.cummin(reverse).$into_logical().into_series()
}

#[cfg(feature = "cum_agg")]
fn _cumsum(&self, _reverse: bool) -> Series {
panic!("cannot sum logical")
}

#[cfg(feature = "asof_join")]
fn join_asof(&self, other: &Series) -> Result<Vec<Option<u32>>> {
let other = other.to_physical_repr();
Expand Down
5 changes: 0 additions & 5 deletions polars/polars-core/src/series/implementations/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ impl private::PrivateSeries for SeriesWrap<DatetimeChunked> {
.into_series()
}

#[cfg(feature = "cum_agg")]
fn _cumsum(&self, _reverse: bool) -> Series {
panic!("cannot sum logical")
}

#[cfg(feature = "asof_join")]
fn join_asof(&self, other: &Series) -> Result<Vec<Option<u32>>> {
let other = other.to_physical_repr();
Expand Down
5 changes: 0 additions & 5 deletions polars/polars-core/src/series/implementations/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ impl private::PrivateSeries for SeriesWrap<DurationChunked> {
.into_series()
}

#[cfg(feature = "cum_agg")]
fn _cumsum(&self, _reverse: bool) -> Series {
panic!("cannot sum logical")
}

#[cfg(feature = "asof_join")]
fn join_asof(&self, other: &Series) -> Result<Vec<Option<u32>>> {
let other = other.to_physical_repr();
Expand Down
5 changes: 0 additions & 5 deletions polars/polars-core/src/series/implementations/floats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ macro_rules! impl_dyn_series {
self.0.cummin(reverse).into_series()
}

#[cfg(feature = "cum_agg")]
fn _cumsum(&self, reverse: bool) -> Series {
self.0.cumsum(reverse).into_series()
}

#[cfg(feature = "asof_join")]
fn join_asof(&self, other: &Series) -> Result<Vec<Option<u32>>> {
self.0.join_asof(other)
Expand Down
10 changes: 0 additions & 10 deletions polars/polars-core/src/series/implementations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,6 @@ macro_rules! impl_dyn_series {
self.0.cummin(reverse).into_series()
}

#[cfg(feature = "cum_agg")]
fn _cumsum(&self, reverse: bool) -> Series {
self.0.cumsum(reverse).into_series()
}

#[cfg(feature = "cum_agg")]
fn _cumprod(&self, reverse: bool) -> Series {
self.0.cumprod(reverse).into_series()
}

#[cfg(feature = "asof_join")]
fn join_asof(&self, other: &Series) -> Result<Vec<Option<u32>>> {
self.0.join_asof(other)
Expand Down
48 changes: 40 additions & 8 deletions polars/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,30 @@ impl Series {
/// If the [`DataType`] is one of `{Int8, UInt8, Int16, UInt16}` the `Series` is
/// first cast to `Int64` to prevent overflow issues.
#[cfg_attr(docsrs, doc(cfg(feature = "cum_agg")))]
pub fn cumsum(&self, _reverse: bool) -> Series {
#[allow(unused_variables)]
pub fn cumsum(&self, reverse: bool) -> Series {
#[cfg(feature = "cum_agg")]
{
use DataType::*;
match self.dtype() {
Boolean => self.cast(&DataType::UInt32).unwrap()._cumsum(_reverse),
Int8 | UInt8 | Int16 | UInt16 => self.cast(&Int64).unwrap()._cumsum(_reverse),
_ => self._cumsum(_reverse),
Boolean => self.cast(&DataType::Int64).unwrap().cumsum(reverse),
Int8 | UInt8 | Int16 | UInt16 => {
let s = self.cast(&Int64).unwrap();
s.cumsum(reverse)
}
Int64 => {
let ca = self.i64().unwrap();
ca.cumsum(reverse).into_series()
}
Float32 => {
let ca = self.f32().unwrap();
ca.cumsum(reverse).into_series()
}
Float64 => {
let ca = self.f64().unwrap();
ca.cumsum(reverse).into_series()
}
dt => panic!("cumsum not supported for dtype: {:?}", dt),
}
}
#[cfg(not(feature = "cum_agg"))]
Expand All @@ -554,14 +570,30 @@ impl Series {
/// If the [`DataType`] is one of `{Int8, UInt8, Int16, UInt16}` the `Series` is
/// first cast to `Int64` to prevent overflow issues.
#[cfg_attr(docsrs, doc(cfg(feature = "cum_agg")))]
pub fn cumprod(&self, _reverse: bool) -> Series {
#[allow(unused_variables)]
pub fn cumprod(&self, reverse: bool) -> Series {
#[cfg(feature = "cum_agg")]
{
use DataType::*;
match self.dtype() {
Boolean => self.cast(&UInt32).unwrap()._cumprod(_reverse),
Int8 | UInt8 | Int16 | UInt16 => self.cast(&Int64).unwrap()._cumprod(_reverse),
_ => self._cumprod(_reverse),
Boolean => self.cast(&DataType::Int64).unwrap().cumprod(reverse),
Int8 | UInt8 | Int16 | UInt16 => {
let s = self.cast(&Int64).unwrap();
s.cumprod(reverse)
}
Int64 => {
let ca = self.i64().unwrap();
ca.cumprod(reverse).into_series()
}
Float32 => {
let ca = self.f32().unwrap();
ca.cumprod(reverse).into_series()
}
Float64 => {
let ca = self.f64().unwrap();
ca.cumprod(reverse).into_series()
}
dt => panic!("cumprod not supported for dtype: {:?}", dt),
}
}
#[cfg(not(feature = "cum_agg"))]
Expand Down
12 changes: 0 additions & 12 deletions polars/polars-core/src/series/series_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,6 @@ pub(crate) mod private {
panic!("operation cummin not supported for this dtype")
}

/// Get an array with the cumulative sum computed at every element
#[cfg(feature = "cum_agg")]
fn _cumsum(&self, _reverse: bool) -> Series {
panic!("operation cumsum not supported for this dtype")
}

/// Get an array with the cumulative sum computed at every element
#[cfg(feature = "cum_agg")]
fn _cumprod(&self, _reverse: bool) -> Series {
panic!("operation cumprod not supported for this dtype")
}

#[cfg(feature = "asof_join")]
fn join_asof(&self, _other: &Series) -> Result<Vec<Option<u32>>> {
invalid_operation!(self)
Expand Down

0 comments on commit 0465717

Please sign in to comment.