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

Better concat_rows error messages #1057

Merged
merged 7 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
119 changes: 89 additions & 30 deletions lib/explorer/data_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5437,47 +5437,106 @@ defmodule Explorer.DataFrame do
@doc type: :multi
@spec concat_rows([DataFrame.t()]) :: DataFrame.t()
def concat_rows([%DataFrame{} = head | _tail] = dfs) do
changed_types = compute_changed_types_concat_rows(dfs)
out_df = out_df(head, head.names, Map.merge(head.dtypes, changed_types))
with :ok <- ensure_column_names_match(dfs),
{:ok, changed_types} <- compute_changed_types_concat_rows(dfs) do
out_df = out_df(head, head.names, Map.merge(head.dtypes, changed_types))

dfs =
if changed_types == %{} do
dfs
else
for df <- dfs do
mutate_with(ungroup(df), fn ldf ->
for {column, target_type} <- changed_types,
do: {column, Series.cast(ldf[column], target_type)}
end)
dfs =
if changed_types == %{} do
dfs
else
for df <- dfs do
mutate_with(ungroup(df), fn ldf ->
for {column, target_type} <- changed_types,
do: {column, Series.cast(ldf[column], target_type)}
end)
end
end
end

Shared.apply_dataframe(dfs, :concat_rows, [out_df])
Shared.apply_dataframe(dfs, :concat_rows, [out_df])
else
{:error, message} -> raise ArgumentError, message
end
end

defp compute_changed_types_concat_rows([head | tail]) do
types = head.dtypes
defp ensure_column_names_match([head | tail]) do
josevalim marked this conversation as resolved.
Show resolved Hide resolved
df_0_cols = head |> names() |> MapSet.new()

Enum.reduce(tail, %{}, fn df, changed_types ->
if n_columns(df) != map_size(types) do
raise ArgumentError,
"dataframes must have the same columns"
tail
|> Enum.with_index(1)
|> Enum.reduce_while(:ok, fn {df, index}, :ok ->
df_i_cols = df |> names() |> MapSet.new()

if MapSet.equal?(df_0_cols, df_i_cols) do
{:cont, :ok}
else
mismatched_cols = MapSet.symmetric_difference(df_0_cols, df_i_cols)
in_0_only = df_0_cols |> MapSet.intersection(mismatched_cols) |> Enum.sort()
in_i_only = df_i_cols |> MapSet.intersection(mismatched_cols) |> Enum.sort()

message_0 =
if in_0_only == [] do
""
else
"""

* dataframe 0 has these columns not present in dataframe #{index}:

#{inspect(in_0_only)}
"""
end

message_i =
if in_i_only == [] do
""
else
"""

* dataframe #{index} has these columns not present in dataframe 0:

#{inspect(in_i_only)}
"""
end

message = "dataframes must have the same columns\n#{message_0}#{message_i}"
{:halt, {:error, message}}
end
end)
end

Enum.reduce(df.dtypes, changed_types, fn {name, type}, changed_types ->
case types do
%{^name => current_type} ->
if dtype = Shared.merge_dtype(Map.get(changed_types, name, current_type), type) do
Map.put(changed_types, name, dtype)
else
raise ArgumentError,
"columns and dtypes must be identical for all dataframes"
end
defp compute_changed_types_concat_rows([head | tail]) do
tail
|> Enum.with_index(1)
|> Enum.reduce_while({:ok, %{}}, fn {df, index}, {:ok, changed_types} ->
Enum.reduce_while(df.dtypes, changed_types, fn {name, type}, changed_types ->
# This runs after `ensure_column_names_match`, so we can `fetch!` here.
current_type = Map.fetch!(head.dtypes, name)

if dtype = Shared.merge_dtype(Map.get(changed_types, name, current_type), type) do
{:cont, Map.put(changed_types, name, dtype)}
else
message =
"""
column dtypes must be compatible for all dataframes

* dataframe 0, column #{name} has dtype:

#{inspect(current_type)}

* dataframe #{index}, column #{name} has dtype:

%{} ->
raise ArgumentError, "dataframes must have the same columns"
#{inspect(dtype)}

these types are incompatible
"""

{:halt, {:error, message}}
end
end)
|> case do
{:error, message} -> {:halt, {:error, message}}
%{} = changed_types -> {:cont, {:ok, changed_types}}
end
end)
end

Expand Down
50 changes: 47 additions & 3 deletions test/explorer/data_frame/lazy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1476,15 +1476,59 @@ defmodule Explorer.DataFrame.LazyTest do
df1 = DF.new([x: [7, 8, 9], y: ["g", "h", nil]], lazy: true)

assert_raise ArgumentError,
"dataframes must have the same columns",
"""
dataframes must have the same columns

* dataframe 0 has these columns not present in dataframe 1:

[\"x\", \"y\"]

* dataframe 1 has these columns not present in dataframe 0:

[\"z\"]
""",
fn -> DF.concat_rows(df1, DF.new([z: [7, 8, 9]], lazy: true)) end

assert_raise ArgumentError,
"dataframes must have the same columns",
"""
dataframes must have the same columns

* dataframe 0 has these columns not present in dataframe 1:

[\"y\"]

* dataframe 1 has these columns not present in dataframe 0:

[\"z\"]
""",
fn -> DF.concat_rows(df1, DF.new([x: [7, 8, 9], z: [7, 8, 9]], lazy: true)) end

assert_raise ArgumentError,
"columns and dtypes must be identical for all dataframes",
"""
dataframes must have the same columns

* dataframe 1 has these columns not present in dataframe 0:

[\"z\"]
""",
fn ->
DF.concat_rows(df1, DF.new([x: [7], y: [8], z: [9]], lazy: true))
end

assert_raise ArgumentError,
"""
column dtypes must be compatible for all dataframes

* dataframe 0, column y has dtype:

:string

* dataframe 1, column y has dtype:

nil

these types are incompatible
""",
fn ->
DF.concat_rows(df1, DF.new([x: [7, 8, 9], y: [10, 11, 12]], lazy: true))
end
Expand Down
26 changes: 24 additions & 2 deletions test/explorer/data_frame_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2883,15 +2883,37 @@ defmodule Explorer.DataFrameTest do
df1 = DF.new(x: [1, 2, 3], y: ["a", "b", "c"])

assert_raise ArgumentError,
"dataframes must have the same columns",
"""
dataframes must have the same columns

* dataframe 0 has these columns not present in dataframe 1:

[\"x\", \"y\"]

* dataframe 1 has these columns not present in dataframe 0:

[\"z\"]
""",
fn -> DF.concat_rows(df1, DF.new(z: [7, 8, 9])) end
end

test "with incompatible column dtypes" do
df1 = DF.new(x: [1, 2, 3], y: ["a", "b", "c"])

assert_raise ArgumentError,
"columns and dtypes must be identical for all dataframes",
"""
column dtypes must be compatible for all dataframes

* dataframe 0, column y has dtype:

:string

* dataframe 1, column y has dtype:

nil

these types are incompatible
""",
fn -> DF.concat_rows(df1, DF.new(x: [7, 8, 9], y: [10, 11, 12])) end
end

Expand Down
Loading