From adc104385e013a3422c58daaa9400822ae4201eb Mon Sep 17 00:00:00 2001 From: pdeffebach <23196228+pdeffebach@users.noreply.github.com> Date: Wed, 26 Dec 2018 05:21:33 -0800 Subject: [PATCH] Show group values when printing grouped dataframe (#1632) --- src/groupeddataframe/show.jl | 30 +++++++++++++++++++++++++----- test/show.jl | 12 ++++++------ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/groupeddataframe/show.jl b/src/groupeddataframe/show.jl index d999e3dd29..f2e1f025ff 100644 --- a/src/groupeddataframe/show.jl +++ b/src/groupeddataframe/show.jl @@ -6,7 +6,10 @@ function Base.show(io::IO, gd::GroupedDataFrame; rowlabel::Symbol = :Row, summary::Bool = true) N = length(gd) - keys = join(':' .* string.(names(gd.parent)[gd.cols]), ", ") + keynames = names(gd.parent)[gd.cols] + parent_names = names(gd.parent) + keys = join(':' .* string.(keynames), ", ") + keystr = length(gd.cols) > 1 ? "keys" : "key" groupstr = N > 1 ? "groups" : "group" summary && print(io, "$(typeof(gd)) with $N $groupstr based on $keystr: $keys") @@ -14,7 +17,13 @@ function Base.show(io::IO, gd::GroupedDataFrame; for i = 1:N nrows = size(gd[i], 1) rows = nrows > 1 ? "rows" : "row" - print(io, "\nGroup $i: $nrows $rows") + + identified_groups = [':' * string(parent_names[col], " = ", first(gd[i][col])) + for col in gd.cols] + + print(io, "\nGroup $i ($nrows $rows): ") + join(io, identified_groups, ", ") + show(io, gd[i], summary=false, allrows=allrows, allcols=allcols, rowlabel=rowlabel) end @@ -22,15 +31,26 @@ function Base.show(io::IO, gd::GroupedDataFrame; if N > 0 nrows = size(gd[1], 1) rows = nrows > 1 ? "rows" : "row" - print(io, "\nFirst Group: $nrows $rows") + + identified_groups = [':' * string(parent_names[col], " = ", first(gd[1][col])) + for col in gd.cols] + + print(io, "\nFirst Group ($nrows $rows): ") + join(io, identified_groups, ", ") + show(io, gd[1], summary=false, allrows=allrows, allcols=allcols, rowlabel=rowlabel) end if N > 1 - print(io, "\n⋮\n") nrows = size(gd[N], 1) rows = nrows > 1 ? "rows" : "row" - print(io, "Last Group: $nrows $rows") + + identified_groups = [':' * string(parent_names[col], " = ", first(gd[N][col])) + for col in gd.cols] + print(io, "\n⋮") + print(io, "\nLast Group ($nrows $rows): ") + join(io, identified_groups, ", ") + show(io, gd[N], summary=false, allrows=allrows, allcols=allcols, rowlabel=rowlabel) end diff --git a/test/show.jl b/test/show.jl index 172aa3abd1..942222917e 100644 --- a/test/show.jl +++ b/test/show.jl @@ -235,13 +235,13 @@ module TestShow str = String(take!(io.io)) @test str == """ GroupedDataFrame{DataFrame} with 4 groups based on key: :A - First Group: 1 row + First Group (1 row): :A = 1 │ Row │ A │ B │ C │ │ │ Int64 │ String │ Float32 │ ├─────┼───────┼────────┼─────────┤ │ 1 │ 1 │ x" │ 1.0 │ ⋮ - Last Group: 1 row + Last Group (1 row): :A = 4 │ Row │ A │ B │ C │ │ │ Int64 │ String │ Float32 │ ├─────┼───────┼────────┼─────────┤ @@ -250,22 +250,22 @@ module TestShow str = String(take!(io.io)) @test str == """ GroupedDataFrame{DataFrame} with 4 groups based on key: :A - Group 1: 1 row + Group 1 (1 row): :A = 1 │ Row │ A │ B │ C │ │ │ Int64 │ String │ Float32 │ ├─────┼───────┼────────┼─────────┤ │ 1 │ 1 │ x\" │ 1.0 │ - Group 2: 1 row + Group 2 (1 row): :A = 2 │ Row │ A │ B │ C │ │ │ Int64 │ String │ Float32 │ ├─────┼───────┼─────────────┼─────────┤ │ 1 │ 2 │ ∀ε>0: x+ε>x │ 2.0 │ - Group 3: 1 row + Group 3 (1 row): :A = 3 │ Row │ A │ B │ C │ │ │ Int64 │ String │ Float32 │ ├─────┼───────┼────────┼─────────┤ │ 1 │ 3 │ z\$ │ 3.0 │ - Group 4: 1 row + Group 4 (1 row): :A = 4 │ Row │ A │ B │ C │ │ │ Int64 │ String │ Float32 │ ├─────┼───────┼────────┼─────────┤