Skip to content

Commit

Permalink
Add mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cigrainger committed Dec 17, 2022
1 parent 9425b0b commit 201075c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/explorer/backend/lazy_series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ defmodule Explorer.Backend.LazySeries do
max: 1,
mean: 1,
median: 1,
mode: 1,
n_distinct: 1,
variance: 1,
standard_deviation: 1,
Expand Down Expand Up @@ -99,6 +100,7 @@ defmodule Explorer.Backend.LazySeries do
:max,
:mean,
:median,
:mode,
:variance,
:standard_deviation,
:count,
Expand Down
1 change: 1 addition & 0 deletions lib/explorer/backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ defmodule Explorer.Backend.Series do
@callback max(s) :: number() | Date.t() | NaiveDateTime.t() | lazy_s() | nil
@callback mean(s) :: float() | lazy_s() | nil
@callback median(s) :: float() | lazy_s() | nil
@callback mode(s) :: number() | lazy_s() | nil
@callback variance(s) :: float() | lazy_s() | nil
@callback standard_deviation(s) :: float() | lazy_s() | nil
@callback quantile(s, float()) :: number | Date.t() | NaiveDateTime.t() | lazy_s() | nil
Expand Down
1 change: 1 addition & 0 deletions lib/explorer/polars_backend/expression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ defmodule Explorer.PolarsBackend.Expression do
mean: 1,
median: 1,
min: 1,
mode: 1,
multiply: 2,
n_distinct: 1,
nil_count: 1,
Expand Down
3 changes: 3 additions & 0 deletions lib/explorer/polars_backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ defmodule Explorer.PolarsBackend.Series do
@impl true
def median(series), do: Shared.apply_series(series, :s_median)

@impl true
def mode(series), do: Shared.apply_series(series, :s_mode)

@impl true
def variance(series), do: Shared.apply_series(series, :s_variance)

Expand Down
21 changes: 21 additions & 0 deletions lib/explorer/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,27 @@ defmodule Explorer.Series do

def mean(%Series{dtype: dtype}), do: dtype_error("mean/1", dtype, [:integer, :float])

@doc """
Gets the mode value of the series.
## Supported dtypes
* `:integer`
* `:float`
## Examples
iex> s = Explorer.Series.from_list([1, 2, 2, 3])
iex> Explorer.Series.mode(s)
2
"""
@doc type: :aggregation
@spec mode(series :: Series.t()) :: float() | nil
def mode(%Series{dtype: dtype} = series) when numeric_dtype?(dtype),
do: Shared.apply_impl(series, :mode)

def mode(%Series{dtype: dtype}), do: dtype_error("mode/1", dtype, [:integer, :float])

@doc """
Gets the median value of the series.
Expand Down
1 change: 1 addition & 0 deletions native/explorer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ features = [
"to_dummies",
"is_in",
"strings",
"mode",
]

[dependencies.polars-ops]
Expand Down
7 changes: 7 additions & 0 deletions native/explorer/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,10 @@ pub fn expr_trim_trailing(expr: ExExpr) -> ExExpr {
let expr: Expr = expr.resource.0.clone();
ExExpr::new(expr.str().rstrip(None))
}

#[rustler::nif]
pub fn expr_mode(expr: ExExpr) -> ExExpr {
let expr: Expr = expr.resource.0.clone();

ExExpr::new(expr.mode())
}
2 changes: 2 additions & 0 deletions native/explorer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ rustler::init!(
expr_mean,
expr_median,
expr_min,
expr_mode,
expr_n_distinct,
expr_nil_count,
expr_quantile,
Expand Down Expand Up @@ -258,6 +259,7 @@ rustler::init!(
s_mean,
s_median,
s_min,
s_mode,
s_multiply,
s_n_distinct,
s_name,
Expand Down
9 changes: 9 additions & 0 deletions native/explorer/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,3 +879,12 @@ pub fn s_trim_trailing(data: ExSeries) -> Result<ExSeries, ExplorerError> {
// There are no eager strip functions.
Ok(ExSeries::new(s1.utf8()?.replace(r#"[ \s]+$"#, "")?.into()))
}

#[rustler::nif(schedule = "DirtyCpu")]
pub fn s_mode(env: Env, data: ExSeries) -> Result<Term, ExplorerError> {
let s = &data.resource.0;
match s.dtype() {
DataType::UInt32 | DataType::Int64 | DataType::Float64 => Ok(s.mode()?.encode(env)),
dt => panic!("mode/1 not implemented for {:?}", dt),
}
}

0 comments on commit 201075c

Please sign in to comment.