Skip to content

Commit c43c8da

Browse files
authored
Improved REPL printing for GroupedDataFrames (#3107)
1 parent 7c1a888 commit c43c8da

File tree

3 files changed

+267
-78
lines changed

3 files changed

+267
-78
lines changed

docs/src/man/split_apply_combine.md

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -215,45 +215,20 @@ First Group (50 rows): Species = "Iris-setosa"
215215
─────┼───────────────────────────────────────────────────────────────
216216
1 │ 5.1 3.5 1.4 0.2 Iris-setosa
217217
2 │ 4.9 3.0 1.4 0.2 Iris-setosa
218-
3 │ 4.7 3.2 1.3 0.2 Iris-setosa
219-
4 │ 4.6 3.1 1.5 0.2 Iris-setosa
220-
5 │ 5.0 3.6 1.4 0.2 Iris-setosa
221-
6 │ 5.4 3.9 1.7 0.4 Iris-setosa
222-
7 │ 4.6 3.4 1.4 0.3 Iris-setosa
223-
8 │ 5.0 3.4 1.5 0.2 Iris-setosa
224218
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮
225-
43 │ 4.4 3.2 1.3 0.2 Iris-setosa
226-
44 │ 5.0 3.5 1.6 0.6 Iris-setosa
227-
45 │ 5.1 3.8 1.9 0.4 Iris-setosa
228-
46 │ 4.8 3.0 1.4 0.3 Iris-setosa
229-
47 │ 5.1 3.8 1.6 0.2 Iris-setosa
230-
48 │ 4.6 3.2 1.4 0.2 Iris-setosa
231219
49 │ 5.3 3.7 1.5 0.2 Iris-setosa
232220
50 │ 5.0 3.3 1.4 0.2 Iris-setosa
233-
34 rows omitted
221+
46 rows omitted
234222
235223
Last Group (50 rows): Species = "Iris-virginica"
236224
Row │ SepalLength SepalWidth PetalLength PetalWidth Species
237225
│ Float64 Float64 Float64 Float64 String15
238226
─────┼──────────────────────────────────────────────────────────────────
239227
1 │ 6.3 3.3 6.0 2.5 Iris-virginica
240228
2 │ 5.8 2.7 5.1 1.9 Iris-virginica
241-
3 │ 7.1 3.0 5.9 2.1 Iris-virginica
242-
4 │ 6.3 2.9 5.6 1.8 Iris-virginica
243-
5 │ 6.5 3.0 5.8 2.2 Iris-virginica
244-
6 │ 7.6 3.0 6.6 2.1 Iris-virginica
245-
7 │ 4.9 2.5 4.5 1.7 Iris-virginica
246-
8 │ 7.3 2.9 6.3 1.8 Iris-virginica
247229
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮
248-
43 │ 5.8 2.7 5.1 1.9 Iris-virginica
249-
44 │ 6.8 3.2 5.9 2.3 Iris-virginica
250-
45 │ 6.7 3.3 5.7 2.5 Iris-virginica
251-
46 │ 6.7 3.0 5.2 2.3 Iris-virginica
252-
47 │ 6.3 2.5 5.0 1.9 Iris-virginica
253-
48 │ 6.5 3.0 5.2 2.0 Iris-virginica
254-
49 │ 6.2 3.4 5.4 2.3 Iris-virginica
255230
50 │ 5.9 3.0 5.1 1.8 Iris-virginica
256-
34 rows omitted
231+
47 rows omitted
257232
258233
julia> combine(gdf, :PetalLength => mean)
259234
3×2 DataFrame
@@ -520,45 +495,20 @@ First Group (50 rows): Species = "Iris-setosa"
520495
─────┼───────────────────────────────────────────────────────────────
521496
1 │ 5.1 3.5 1.4 0.2 Iris-setosa
522497
2 │ 4.9 3.0 1.4 0.2 Iris-setosa
523-
3 │ 4.7 3.2 1.3 0.2 Iris-setosa
524-
4 │ 4.6 3.1 1.5 0.2 Iris-setosa
525-
5 │ 5.0 3.6 1.4 0.2 Iris-setosa
526-
6 │ 5.4 3.9 1.7 0.4 Iris-setosa
527-
7 │ 4.6 3.4 1.4 0.3 Iris-setosa
528-
8 │ 5.0 3.4 1.5 0.2 Iris-setosa
529498
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮
530-
43 │ 4.4 3.2 1.3 0.2 Iris-setosa
531-
44 │ 5.0 3.5 1.6 0.6 Iris-setosa
532-
45 │ 5.1 3.8 1.9 0.4 Iris-setosa
533-
46 │ 4.8 3.0 1.4 0.3 Iris-setosa
534-
47 │ 5.1 3.8 1.6 0.2 Iris-setosa
535-
48 │ 4.6 3.2 1.4 0.2 Iris-setosa
536499
49 │ 5.3 3.7 1.5 0.2 Iris-setosa
537500
50 │ 5.0 3.3 1.4 0.2 Iris-setosa
538-
34 rows omitted
501+
46 rows omitted
539502
540503
Last Group (50 rows): Species = "Iris-virginica"
541504
Row │ SepalLength SepalWidth PetalLength PetalWidth Species
542505
│ Float64 Float64 Float64 Float64 String15
543506
─────┼──────────────────────────────────────────────────────────────────
544507
1 │ 6.3 3.3 6.0 2.5 Iris-virginica
545508
2 │ 5.8 2.7 5.1 1.9 Iris-virginica
546-
3 │ 7.1 3.0 5.9 2.1 Iris-virginica
547-
4 │ 6.3 2.9 5.6 1.8 Iris-virginica
548-
5 │ 6.5 3.0 5.8 2.2 Iris-virginica
549-
6 │ 7.6 3.0 6.6 2.1 Iris-virginica
550-
7 │ 4.9 2.5 4.5 1.7 Iris-virginica
551-
8 │ 7.3 2.9 6.3 1.8 Iris-virginica
552509
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮
553-
43 │ 5.8 2.7 5.1 1.9 Iris-virginica
554-
44 │ 6.8 3.2 5.9 2.3 Iris-virginica
555-
45 │ 6.7 3.3 5.7 2.5 Iris-virginica
556-
46 │ 6.7 3.0 5.2 2.3 Iris-virginica
557-
47 │ 6.3 2.5 5.0 1.9 Iris-virginica
558-
48 │ 6.5 3.0 5.2 2.0 Iris-virginica
559-
49 │ 6.2 3.4 5.4 2.3 Iris-virginica
560510
50 │ 5.9 3.0 5.1 1.8 Iris-virginica
561-
34 rows omitted
511+
47 rows omitted
562512
563513
julia> combine(gd, valuecols(gd) .=> mean)
564514
3×5 DataFrame

src/groupeddataframe/show.jl

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,65 @@ function Base.show(io::IO, gd::GroupedDataFrame;
3838
truncate=truncate, kwargs...)
3939
end
4040
else
41-
if N > 0
42-
nrows = size(gd[1], 1)
43-
rows = nrows > 1 ? "rows" : "row"
44-
45-
identified_groups = [string(col, " = ", repr(gd[1][1, col]))
46-
for col in gd.cols]
41+
N > 0 || return
4742

48-
print(io, "\nFirst Group ($nrows $rows): ")
49-
join(io, identified_groups, ", ")
50-
println(io)
43+
(h, w) = displaysize(io)
5144

52-
show(io, gd[1]; summary=false,
53-
allrows=allrows, allcols=allcols, rowlabel=rowlabel,
54-
truncate=truncate, kwargs...)
45+
if h > 0
46+
h -= 2 # two lines are already used for header and gap between groups
47+
h <= 5 && (h = 5) # if too small to fit, print fully compact
5548
end
56-
if N > 1
57-
nrows = size(gd[N], 1)
58-
rows = nrows > 1 ? "rows" : "row"
5949

60-
identified_groups = [string(col, " = ", repr(gd[N][1, col]))
61-
for col in gd.cols]
62-
print(io, "\n")
63-
print(io, "\nLast Group ($nrows $rows): ")
64-
join(io, identified_groups, ", ")
65-
println(io)
50+
h1 = h2 = h # display heights available for first and last groups
51+
if N > 1 && h > 0
6652

67-
show(io, gd[N]; summary=false,
68-
allrows=allrows, allcols=allcols, rowlabel=rowlabel,
69-
truncate=truncate, kwargs...)
53+
# line height of groups if printed in full (nrows + 3 extra for header)
54+
g1 = size(gd[1], 1) + 3
55+
g2 = size(gd[N], 1) + 3
56+
57+
if g1 + g2 > h # won't fit on screen
58+
if g1 < h ÷ 2
59+
h2 = h - g1 - 2 # show first group fully, squash last
60+
elseif g2 < h ÷ 2
61+
h1 = h - g2 - 2 # show last group fully, squash first
62+
else
63+
# squash both groups
64+
h += 1
65+
h2 = h ÷ 2
66+
h1 = h - h2
67+
end
68+
end
7069
end
70+
71+
nrows = size(gd[1], 1)
72+
rows = nrows > 1 ? "rows" : "row"
73+
74+
identified_groups = [string(col, " = ", repr(gd[1][1, col]))
75+
for col in gd.cols]
76+
77+
print(io, "\nFirst Group ($nrows $rows): ")
78+
join(io, identified_groups, ", ")
79+
println(io)
80+
81+
show(io, gd[1]; summary=false,
82+
allrows=allrows, allcols=allcols, rowlabel=rowlabel,
83+
truncate=truncate, display_size=(h1, w), kwargs...)
84+
85+
N > 1 || return
86+
87+
nrows = size(gd[N], 1)
88+
rows = nrows > 1 ? "rows" : "row"
89+
90+
identified_groups = [string(col, " = ", repr(gd[N][1, col]))
91+
for col in gd.cols]
92+
print(io, "\n")
93+
print(io, "\nLast Group ($nrows $rows): ")
94+
join(io, identified_groups, ", ")
95+
println(io)
96+
97+
show(io, gd[N]; summary=false,
98+
allrows=allrows, allcols=allcols, rowlabel=rowlabel,
99+
truncate=truncate, display_size=(h2, w), kwargs...)
71100
end
72101
end
73102

0 commit comments

Comments
 (0)