Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ jobs:
strategy:
matrix:
include:
- otp_version: 25.3
elixir_version: 1.15
- otp_version: "27.3"
elixir_version: "1.18"
runner: ubuntu-latest

- otp_version: "27.2"
elixir_version: 1.18
- otp_version: "29.1"
elixir_version: "1.20"
runner: ubuntu-latest

runs-on: ubuntu-latest
name: test
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added `Explorer.Series.starts_with/2` and `Explorer.Series.ends_with/2`, which
detect literal prefixes and suffixes without needing a regular expression.
- Added `Explorer.Series.is_unique/1` and `Explorer.Series.is_duplicated/1`, which
return a boolean mask marking the values that appear exactly once or more than once.
- Added `Explorer.Series.diff/2`, which calculates the difference between
consecutive values, with `:n` and `:null_behavior` options.
- Added `Explorer.Series.kurtosis/2`, which computes the sample kurtosis of a
series, with `:fisher` and `:bias` options.
- Added `Explorer.Series.sign/1`, which returns the sign of each value in a series.

## [v0.13.0] - 2026-09-28

### Added
Expand Down
82 changes: 82 additions & 0 deletions lib/explorer/backend/lazy_series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ defmodule Explorer.Backend.LazySeries do
cast: 2,
select: 3,
abs: 1,
sign: 1,
is_unique: 1,
is_duplicated: 1,
diff: 3,
strptime: 2,
strftime: 2,
clip_float: 3,
Expand Down Expand Up @@ -122,6 +126,7 @@ defmodule Explorer.Backend.LazySeries do
nil_count: 1,
size: 1,
skew: 2,
kurtosis: 3,
correlation: 3,
covariance: 3,
all: 1,
Expand All @@ -130,6 +135,8 @@ defmodule Explorer.Backend.LazySeries do
rle_id: 1,
# Strings
contains: 2,
starts_with: 2,
ends_with: 2,
re_contains: 2,
replace: 3,
re_replace: 3,
Expand Down Expand Up @@ -588,6 +595,14 @@ defmodule Explorer.Backend.LazySeries do
Backend.Series.new(data, {:f, 64})
end

@impl true
def kurtosis(%Series{} = series, fisher, bias) do
args = [series_or_lazy_series!(series), fisher, bias]
data = new(:kurtosis, args, {:f, 64}, true)

Backend.Series.new(data, {:f, 64})
end

@impl true
def correlation(%Series{} = left, %Series{} = right, method) do
args = [series_or_lazy_series!(left), series_or_lazy_series!(right), method]
Expand Down Expand Up @@ -931,6 +946,13 @@ defmodule Explorer.Backend.LazySeries do
Backend.Series.new(data, series.dtype)
end

@impl true
def sign(%Series{} = series) do
data = new(:sign, [lazy_series!(series)], series.dtype)

Backend.Series.new(data, series.dtype)
end

@impl true
def strptime(%Series{} = series, format_string) do
dtype = {:naive_datetime, :microsecond}
Expand Down Expand Up @@ -1102,6 +1124,20 @@ defmodule Explorer.Backend.LazySeries do
Backend.Series.new(data, :boolean)
end

@impl true
def starts_with(series, prefix) do
data = new(:starts_with, [lazy_series!(series), prefix], :boolean)

Backend.Series.new(data, :boolean)
end

@impl true
def ends_with(series, suffix) do
data = new(:ends_with, [lazy_series!(series), suffix], :boolean)

Backend.Series.new(data, :boolean)
end

@impl true
def re_contains(series, pattern) do
data = new(:re_contains, [lazy_series!(series), pattern], :boolean)
Expand Down Expand Up @@ -1263,6 +1299,52 @@ defmodule Explorer.Backend.LazySeries do
Backend.Series.new(data, {:u, 32})
end

@impl true
def is_unique(series) do
args = [lazy_series!(series)]

if aggregations?(args), do: raise_agg_inside_window(:is_unique)

data = new(:is_unique, args, :boolean, false)

Backend.Series.new(data, :boolean)
end

@impl true
def is_duplicated(series) do
args = [lazy_series!(series)]

if aggregations?(args), do: raise_agg_inside_window(:is_duplicated)

data = new(:is_duplicated, args, :boolean, false)

Backend.Series.new(data, :boolean)
end

@impl true
def diff(series, n, null_behavior) do
args = [lazy_series!(series), n, null_behavior]

if aggregations?(args), do: raise_agg_inside_window(:diff)

dtype = diff_dtype(series.dtype)
data = new(:diff, args, dtype, false)

Backend.Series.new(data, dtype)
end

# Mirrors the output dtype that Polars computes for `diff`.
defp diff_dtype({:datetime, precision, _time_zone}), do: {:duration, precision}
defp diff_dtype({:naive_datetime, precision}), do: {:duration, precision}
defp diff_dtype(:date), do: {:duration, :microsecond}
defp diff_dtype(:time), do: {:duration, :nanosecond}
defp diff_dtype({:u, 64}), do: {:s, 64}
defp diff_dtype({:u, 32}), do: {:s, 64}
defp diff_dtype({:u, 16}), do: {:s, 32}
defp diff_dtype({:u, 8}), do: {:s, 16}
defp diff_dtype({:decimal, _precision, scale}), do: {:decimal, 38, scale}
defp diff_dtype(dtype), do: dtype

@impl true
def count_matches(series, substring) do
data = new(:count_matches, [lazy_series!(series), substring], {:u, 32})
Expand Down
10 changes: 10 additions & 0 deletions lib/explorer/backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ defmodule Explorer.Backend.Series do
@callback nil_count(s) :: number() | lazy_s()
@callback product(s) :: float() | non_finite() | lazy_s() | nil
@callback skew(s, bias? :: boolean()) :: float() | non_finite() | lazy_s() | nil
@callback kurtosis(s, fisher? :: boolean(), bias? :: boolean()) ::
float() | non_finite() | lazy_s() | nil
@callback correlation(s, s, method :: atom()) :: float() | non_finite() | lazy_s() | nil
@callback covariance(s, s, ddof :: non_neg_integer()) :: float() | non_finite() | lazy_s() | nil
@callback all?(s) :: boolean() | lazy_s()
@callback any?(s) :: boolean() | lazy_s()
@callback row_index(s) :: s | lazy_s()
@callback rle_id(s) :: s | lazy_s()
@callback is_unique(s) :: s | lazy_s()
@callback is_duplicated(s) :: s | lazy_s()
@callback diff(s, n :: integer(), null_behavior :: :ignore | :drop) :: s | lazy_s()

# Cumulative

Expand All @@ -121,6 +126,7 @@ defmodule Explorer.Backend.Series do
@callback log(argument :: s, base :: float()) :: s
@callback exp(s) :: s
@callback abs(s) :: s
@callback sign(s) :: s
@callback clip(s, number(), number()) :: s

# Trigonometry
Expand Down Expand Up @@ -300,6 +306,8 @@ defmodule Explorer.Backend.Series do
# Strings

@callback contains(s, String.t()) :: s
@callback starts_with(s, String.t()) :: s
@callback ends_with(s, String.t()) :: s
@callback upcase(s) :: s
@callback downcase(s) :: s
@callback replace(s, String.t(), String.t()) :: s
Expand Down Expand Up @@ -357,6 +365,7 @@ defmodule Explorer.Backend.Series do
%Explorer.Series{data: data, dtype: dtype}
end

@default_limit 50
alias Inspect.Algebra, as: A
alias Explorer.Series

Expand All @@ -366,6 +375,7 @@ defmodule Explorer.Backend.Series do
def inspect(series, backend, n_rows, inspect_opts, opts \\ [])
when is_binary(backend) and (is_integer(n_rows) or is_nil(n_rows) or is_binary(n_rows)) and
is_list(opts) do
inspect_opts = %{inspect_opts | limit: @default_limit}
open = A.color("[", :list, inspect_opts)
close = A.color("]", :list, inspect_opts)

Expand Down
7 changes: 7 additions & 0 deletions lib/explorer/polars_backend/expression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ defmodule Explorer.PolarsBackend.Expression do
equal: 2,
exp: 1,
abs: 1,
sign: 1,
is_unique: 1,
is_duplicated: 1,
fill_missing_with_value: 2,
first: 1,
format: 1,
Expand Down Expand Up @@ -102,6 +105,8 @@ defmodule Explorer.PolarsBackend.Expression do
sample_frac: 5,
exp: 1,
skew: 2,
kurtosis: 3,
diff: 3,
round: 2,
clip_float: 3,
clip_integer: 3,
Expand Down Expand Up @@ -140,6 +145,8 @@ defmodule Explorer.PolarsBackend.Expression do

# Strings
contains: 2,
starts_with: 2,
ends_with: 2,
re_contains: 2,
replace: 3,
re_replace: 3,
Expand Down
12 changes: 9 additions & 3 deletions lib/explorer/polars_backend/lazy_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ defmodule Explorer.PolarsBackend.LazyFrame do
# IO

@impl true
def from_query(conn, query, params) do
with {:ok, df} <- Eager.from_query(conn, query, params) do
{:ok, Eager.lazy(df)}
if Code.ensure_loaded?(Adbc) do
def from_query(conn, query, params) do
with {:ok, df} <- Eager.from_query(conn, query, params) do
{:ok, Eager.lazy(df)}
end
end
else
def from_query(_conn, _query, _params) do
raise "you must install :adbc as a dependency in order to use from_query/3"
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/explorer/polars_backend/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ defmodule Explorer.PolarsBackend.Native do
def s_cumulative_count(_s, _reverse), do: err()
def s_cumulative_product(_s, _reverse), do: err()
def s_skew(_s, _bias), do: err()
def s_kurtosis(_s, _fisher, _bias), do: err()
def s_correlation(_s1, _s2, _method), do: err()
def s_covariance(_s1, _s2, _ddof), do: err()
def s_distinct(_s), do: err()
Expand Down Expand Up @@ -428,6 +429,12 @@ defmodule Explorer.PolarsBackend.Native do
def s_round(_s, _decimals), do: err()
def s_row_index(_s), do: err()
def s_rle_id(_s), do: err()
def s_is_unique(_s), do: err()
def s_is_duplicated(_s), do: err()
def s_diff(_s, _n, _null_behavior), do: err()
def s_sign(_s), do: err()
def s_starts_with(_s, _prefix), do: err()
def s_ends_with(_s, _suffix), do: err()
def s_floor(_s), do: err()
def s_ceil(_s), do: err()
def s_rstrip(_s, _string), do: err()
Expand Down
25 changes: 25 additions & 0 deletions lib/explorer/polars_backend/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ defmodule Explorer.PolarsBackend.Series do
def skew(series, bias?),
do: Shared.apply_series(series, :s_skew, [bias?])

@impl true
def kurtosis(series, fisher?, bias?),
do: Shared.apply_series(series, :s_kurtosis, [fisher?, bias?])

@impl true
def correlation(left, right, method),
do: Shared.apply_series(matching_size!(left, right), :s_correlation, [right.data, method])
Expand All @@ -256,6 +260,16 @@ defmodule Explorer.PolarsBackend.Series do
@impl true
def rle_id(series), do: Shared.apply_series(series, :s_rle_id)

@impl true
def is_unique(series), do: Shared.apply_series(series, :s_is_unique)

@impl true
def is_duplicated(series), do: Shared.apply_series(series, :s_is_duplicated)

@impl true
def diff(series, n, null_behavior),
do: Shared.apply_series(series, :s_diff, [n, null_behavior])

# Cumulative

@impl true
Expand Down Expand Up @@ -391,6 +405,9 @@ defmodule Explorer.PolarsBackend.Series do
@impl true
def abs(%Series{} = s), do: Shared.apply_series(s, :s_abs, [])

@impl true
def sign(%Series{} = s), do: Shared.apply_series(s, :s_sign, [])

@impl true
def clip(%Series{dtype: dtype} = s, min, max)
when dtype in @integer_types and is_integer(min) and is_integer(max),
Expand Down Expand Up @@ -699,6 +716,14 @@ defmodule Explorer.PolarsBackend.Series do
def contains(series, substring),
do: Shared.apply_series(series, :s_contains, [substring, true])

@impl true
def starts_with(series, prefix),
do: Shared.apply_series(series, :s_starts_with, [prefix])

@impl true
def ends_with(series, suffix),
do: Shared.apply_series(series, :s_ends_with, [suffix])

@impl true
def re_contains(series, pattern),
do: Shared.apply_series(series, :s_contains, [pattern, false])
Expand Down
Loading
Loading