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

improve show of GroupedDataFrame #1684

Merged
merged 2 commits into from Jan 18, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/abstractdataframe/io.jl
Expand Up @@ -168,15 +168,15 @@ function Base.show(io::IO, mime::MIME"text/html", gd::GroupedDataFrame)
N = length(gd)
keynames = names(gd.parent)[gd.cols]
parent_names = names(gd.parent)
keys = join(':' .* string.(keynames), ", ")
keys = html_escape(join(string.(keynames), ", "))
keystr = length(gd.cols) > 1 ? "keys" : "key"
groupstr = N > 1 ? "groups" : "group"
write(io, "<p><b>$(typeof(gd).name) with $N $groupstr based on $keystr: $keys</b></p>")
if N > 0
nrows = size(gd[1], 1)
rows = nrows > 1 ? "rows" : "row"

identified_groups = [':' * string(parent_names[col], " = ", first(gd[1][col]))
identified_groups = [html_escape(string(parent_names[col], " = ", repr(first(gd[1][col]))))
for col in gd.cols]

write(io, "<p><i>First Group ($nrows $rows): ")
Expand All @@ -188,7 +188,7 @@ function Base.show(io::IO, mime::MIME"text/html", gd::GroupedDataFrame)
nrows = size(gd[N], 1)
rows = nrows > 1 ? "rows" : "row"

identified_groups = [':' * string(parent_names[col], " = ", first(gd[N][col]))
identified_groups = [html_escape(string(parent_names[col], " = ", repr(first(gd[N][col]))))
for col in gd.cols]

write(io, "<p>&vellip;</p>")
Expand Down Expand Up @@ -288,16 +288,16 @@ function Base.show(io::IO, mime::MIME"text/latex", gd::GroupedDataFrame)
N = length(gd)
keynames = names(gd.parent)[gd.cols]
parent_names = names(gd.parent)
keys = join(latex_escape.(':' .* string.(keynames)), ", ")
keys = join(latex_escape.(string.(keynames)), ", ")
keystr = length(gd.cols) > 1 ? "keys" : "key"
groupstr = N > 1 ? "groups" : "group"
write(io, "$(typeof(gd).name) with $N $groupstr based on $keystr: $keys\n\n")
if N > 0
nrows = size(gd[1], 1)
rows = nrows > 1 ? "rows" : "row"

identified_groups = [latex_escape(':' * string(parent_names[col], " = ",
first(gd[1][col])))
identified_groups = [latex_escape(string(parent_names[col], " = ",
repr(first(gd[1][col]))))
for col in gd.cols]

write(io, "First Group ($nrows $rows): ")
Expand All @@ -309,8 +309,8 @@ function Base.show(io::IO, mime::MIME"text/latex", gd::GroupedDataFrame)
nrows = size(gd[N], 1)
rows = nrows > 1 ? "rows" : "row"

identified_groups = [latex_escape(':' * string(parent_names[col], " = ",
first(gd[N][col])))
identified_groups = [latex_escape(string(parent_names[col], " = ",
repr(first(gd[N][col]))))
for col in gd.cols]

write(io, "\n\$\\dots\$\n\n")
Expand Down
14 changes: 7 additions & 7 deletions src/groupeddataframe/show.jl
Expand Up @@ -8,7 +8,7 @@ function Base.show(io::IO, gd::GroupedDataFrame;
N = length(gd)
keynames = names(gd.parent)[gd.cols]
parent_names = names(gd.parent)
keys = join(':' .* string.(keynames), ", ")
keys = join(string.(keynames), ", ")

keystr = length(gd.cols) > 1 ? "keys" : "key"
groupstr = N > 1 ? "groups" : "group"
Expand All @@ -18,21 +18,21 @@ function Base.show(io::IO, gd::GroupedDataFrame;
nrows = size(gd[i], 1)
rows = nrows > 1 ? "rows" : "row"

identified_groups = [':' * string(parent_names[col], " = ", first(gd[i][col]))
identified_groups = [string(parent_names[col], " = ", repr(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
else
if N > 0
nrows = size(gd[1], 1)
rows = nrows > 1 ? "rows" : "row"
identified_groups = [':' * string(parent_names[col], " = ", first(gd[1][col]))

identified_groups = [string(parent_names[col], " = ", repr(first(gd[1][col])))
for col in gd.cols]

print(io, "\nFirst Group ($nrows $rows): ")
Expand All @@ -44,8 +44,8 @@ function Base.show(io::IO, gd::GroupedDataFrame;
if N > 1
nrows = size(gd[N], 1)
rows = nrows > 1 ? "rows" : "row"
identified_groups = [':' * string(parent_names[col], " = ", first(gd[N][col]))

identified_groups = [string(parent_names[col], " = ", repr(first(gd[N][col])))
for col in gd.cols]
print(io, "\n⋮")
print(io, "\nLast Group ($nrows $rows): ")
Expand Down
58 changes: 44 additions & 14 deletions test/grouping.jl
Expand Up @@ -902,38 +902,38 @@ module TestGrouping
show(io, gd)
str = String(take!(io.io))
@test str == """
GroupedDataFrame with 4 groups based on key: :A
First Group (1 row): :A = 1
GroupedDataFrame with 4 groups based on key: A
First Group (1 row): A = 1
│ Row │ A │ B │ C │
│ │ Int64 │ String │ Float32 │
├─────┼───────┼────────┼─────────┤
│ 1 │ 1 │ x" │ 1.0 │
Last Group (1 row): :A = 4
Last Group (1 row): A = 4
│ Row │ A │ B │ C │
│ │ Int64 │ String │ Float32 │
├─────┼───────┼────────┼─────────┤
│ 1 │ 4 │ A\\nC │ 4.0 │"""
show(io, gd, allgroups=true)
str = String(take!(io.io))
@test str == """
GroupedDataFrame with 4 groups based on key: :A
Group 1 (1 row): :A = 1
GroupedDataFrame with 4 groups based on key: A
Group 1 (1 row): A = 1
│ Row │ A │ B │ C │
│ │ Int64 │ String │ Float32 │
├─────┼───────┼────────┼─────────┤
│ 1 │ 1 │ x\" │ 1.0 │
Group 2 (1 row): :A = 2
Group 2 (1 row): A = 2
│ Row │ A │ B │ C │
│ │ Int64 │ String │ Float32 │
├─────┼───────┼─────────────┼─────────┤
│ 1 │ 2 │ ∀ε>0: x+ε>x │ 2.0 │
Group 3 (1 row): :A = 3
Group 3 (1 row): A = 3
│ Row │ A │ B │ C │
│ │ Int64 │ String │ Float32 │
├─────┼───────┼────────┼─────────┤
│ 1 │ 3 │ z\$ │ 3.0 │
Group 4 (1 row): :A = 4
Group 4 (1 row): A = 4
│ Row │ A │ B │ C │
│ │ Int64 │ String │ Float32 │
├─────┼───────┼────────┼─────────┤
Expand All @@ -950,20 +950,20 @@ module TestGrouping


@test sprint(show, "text/html", gd) ==
"<p><b>GroupedDataFrame with 4 groups based on key: :A</b></p>" *
"<p><i>First Group (1 row): :A = 1</i></p><table class=\"data-frame\">" *
"<p><b>GroupedDataFrame with 4 groups based on key: A</b></p>" *
"<p><i>First Group (1 row): A = 1</i></p><table class=\"data-frame\">" *
"<thead><tr><th></th><th>A</th><th>B</th><th>C</th></tr><tr><th></th>" *
"<th>Int64</th><th>String</th><th>Float32</th></tr></thead>" *
"<tbody><tr><th>1</th><td>1</td><td>x\"</td><td>1.0</td></tr></tbody>" *
"</table><p>&vellip;</p><p><i>Last Group (1 row): :A = 4</i></p>" *
"</table><p>&vellip;</p><p><i>Last Group (1 row): A = 4</i></p>" *
"<table class=\"data-frame\"><thead><tr><th></th><th>A</th><th>B</th><th>C</th></tr>" *
"<tr><th></th><th>Int64</th><th>String</th><th>Float32</th></tr></thead>" *
"<tbody><tr><th>1</th><td>4</td><td>A\\nC</td><td>4.0</td></tr></tbody></table>"

@test sprint(show, "text/latex", gd) == """
GroupedDataFrame with 4 groups based on key: :A
GroupedDataFrame with 4 groups based on key: A

First Group (1 row): :A = 1
First Group (1 row): A = 1

\\begin{tabular}{r|ccc}
\t& A & B & C\\\\
Expand All @@ -975,7 +975,7 @@ module TestGrouping

\$\\dots\$

Last Group (1 row): :A = 4
Last Group (1 row): A = 4

\\begin{tabular}{r|ccc}
\t& A & B & C\\\\
Expand All @@ -985,5 +985,35 @@ module TestGrouping
\t1 & 4 & A\\textbackslash{}nC & 4.0 \\\\
\\end{tabular}
"""

gd = groupby(DataFrame(a=[Symbol("&")], b=["&"]), [1,2])
@test sprint(show, gd) === """
GroupedDataFrame with 1 group based on keys: a, b
Group 1 (1 row): a = :&, b = "&"
│ Row │ a │ b │
│ │ Symbol │ String │
├─────┼────────┼────────┤
│ 1 │ & │ & │"""

@test sprint(show, "text/html", gd) ==
"<p><b>GroupedDataFrame with 1 group based on keys: a, b</b></p><p><i>" *
"First Group (1 row): a = :&amp;, b = \"&amp;\"</i></p>" *
"<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>b</th></tr>" *
"<tr><th></th><th>Symbol</th><th>String</th></tr></thead><tbody><tr><th>1</th>" *
"<td>&amp;</td><td>&amp;</td></tr></tbody></table>"

@test sprint(show, "text/latex", gd) == """
GroupedDataFrame with 1 group based on keys: a, b

First Group (1 row): a = :\\&, b = "\\&"

\\begin{tabular}{r|cc}
\t& a & b\\\\
\t\\hline
\t& Symbol & String\\\\
\t\\hline
\t1 & \\& & \\& \\\\
\\end{tabular}
"""
end
end