Skip to content

Commit

Permalink
refactor[rust]: remove agg_median from series trait (#4963)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 24, 2022
1 parent d960c03 commit d12be1c
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 29 deletions.
6 changes: 6 additions & 0 deletions polars/polars-core/src/datatypes/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ impl DataType {
self != &self.to_physical()
}

/// Check if this [`DataType`] is a temporal type
pub fn is_temporal(&self) -> bool {
use DataType::*;
matches!(self, Date | Datetime(_, _) | Duration(_) | Time)
}

/// Check if this [`DataType`] is a numeric type
pub fn is_numeric(&self) -> bool {
// allow because it cannot be replaced when object feature is activated
Expand Down
21 changes: 21 additions & 0 deletions polars/polars-core/src/frame/groupby/aggregations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,27 @@ impl Series {
}
}

#[doc(hidden)]
pub unsafe fn agg_median(&self, groups: &GroupsProxy) -> Series {
use DataType::*;

match self.dtype() {
Float32 => SeriesWrap(self.f32().unwrap().clone()).agg_median(groups),
Float64 => SeriesWrap(self.f64().unwrap().clone()).agg_median(groups),
dt if dt.is_numeric() || dt.is_temporal() => {
let ca = self.to_physical_repr();
let s = apply_method_physical_integer!(ca, agg_median, groups);
if dt.is_logical() {
// back to logical type
s.cast(dt).unwrap()
} else {
s
}
}
_ => Series::full_null("", groups.len(), self.dtype()),
}
}

#[doc(hidden)]
pub unsafe fn agg_mean(&self, groups: &GroupsProxy) -> Series {
use DataType::*;
Expand Down
4 changes: 0 additions & 4 deletions polars/polars-core/src/series/implementations/dates_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ macro_rules! impl_dyn_series {
.into_series()
}

unsafe fn agg_median(&self, groups: &GroupsProxy) -> Series {
self.0.agg_median(groups).$into_logical().into_series()
}

fn zip_outer_join_column(
&self,
right_column: &Series,
Expand Down
6 changes: 0 additions & 6 deletions polars/polars-core/src/series/implementations/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,6 @@ impl private::PrivateSeries for SeriesWrap<DatetimeChunked> {
.into_series()
}

unsafe fn agg_median(&self, groups: &GroupsProxy) -> Series {
self.0
.agg_median(groups)
.into_datetime(self.0.time_unit(), self.0.time_zone().clone())
.into_series()
}
fn zip_outer_join_column(
&self,
right_column: &Series,
Expand Down
10 changes: 0 additions & 10 deletions polars/polars-core/src/series/implementations/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,6 @@ impl private::PrivateSeries for SeriesWrap<DurationChunked> {
.into_series()
}

unsafe fn agg_median(&self, groups: &GroupsProxy) -> Series {
self.0
.agg_median(groups)
// cast f64 back to physical type
.cast(&DataType::Int64)
.unwrap()
.into_duration(self.0.time_unit())
.into_series()
}

fn zip_outer_join_column(
&self,
right_column: &Series,
Expand Down
3 changes: 0 additions & 3 deletions polars/polars-core/src/series/implementations/floats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ macro_rules! impl_dyn_series {
self.agg_quantile(groups, quantile, interpol)
}

unsafe fn agg_median(&self, groups: &GroupsProxy) -> Series {
self.agg_median(groups)
}
fn zip_outer_join_column(
&self,
right_column: &Series,
Expand Down
3 changes: 0 additions & 3 deletions polars/polars-core/src/series/implementations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,6 @@ macro_rules! impl_dyn_series {
self.0.agg_quantile(groups, quantile, interpol)
}

unsafe fn agg_median(&self, groups: &GroupsProxy) -> Series {
self.0.agg_median(groups)
}
fn zip_outer_join_column(
&self,
right_column: &Series,
Expand Down
4 changes: 1 addition & 3 deletions polars/polars-core/src/series/series_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ pub(crate) mod private {
) -> Series {
Series::full_null(self._field().name(), groups.len(), self._dtype())
}
unsafe fn agg_median(&self, groups: &GroupsProxy) -> Series {
Series::full_null(self._field().name(), groups.len(), self._dtype())
}

fn zip_outer_join_column(
&self,
_right_column: &Series,
Expand Down

0 comments on commit d12be1c

Please sign in to comment.