Skip to content

Commit

Permalink
Fix concat_rows with not aligned dataframes (#920)
Browse files Browse the repository at this point in the history
Closes #902
  • Loading branch information
philss authored Jun 7, 2024
1 parent ebccf78 commit b2a6b53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/explorer/polars_backend/lazy_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ defmodule Explorer.PolarsBackend.LazyFrame do

@impl true
def concat_rows([%DF{} | _tail] = dfs, %DF{} = out_df) do
polars_dfs = Enum.map(dfs, & &1.data)
polars_dfs = Enum.map(dfs, fn df -> select(df, out_df).data end)
%__MODULE__{} = polars_df = Shared.apply(:lf_concat_rows, [polars_dfs])

%{out_df | data: polars_df}
Expand Down
11 changes: 11 additions & 0 deletions test/explorer/data_frame_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2759,6 +2759,17 @@ defmodule Explorer.DataFrameTest do
assert Series.to_list(df4["y"]) == ~w(a b c d e f g h) ++ [nil]
end

test "same dtype columns but columns not aligned" do
df1 = DF.new(x: [1, 2, 3, 4], y: ["a", "b", nil, "c"])
df2 = DF.new(y: ["d", "e", "f", nil], x: [4, 5, 6, 7])
df3 = DF.concat_rows(df1, df2)

assert DF.dtypes(df3) == %{"x" => {:s, 64}, "y" => :string}

assert Series.to_list(df3["x"]) == [1, 2, 3, 4, 4, 5, 6, 7]
assert Series.to_list(df3["y"]) == ["a", "b", nil] ++ ~w(c d e f) ++ [nil]
end

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

Expand Down

0 comments on commit b2a6b53

Please sign in to comment.