From 3b70ff5fa5dc90b040dd59b3e0ed8f9ff6de906b Mon Sep 17 00:00:00 2001 From: Kartheek Date: Mon, 29 Jan 2024 19:56:46 +0530 Subject: [PATCH 1/2] fix DF.print for :struct --- lib/explorer/data_frame.ex | 10 +++++++++- test/explorer/data_frame_test.exs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/explorer/data_frame.ex b/lib/explorer/data_frame.ex index 8fdd15870..069ad887b 100644 --- a/lib/explorer/data_frame.ex +++ b/lib/explorer/data_frame.ex @@ -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) diff --git a/test/explorer/data_frame_test.exs b/test/explorer/data_frame_test.exs index 906c711eb..b08997c54 100644 --- a/test/explorer/data_frame_test.exs +++ b/test/explorer/data_frame_test.exs @@ -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 | + | | | + +==================+========================+ + | 2 | %{"a" => 1} | + +------------------+------------------------+ + | 3 | %{"a" => 2} | + +------------------+------------------------+ + + """ + end end test "fetch/2" do From c67ae3a1cb957f207ce22afb2e584eb61d680ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 29 Jan 2024 16:27:32 +0100 Subject: [PATCH 2/2] Update lib/explorer/data_frame.ex --- lib/explorer/data_frame.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/explorer/data_frame.ex b/lib/explorer/data_frame.ex index 069ad887b..847bb9beb 100644 --- a/lib/explorer/data_frame.ex +++ b/lib/explorer/data_frame.ex @@ -5675,7 +5675,7 @@ defmodule Explorer.DataFrame do list = Series.to_list(s) case s.dtype do - {:struct, _} -> Enum.map(list, &"#{inspect(&1)}") + {:struct, _} -> Enum.map(list, &inspect/1) _ -> list end end)