Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Series.sort_by and Series.sort_with #762

Merged
merged 5 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions lib/explorer/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,28 @@ defmodule Explorer.Series do
|> Explorer.DataFrame.pull(:series)
end

@doc type: :element_wise
defmacro arrange(series, query, opts \\ []) do
{direction, opts} = Keyword.pop(opts, :direction, :asc)

quote do
require Explorer.DataFrame

Explorer.DataFrame.new(_: unquote(series))
|> Explorer.DataFrame.arrange([{unquote(direction), unquote(query)}], unquote(opts))
|> Explorer.DataFrame.pull(:_)
end
end

@doc type: :element_wise
def arrange_with(%Series{} = series, fun, opts \\ []) do
{direction, opts} = Keyword.pop(opts, :direction, :asc)

Explorer.DataFrame.new(series: series)
|> Explorer.DataFrame.arrange_with(&[{direction, fun.(&1[:series])}], opts)
|> Explorer.DataFrame.pull(:series)
end

@doc """
Filters a series with a mask.

Expand Down
60 changes: 60 additions & 0 deletions test/explorer/series_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2848,6 +2848,66 @@ defmodule Explorer.SeriesTest do
end
end

describe "arrange/2" do
billylanchantin marked this conversation as resolved.
Show resolved Hide resolved
test "ascending order (default)" do
require Explorer.Series

s1 = Series.from_list([1, 2, 3])
result = Series.arrange(s1, remainder(_, 3))
assert Series.to_list(result) == [3, 1, 2]
end

test "descending order" do
require Explorer.Series

s1 = Series.from_list([1, 2, 3])
result = Series.arrange(s1, remainder(_, 3), direction: :desc)
assert Series.to_list(result) == [2, 1, 3]
end

test "nils last (default)" do
require Explorer.Series

s1 = Series.from_list([1, nil, 2, 3])
result = Series.arrange(s1, remainder(_, 3))
assert Series.to_list(result) == [3, 1, 2, nil]
end

test "nils first" do
require Explorer.Series

s1 = Series.from_list([1, nil, 2, 3])
result = Series.arrange(s1, remainder(_, 3), nils: :first)
assert Series.to_list(result) == [nil, 3, 1, 2]
end
end

describe "arrange_with/2" do
billylanchantin marked this conversation as resolved.
Show resolved Hide resolved
test "ascending order (default)" do
s1 = Series.from_list([1, 2, 3])
result = Series.arrange_with(s1, &Series.remainder(&1, 3))
assert Series.to_list(result) == [3, 1, 2]
end

test "descending order" do
s1 = Series.from_list([1, 2, 3])
result = Series.arrange_with(s1, &Series.remainder(&1, 3), direction: :desc)
assert Series.to_list(result) == [2, 1, 3]
end

test "nils last (default)" do
s1 = Series.from_list([1, nil, 2, 3])
result = Series.arrange_with(s1, &Series.remainder(&1, 3))
assert Series.to_list(result) == [3, 1, 2, nil]
end

test "nils first" do
s1 = Series.from_list([1, nil, 2, 3])
result = Series.arrange_with(s1, &Series.remainder(&1, 3), nils: :first)
assert Series.to_list(result) == [nil, 3, 1, 2]
end
end

describe "sample/2" do
test "sample taking 10 elements" do
s = 1..100 |> Enum.to_list() |> Series.from_list()
Expand Down
Loading