Skip to content

Commit

Permalink
Fix DF.print for :struct (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkarthee authored Jan 29, 2024
1 parent 56e6051 commit 8b03726
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/explorer/data_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5670,7 +5670,15 @@ defmodule Explorer.DataFrame do

values =
headers
|> Enum.map(&Series.to_list(df[&1]))
|> Enum.map(fn n ->
s = df[n]
list = Series.to_list(s)

case s.dtype do
{:struct, _} -> Enum.map(list, &inspect/1)
_ -> list
end
end)
|> Enum.zip_with(& &1)

name_type = Enum.zip_with(headers, types, fn x, y -> x <> y end)
Expand Down
18 changes: 18 additions & 0 deletions test/explorer/data_frame_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,24 @@ defmodule Explorer.DataFrameTest do
"""
end

test "works with structs" do
df = DF.new([%{n: %{a: 1}, m: 2}, %{n: %{a: 2}, m: 3}])

assert capture_io(fn -> DF.print(df) end) == """
+-------------------------------------------+
| Explorer DataFrame: [rows: 2, columns: 2] |
+------------------+------------------------+
| m | n |
| <s64> | <struct[1]> |
+==================+========================+
| 2 | %{"a" => 1} |
+------------------+------------------------+
| 3 | %{"a" => 2} |
+------------------+------------------------+
"""
end
end

test "fetch/2" do
Expand Down

0 comments on commit 8b03726

Please sign in to comment.