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

Print empty DataFrame #1019

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 21 additions & 1 deletion lib/explorer/data_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5924,6 +5924,10 @@ defmodule Explorer.DataFrame do
@doc """
Prints the DataFrame in a tabular fashion.

## Options

* `:limit` (non_neg_integer() | :infinity) - number of rows to print.

## Examples

df = Explorer.Datasets.iris()
Expand All @@ -5934,6 +5938,23 @@ defmodule Explorer.DataFrame do
@doc type: :introspection
@spec print(df :: DataFrame.t(), opts :: Keyword.t()) :: :ok
def print(df, opts \\ []) do
string =
if n_columns(df) == 0 do
empty_table_string()
else
non_empty_table_string(df, opts)
end

IO.puts(string)
end

defp empty_table_string() do
TableRex.Table.new()
|> TableRex.Table.add_rows([["Explorer DataFrame: [rows: 0, columns: 0]"]])
|> TableRex.Table.render!()
end

defp non_empty_table_string(df, opts) do
{rows, columns} =
if lazy?(df) do
{"???", n_columns(df)}
Expand Down Expand Up @@ -5977,7 +5998,6 @@ defmodule Explorer.DataFrame do
header_separator_symbol: "=",
horizontal_style: :all
)
|> IO.puts()
end

@doc """
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 @@ -2638,6 +2638,17 @@ defmodule Explorer.DataFrameTest do
"""
end

test "works with empty DataFrame" do
empty_df = DF.new([])

assert capture_io(fn -> DF.print(empty_df) end) == """
+-------------------------------------------+
| Explorer DataFrame: [rows: 0, columns: 0] |
+-------------------------------------------+

"""
end

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

Expand Down
Loading