Skip to content

Commit

Permalink
enable adding eager series
Browse files Browse the repository at this point in the history
  • Loading branch information
lkarthee committed Feb 14, 2024
1 parent 1f174d7 commit 3c20a80
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
10 changes: 1 addition & 9 deletions lib/explorer/data_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2817,15 +2817,7 @@ defmodule Explorer.DataFrame do
Shared.apply_impl(df, :mutate_with, [df_out, column_pairs])
end

defp value!(%Series{data: %LazySeries{}} = lazy_series) do
lazy_series
end

defp value!(%Series{data: _other}) do
raise ArgumentError,
"expecting a lazy series. Consider using `Explorer.DataFrame.put/3` " <>
"to add eager series to your dataframe."
end
defp value!(%Series{} = series), do: series

defp value!(list) when is_list(list) do
raise ArgumentError,
Expand Down
22 changes: 18 additions & 4 deletions test/explorer/data_frame_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,20 @@ defmodule Explorer.DataFrameTest do
}
end

test "add series to dataframe" do
df = DF.new([%{a: 1}, %{a: 2}, %{a: 3}])
s = Explorer.Series.from_list(["x", "y", "z"])

df1 = DF.mutate(df, s: ^s, a1: a + 1)
assert df1.names == ["a", "s", "a1"]

assert df1.dtypes == %{
"a" => {:s, 64},
"s" => :string,
"a1" => {:s, 64}
}
end

test "adds new columns" do
df = DF.new(a: [1, 2, 3], b: ["a", "b", "c"])

Expand Down Expand Up @@ -1578,12 +1592,12 @@ defmodule Explorer.DataFrameTest do
}
end

test "raises when adding eager series" do
test "raises when adding eager series whose length does not match dataframe's length" do
df = DF.new(a: [1, 2, 3])
series = Series.from_list([4, 5, 6])
series = Series.from_list([4, 5, 6, 7])

assert_raise ArgumentError,
"expecting a lazy series. Consider using `Explorer.DataFrame.put/3` to add eager series to your dataframe.",
assert_raise RuntimeError,
"Polars Error: lengths don't match: unable to add a column of length 4 to a DataFrame of height 3",
fn ->
DF.mutate(df, b: ^series)
end
Expand Down

0 comments on commit 3c20a80

Please sign in to comment.