Skip to content

Commit

Permalink
remove mean_as_series from series_trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Feb 4, 2022
1 parent 7ea47cb commit 53365be
Show file tree
Hide file tree
Showing 20 changed files with 34 additions and 72 deletions.
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/categorical/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ impl CategoricalChunked {
}

#[cfg(test)]
#[cfg(feature = "single_thread")]
mod test {
use super::*;
use crate::chunked_array::categorical::CategoricalChunkedBuilder;
use crate::{reset_string_cache, toggle_string_cache};

#[test]
#[cfg(feature = "single_thread")]
fn test_merge_rev_map() {
reset_string_cache();
toggle_string_cache(true);
Expand Down
32 changes: 13 additions & 19 deletions polars/polars-core/src/chunked_array/ops/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ pub trait ChunkAggSeries {
fn min_as_series(&self) -> Series {
unimplemented!()
}
/// Get the mean of the ChunkedArray as a new Series of length 1.
fn mean_as_series(&self) -> Series {
unimplemented!()
}
/// Get the product of the ChunkedArray as a new Series of length 1.
fn prod_as_series(&self) -> Series {
unimplemented!()
Expand Down Expand Up @@ -522,10 +518,6 @@ where
ca.rename(self.name());
ca.into_series()
}
fn mean_as_series(&self) -> Series {
let val = [self.mean()];
Series::new(self.name(), val)
}

fn prod_as_series(&self) -> Series {
let mut prod = None;
Expand Down Expand Up @@ -777,9 +769,6 @@ impl ChunkAggSeries for BooleanChunked {
ca.rename(self.name());
ca.into_series()
}
fn mean_as_series(&self) -> Series {
BooleanChunked::full_null(self.name(), 1).into_series()
}
}

macro_rules! one_null_utf8 {
Expand All @@ -800,9 +789,6 @@ impl ChunkAggSeries for Utf8Chunked {
fn min_as_series(&self) -> Series {
one_null_utf8!(self)
}
fn mean_as_series(&self) -> Series {
one_null_utf8!(self)
}
}

#[cfg(feature = "dtype-categorical")]
Expand All @@ -826,9 +812,6 @@ impl ChunkAggSeries for ListChunked {
fn min_as_series(&self) -> Series {
one_null_list!(self)
}
fn mean_as_series(&self) -> Series {
one_null_list!(self)
}
}

#[cfg(feature = "object")]
Expand Down Expand Up @@ -969,11 +952,22 @@ mod test {
let ca = Float32Chunked::new("", &[Some(1.0), Some(2.0), None]);
assert_eq!(ca.mean().unwrap(), 1.5);
// all mean_as_series are cast to f64.
assert_eq!(ca.mean_as_series().f64().unwrap().get(0).unwrap(), 1.5);
assert_eq!(
ca.into_series()
.mean_as_series()
.f64()
.unwrap()
.get(0)
.unwrap(),
1.5
);
// all null values case
let ca = Float32Chunked::full_null("", 3);
assert_eq!(ca.mean(), None);
assert_eq!(ca.mean_as_series().f64().unwrap().get(0), None);
assert_eq!(
ca.into_series().mean_as_series().f64().unwrap().get(0),
None
);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions polars/polars-core/src/frame/groupby/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1470,10 +1470,10 @@ mod test {
let ca = UInt32Chunked::new("", slice);
let split = split_ca(&ca, 4).unwrap();

let mut a = groupby(ca.into_iter(), true).into_idx();
let a = groupby(ca.into_iter(), true).into_idx();

let keys = split.iter().map(|ca| ca.cont_slice().unwrap()).collect();
let mut b = groupby_threaded_num(keys, 0, split.len() as u64, true).into_idx();
let b = groupby_threaded_num(keys, 0, split.len() as u64, true).into_idx();

assert_eq!(a, b);
}
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use crate::{
df,
error::{PolarsError, Result},
frame::{groupby::GroupsProxy, hash_join::JoinType, *},
named_from::NamedFrom,
named_from::{NamedFrom, NamedFromOwned},
series::{
arithmetic::{LhsNumOps, NumOpsDispatch},
IntoSeries, Series, SeriesTrait,
Expand Down
3 changes: 0 additions & 3 deletions polars/polars-core/src/series/implementations/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,6 @@ impl SeriesTrait for SeriesWrap<BooleanChunked> {
fn min_as_series(&self) -> Series {
ChunkAggSeries::min_as_series(&self.0)
}
fn mean_as_series(&self) -> Series {
ChunkAggSeries::mean_as_series(&self.0)
}
fn median_as_series(&self) -> Series {
QuantileAggSeries::median_as_series(&self.0)
}
Expand Down
3 changes: 0 additions & 3 deletions polars/polars-core/src/series/implementations/categorical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,6 @@ impl SeriesTrait for SeriesWrap<CategoricalChunked> {
fn min_as_series(&self) -> Series {
CategoricalChunked::full_null(self.name(), 1).into_series()
}
fn mean_as_series(&self) -> Series {
CategoricalChunked::full_null(self.name(), 1).into_series()
}
fn median_as_series(&self) -> Series {
CategoricalChunked::full_null(self.name(), 1).into_series()
}
Expand Down
6 changes: 0 additions & 6 deletions polars/polars-core/src/series/implementations/dates_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,6 @@ macro_rules! impl_dyn_series {
fn min_as_series(&self) -> Series {
self.0.min_as_series().$into_logical()
}
fn mean_as_series(&self) -> Series {
Int32Chunked::full_null(self.name(), 1)
.cast(self.dtype())
.unwrap()
.into()
}
fn median_as_series(&self) -> Series {
Int32Chunked::full_null(self.name(), 1)
.cast(self.dtype())
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 @@ -532,11 +532,6 @@ impl SeriesTrait for SeriesWrap<DatetimeChunked> {
.min_as_series()
.into_datetime(self.0.time_unit(), self.0.time_zone().clone())
}
fn mean_as_series(&self) -> Series {
Int32Chunked::full_null(self.name(), 1)
.cast(self.dtype())
.unwrap()
}
fn median_as_series(&self) -> Series {
Int32Chunked::full_null(self.name(), 1)
.cast(self.dtype())
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 @@ -492,11 +492,6 @@ impl SeriesTrait for SeriesWrap<DurationChunked> {
fn min_as_series(&self) -> Series {
self.0.min_as_series().into_duration(self.0.time_unit())
}
fn mean_as_series(&self) -> Series {
Int32Chunked::full_null(self.name(), 1)
.cast(self.dtype())
.unwrap()
}
fn median_as_series(&self) -> Series {
Int32Chunked::full_null(self.name(), 1)
.cast(self.dtype())
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 @@ -487,9 +487,6 @@ macro_rules! impl_dyn_series {
fn min_as_series(&self) -> Series {
ChunkAggSeries::min_as_series(&self.0)
}
fn mean_as_series(&self) -> Series {
ChunkAggSeries::mean_as_series(&self.0)
}
fn median_as_series(&self) -> Series {
QuantileAggSeries::median_as_series(&self.0)
}
Expand Down
3 changes: 0 additions & 3 deletions polars/polars-core/src/series/implementations/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ impl SeriesTrait for SeriesWrap<ListChunked> {
fn min_as_series(&self) -> Series {
ChunkAggSeries::min_as_series(&self.0)
}
fn mean_as_series(&self) -> Series {
ChunkAggSeries::mean_as_series(&self.0)
}
fn median_as_series(&self) -> Series {
QuantileAggSeries::median_as_series(&self.0)
}
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 @@ -654,9 +654,6 @@ macro_rules! impl_dyn_series {
fn min_as_series(&self) -> Series {
ChunkAggSeries::min_as_series(&self.0)
}
fn mean_as_series(&self) -> Series {
ChunkAggSeries::mean_as_series(&self.0)
}
fn median_as_series(&self) -> Series {
QuantileAggSeries::median_as_series(&self.0)
}
Expand Down
3 changes: 0 additions & 3 deletions polars/polars-core/src/series/implementations/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,6 @@ where
fn min_as_series(&self) -> Series {
ObjectChunked::<T>::full_null(self.name(), 1).into_series()
}
fn mean_as_series(&self) -> Series {
ObjectChunked::<T>::full_null(self.name(), 1).into_series()
}
fn median_as_series(&self) -> Series {
ObjectChunked::<T>::full_null(self.name(), 1).into_series()
}
Expand Down
3 changes: 0 additions & 3 deletions polars/polars-core/src/series/implementations/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,6 @@ impl SeriesTrait for SeriesWrap<Utf8Chunked> {
fn min_as_series(&self) -> Series {
ChunkAggSeries::min_as_series(&self.0)
}
fn mean_as_series(&self) -> Series {
ChunkAggSeries::mean_as_series(&self.0)
}
fn median_as_series(&self) -> Series {
QuantileAggSeries::median_as_series(&self.0)
}
Expand Down
10 changes: 10 additions & 0 deletions polars/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,16 @@ impl Series {
};
self.slice(-(len as i64), len)
}

pub fn mean_as_series(&self) -> Series {
let val = [self.mean()];
let s = Series::new(self.name(), val);
if !self.is_numeric() {
s.cast(self.dtype()).unwrap()
} else {
s
}
}
}

impl Deref for Series {
Expand Down
4 changes: 0 additions & 4 deletions polars/polars-core/src/series/series_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,6 @@ pub trait SeriesTrait:
fn min_as_series(&self) -> Series {
invalid_operation_panic!(self)
}
/// Get the mean of the Series as a new Series of length 1.
fn mean_as_series(&self) -> Series {
invalid_operation_panic!(self)
}
/// Get the median of the Series as a new Series of length 1.
fn median_as_series(&self) -> Series {
invalid_operation_panic!(self)
Expand Down
6 changes: 3 additions & 3 deletions polars/polars-lazy/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2108,7 +2108,7 @@ impl Rem for Expr {
///
/// It is the responsibility of the caller that the schema is correct by giving
/// the correct output_type. If None given the output type of the input expr is used.
pub fn map_mul<F, E>(function: F, expr: E, output_type: GetOutput) -> Expr
pub fn map_multiple<F, E>(function: F, expr: E, output_type: GetOutput) -> Expr
where
F: Fn(&mut [Series]) -> Result<Series> + 'static + Send + Sync,
E: AsRef<[Expr]>,
Expand All @@ -2135,7 +2135,7 @@ where
/// * `map_mul` should be used for operations that are independent of groups, e.g. `multiply * 2`, or `raise to the power`
/// * `apply_mul` should be used for operations that work on a group of data. e.g. `sum`, `count`, etc.
/// * `map_list_mul` should be used when the function expects a list aggregated series.
pub fn map_list_mul<F, E>(function: F, expr: E, output_type: GetOutput) -> Expr
pub fn map_list_multiple<F, E>(function: F, expr: E, output_type: GetOutput) -> Expr
where
F: Fn(&mut [Series]) -> Result<Series> + 'static + Send + Sync,
E: AsRef<[Expr]>,
Expand Down Expand Up @@ -2164,7 +2164,7 @@ where
///
/// * `[map_mul]` should be used for operations that are independent of groups, e.g. `multiply * 2`, or `raise to the power`
/// * `[apply_mul]` should be used for operations that work on a group of data. e.g. `sum`, `count`, etc.
pub fn apply_mul<F, E>(function: F, expr: E, output_type: GetOutput) -> Expr
pub fn apply_multiple<F, E>(function: F, expr: E, output_type: GetOutput) -> Expr
where
F: Fn(&mut [Series]) -> Result<Series> + 'static + Send + Sync,
E: AsRef<[Expr]>,
Expand Down
1 change: 0 additions & 1 deletion polars/polars-lazy/src/tests/logical.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use polars_core::prelude::TimeUnit::Milliseconds;
use polars_core::utils::arrow::temporal_conversions::MILLISECONDS_IN_DAY;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions polars/polars-lazy/src/tests/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ fn test_apply_multiple_columns() -> Result<()> {
let out = df
.clone()
.lazy()
.select([map_mul(
.select([map_multiple(
multiply,
[col("A"), col("B")],
GetOutput::from_type(DataType::Float64),
Expand All @@ -1866,7 +1866,7 @@ fn test_apply_multiple_columns() -> Result<()> {
let out = df
.lazy()
.groupby_stable([col("cars")])
.agg([apply_mul(
.agg([apply_multiple(
multiply,
[col("A"), col("B")],
GetOutput::from_type(DataType::Float64),
Expand Down
4 changes: 2 additions & 2 deletions py-polars/src/lazy/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ pub fn map_mul(
None => fld.clone(),
});
if apply_groups {
polars::lazy::dsl::apply_mul(function, exprs, output_map).into()
polars::lazy::dsl::apply_multiple(function, exprs, output_map).into()
} else {
polars::lazy::dsl::map_mul(function, exprs, output_map).into()
polars::lazy::dsl::map_multiple(function, exprs, output_map).into()
}
}

0 comments on commit 53365be

Please sign in to comment.