Skip to content

Commit

Permalink
Add support for :lazy option for DF.new/2
Browse files Browse the repository at this point in the history
This enable the creation of lazy dataframes right from the `new/2`
function.
  • Loading branch information
philss committed Dec 17, 2022
1 parent 53c823a commit 9425b0b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/explorer/polars_backend/lazy_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ defmodule Explorer.PolarsBackend.LazyFrame do
@impl true
def collect(ldf), do: Shared.apply_dataframe(ldf, ldf, :lf_collect, [])

@impl true
def from_tabular(tabular), do: Eager.from_tabular(tabular) |> Eager.to_lazy()

@impl true
def from_series(pairs), do: Eager.from_series(pairs) |> Eager.to_lazy()

# Introspection

@impl true
Expand Down
17 changes: 17 additions & 0 deletions test/explorer/data_frame/lazy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ defmodule Explorer.DataFrame.LazyTest do

alias Explorer.DataFrame, as: DF
alias Explorer.Datasets
alias Explorer.Series

setup do
df = Datasets.fossil_fuels()
ldf = DF.to_lazy(df)
{:ok, ldf: ldf, df: df}
end

test "new/1" do
ldf = DF.new([a: [1, 2, 3], b: ["a", "b", "c"]], lazy: true)
assert DF.to_lazy(ldf) == ldf

ldf1 = DF.new([%{a: 42, b: "a"}, %{a: 51, b: "c"}], lazy: true)
assert DF.to_lazy(ldf1) == ldf1

ldf2 =
DF.new(
[a: Series.from_list([1, 2, 3]), b: Series.from_list(["a", "b", "c"])],
lazy: true
)

assert DF.to_lazy(ldf2) == ldf2
end

test "names/1", %{ldf: ldf} do
assert DF.names(ldf) == [
"year",
Expand Down

0 comments on commit 9425b0b

Please sign in to comment.