Skip to content

Commit 79507fa

Browse files
committed
Convert the one and two argument show methods to MIME"text/plain" methods
1 parent 6733162 commit 79507fa

File tree

11 files changed

+109
-253
lines changed

11 files changed

+109
-253
lines changed

docs/src/man/basics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ You can adjust how data frame is displayed by calling the `show` function manual
647647
`show(german, allcols=true)` does the same for columns, e.g.:
648648

649649
```jldoctest dataframe
650-
julia> show(german, allcols=true)
650+
julia> show(MIME("text/plain"), german, allcols=true)
651651
1000×10 DataFrame
652652
Row │ id Age Sex Job Housing Saving accounts Checking account Credit amount Duration Purpose
653653
│ Int64 Int64 String7 Int64 String7 String15 String15 Int64 Int64 String31

docs/src/man/customizing_output.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ julia> df
5353
5454
julia> # Using this option, no cell will be truncated if there is room to display it.
5555
56-
julia> show(df; truncate = 0)
56+
julia> show(MIME("text/plain"), df; truncate = 0)
5757
3×3 DataFrame
5858
Row │ a b c
5959
│ Int64 Float64 String
@@ -64,7 +64,7 @@ julia> show(df; truncate = 0)
6464
6565
julia> # Hide row numbers.
6666
67-
julia> show(df; show_row_number = false)
67+
julia> show(MIME("text/plain"), df; show_row_number = false)
6868
3×3 DataFrame
6969
a b c
7070
Int64 Float64 String
@@ -75,7 +75,7 @@ julia> show(df; show_row_number = false)
7575
7676
julia> # Hide the column element types in text output.
7777
78-
julia> show(df; eltypes = false)
78+
julia> show(MIME("text/plain"), df; eltypes = false)
7979
3×3 DataFrame
8080
Row │ a b c
8181
─────┼─────────────────────────────────────────────
@@ -140,7 +140,7 @@ julia> function parentheses_fmt(v, i, j)
140140
return v
141141
end;
142142
143-
julia> show(df; formatters = [parentheses_fmt])
143+
julia> show(MIME("text/plain"), df; formatters = [parentheses_fmt])
144144
7×5 DataFrame
145145
Row │ A B C D E
146146
│ Float64 Float64 Float64 Float64 Float64
@@ -190,6 +190,7 @@ julia> profit = DataFrame(
190190
);
191191
192192
julia> show(
193+
MIME("text/plain"),
193194
profit;
194195
# We use this option to align the summary rows with the data rows at the decimal
195196
# point.

docs/src/man/split_apply_combine.md

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -635,71 +635,7 @@ data frame and get a vector of results either use a comprehension or `collect`
635635
`GroupedDataFrame` into a vector first. Here are examples of both approaches:
636636

637637
```jldoctest sac
638-
julia> sdf_vec = collect(iris_gdf)
639-
3-element Vector{Any}:
640-
50×5 SubDataFrame
641-
Row │ SepalLength SepalWidth PetalLength PetalWidth Species
642-
│ Float64 Float64 Float64 Float64 String15
643-
─────┼───────────────────────────────────────────────────────────────
644-
1 │ 5.1 3.5 1.4 0.2 Iris-setosa
645-
2 │ 4.9 3.0 1.4 0.2 Iris-setosa
646-
3 │ 4.7 3.2 1.3 0.2 Iris-setosa
647-
4 │ 4.6 3.1 1.5 0.2 Iris-setosa
648-
5 │ 5.0 3.6 1.4 0.2 Iris-setosa
649-
6 │ 5.4 3.9 1.7 0.4 Iris-setosa
650-
7 │ 4.6 3.4 1.4 0.3 Iris-setosa
651-
8 │ 5.0 3.4 1.5 0.2 Iris-setosa
652-
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮
653-
44 │ 5.0 3.5 1.6 0.6 Iris-setosa
654-
45 │ 5.1 3.8 1.9 0.4 Iris-setosa
655-
46 │ 4.8 3.0 1.4 0.3 Iris-setosa
656-
47 │ 5.1 3.8 1.6 0.2 Iris-setosa
657-
48 │ 4.6 3.2 1.4 0.2 Iris-setosa
658-
49 │ 5.3 3.7 1.5 0.2 Iris-setosa
659-
50 │ 5.0 3.3 1.4 0.2 Iris-setosa
660-
35 rows omitted
661-
50×5 SubDataFrame
662-
Row │ SepalLength SepalWidth PetalLength PetalWidth Species
663-
│ Float64 Float64 Float64 Float64 String15
664-
─────┼───────────────────────────────────────────────────────────────────
665-
1 │ 7.0 3.2 4.7 1.4 Iris-versicolor
666-
2 │ 6.4 3.2 4.5 1.5 Iris-versicolor
667-
3 │ 6.9 3.1 4.9 1.5 Iris-versicolor
668-
4 │ 5.5 2.3 4.0 1.3 Iris-versicolor
669-
5 │ 6.5 2.8 4.6 1.5 Iris-versicolor
670-
6 │ 5.7 2.8 4.5 1.3 Iris-versicolor
671-
7 │ 6.3 3.3 4.7 1.6 Iris-versicolor
672-
8 │ 4.9 2.4 3.3 1.0 Iris-versicolor
673-
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮
674-
44 │ 5.0 2.3 3.3 1.0 Iris-versicolor
675-
45 │ 5.6 2.7 4.2 1.3 Iris-versicolor
676-
46 │ 5.7 3.0 4.2 1.2 Iris-versicolor
677-
47 │ 5.7 2.9 4.2 1.3 Iris-versicolor
678-
48 │ 6.2 2.9 4.3 1.3 Iris-versicolor
679-
49 │ 5.1 2.5 3.0 1.1 Iris-versicolor
680-
50 │ 5.7 2.8 4.1 1.3 Iris-versicolor
681-
35 rows omitted
682-
50×5 SubDataFrame
683-
Row │ SepalLength SepalWidth PetalLength PetalWidth Species
684-
│ Float64 Float64 Float64 Float64 String15
685-
─────┼──────────────────────────────────────────────────────────────────
686-
1 │ 6.3 3.3 6.0 2.5 Iris-virginica
687-
2 │ 5.8 2.7 5.1 1.9 Iris-virginica
688-
3 │ 7.1 3.0 5.9 2.1 Iris-virginica
689-
4 │ 6.3 2.9 5.6 1.8 Iris-virginica
690-
5 │ 6.5 3.0 5.8 2.2 Iris-virginica
691-
6 │ 7.6 3.0 6.6 2.1 Iris-virginica
692-
7 │ 4.9 2.5 4.5 1.7 Iris-virginica
693-
8 │ 7.3 2.9 6.3 1.8 Iris-virginica
694-
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮
695-
44 │ 6.8 3.2 5.9 2.3 Iris-virginica
696-
45 │ 6.7 3.3 5.7 2.5 Iris-virginica
697-
46 │ 6.7 3.0 5.2 2.3 Iris-virginica
698-
47 │ 6.3 2.5 5.0 1.9 Iris-virginica
699-
48 │ 6.5 3.0 5.2 2.0 Iris-virginica
700-
49 │ 6.2 3.4 5.4 2.3 Iris-virginica
701-
50 │ 5.9 3.0 5.1 1.8 Iris-virginica
702-
35 rows omitted
638+
julia> sdf_vec = collect(iris_gdf);
703639
704640
julia> map(nrow, sdf_vec)
705641
3-element Vector{Int64}:
@@ -942,7 +878,7 @@ julia> df = DataFrame(customer_id=["a", "b", "b", "b", "c", "c"],
942878
943879
julia> gdf = groupby(df, :customer_id, sort=true);
944880
945-
julia> show(gdf, allgroups=true)
881+
julia> show(MIME("text/plain"), gdf, allgroups=true)
946882
GroupedDataFrame with 3 groups based on key: customer_id
947883
Group 1 (1 row): customer_id = "a"
948884
Row │ customer_id transaction_id volume

src/abstractdataframe/abstractdataframe.jl

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,23 +2947,9 @@ as a `SubDataFrame`.
29472947
```jldoctest
29482948
julia> collect(Iterators.partition(DataFrame(x=1:5), 2))
29492949
3-element Vector{SubDataFrame{DataFrame, DataFrames.Index, UnitRange{Int64}}}:
2950-
2×1 SubDataFrame
2951-
Row │ x
2952-
│ Int64
2953-
─────┼───────
2954-
1 │ 1
2955-
2 │ 2
2956-
2×1 SubDataFrame
2957-
Row │ x
2958-
│ Int64
2959-
─────┼───────
2960-
1 │ 3
2961-
2 │ 4
2962-
1×1 SubDataFrame
2963-
Row │ x
2964-
│ Int64
2965-
─────┼───────
2966-
1 │ 5
2950+
SubDataFrame{DataFrame, DataFrames.Index, UnitRange{Int64}}(DataFrame(AbstractVector[[1, 2, 3, 4, 5]], DataFrames.Index(Dict(:x => 1), [:x]), nothing, nothing, true), DataFrames.Index(Dict(:x => 1), [:x]), 1:2)
2951+
SubDataFrame{DataFrame, DataFrames.Index, UnitRange{Int64}}(DataFrame(AbstractVector[[1, 2, 3, 4, 5]], DataFrames.Index(Dict(:x => 1), [:x]), nothing, nothing, true), DataFrames.Index(Dict(:x => 1), [:x]), 3:4)
2952+
SubDataFrame{DataFrame, DataFrames.Index, UnitRange{Int64}}(DataFrame(AbstractVector[[1, 2, 3, 4, 5]], DataFrames.Index(Dict(:x => 1), [:x]), nothing, nothing, true), DataFrames.Index(Dict(:x => 1), [:x]), 5:5)
29672953
```
29682954
"""
29692955
function Iterators.partition(df::AbstractDataFrame, n::Integer)

src/abstractdataframe/io.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ Base.show(io::IO, mime::MIME"text/csv", df::AbstractDataFrame) =
147147
printtable(io, df, header = true, separator = ',')
148148
Base.show(io::IO, mime::MIME"text/tab-separated-values", df::AbstractDataFrame) =
149149
printtable(io, df, header = true, separator = '\t')
150-
Base.show(io::IO, mime::MIME"text/plain", df::AbstractDataFrame; kwargs...) =
151-
show(io, df; kwargs...)
152150

153151
##############################################################################
154152
#

src/abstractdataframe/show.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ else
409409
end
410410

411411
"""
412-
show([io::IO, ]df::AbstractDataFrame;
412+
show([io::IO, ],
413+
::MIME"text/plain",
414+
df::AbstractDataFrame;
413415
allrows::Bool = !get(io, :limit, false),
414416
allcols::Bool = !get(io, :limit, false),
415417
allgroups::Bool = !get(io, :limit, false),
@@ -453,7 +455,7 @@ julia> using DataFrames
453455
454456
julia> df = DataFrame(A=1:3, B=["x", "y", "z"]);
455457
456-
julia> show(df, row_labels=nothing)
458+
julia> show(MIME("text/plain"), df, row_labels=nothing)
457459
3×2 DataFrame
458460
A B
459461
Int64 String
@@ -464,6 +466,7 @@ julia> show(df, row_labels=nothing)
464466
```
465467
"""
466468
function Base.show(io::IO,
469+
::MIME"text/plain",
467470
df::AbstractDataFrame;
468471
allrows::Bool = !get(io, :limit, false),
469472
allcols::Bool = !get(io, :limit, false),
@@ -480,15 +483,16 @@ function Base.show(io::IO,
480483
summary=summary, eltypes=eltypes, truncate=truncate, kwargs...)
481484
end
482485

483-
Base.show(df::AbstractDataFrame;
486+
Base.show(::MIME"text/plain",
487+
df::AbstractDataFrame;
484488
allrows::Bool = !get(stdout, :limit, true),
485489
allcols::Bool = !get(stdout, :limit, true),
486490
rowlabel::Symbol = :Row,
487491
summary::Bool = true,
488492
eltypes::Bool = true,
489493
truncate::Int = 32,
490494
kwargs...) =
491-
show(stdout, df;
495+
show(stdout, MIME("text/plain"), df;
492496
allrows=allrows, allcols=allcols, rowlabel=rowlabel, summary=summary,
493497
eltypes=eltypes, truncate=truncate, kwargs...)
494498

src/groupeddataframe/groupeddataframe.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ julia> gd[k]
187187
2 │ 1 2 5
188188
189189
julia> for g in gd
190-
println(g)
190+
show(MIME("text/plain"), g)
191+
println()
191192
end
192193
2×3 SubDataFrame
193194
Row │ a b c

src/groupeddataframe/show.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function Base.summary(io::IO, gd::GroupedDataFrame)
66
join(io, groupcols(gd), ", ")
77
end
88

9-
function Base.show(io::IO, gd::GroupedDataFrame;
9+
function Base.show(io::IO, ::MIME"text/plain", gd::GroupedDataFrame;
1010
allgroups::Bool = !get(io, :limit, false),
1111
allrows::Bool = !get(io, :limit, false),
1212
allcols::Bool = !get(io, :limit, false),
@@ -33,7 +33,7 @@ function Base.show(io::IO, gd::GroupedDataFrame;
3333
join(io, identified_groups, ", ")
3434
println(io)
3535

36-
show(io, gd[i]; summary=false,
36+
show(io, MIME("text/plain"), gd[i]; summary=false,
3737
allrows=allrows, allcols=allcols, rowlabel=rowlabel,
3838
truncate=truncate, kwargs...)
3939
end
@@ -90,7 +90,7 @@ function Base.show(io::IO, gd::GroupedDataFrame;
9090
join(io, identified_groups, ", ")
9191
println(io)
9292

93-
show(io, gd[1]; summary=false,
93+
show(io, MIME("text/plain"), gd[1]; summary=false,
9494
allrows=allrows, allcols=allcols, rowlabel=rowlabel,
9595
truncate=truncate, display_size=(h1, w), kwargs...)
9696

@@ -106,21 +106,22 @@ function Base.show(io::IO, gd::GroupedDataFrame;
106106
join(io, identified_groups, ", ")
107107
println(io)
108108

109-
show(io, gd[N]; summary=false,
109+
show(io, MIME("text/plain"), gd[N]; summary=false,
110110
allrows=allrows, allcols=allcols, rowlabel=rowlabel,
111111
truncate=truncate, display_size=(h2, w), kwargs...)
112112
end
113113
end
114114

115-
function Base.show(df::GroupedDataFrame;
115+
function Base.show(::MIME"text/plain",
116+
df::GroupedDataFrame;
116117
allrows::Bool = !get(stdout, :limit, true),
117118
allcols::Bool = !get(stdout, :limit, true),
118119
allgroups::Bool = !get(stdout, :limit, true),
119120
rowlabel::Symbol = :Row,
120121
summary::Bool = true,
121122
truncate::Int = 32,
122123
kwargs...) # -> Nothing
123-
return show(stdout, df;
124+
return show(stdout, MIME("text/plain"), df;
124125
allrows=allrows, allcols=allcols, allgroups=allgroups,
125126
rowlabel=rowlabel, summary=summary, truncate=truncate,
126127
kwargs...)

test/grouping.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ end
14341434
C=Float32[1.0, 2.0, 3.0, 4.0])
14351435
gd = groupby_checked(df, :A)
14361436
io = IOContext(IOBuffer(), :limit=>true)
1437-
show(io, gd)
1437+
show(io, MIME("text/plain"), gd)
14381438
str = String(take!(io.io))
14391439
summary_str = summary(gd)
14401440
@test summary_str == "GroupedDataFrame with 4 groups based on key: A"
@@ -1451,7 +1451,7 @@ end
14511451
│ Int64 String Float32
14521452
─────┼────────────────────────
14531453
1 │ 4 A\\nC 4.0"""
1454-
show(io, gd, allgroups=true)
1454+
show(io, MIME("text/plain"), gd, allgroups=true)
14551455
str = String(take!(io.io))
14561456
@test str == """
14571457
$summary_str
@@ -1476,17 +1476,17 @@ end
14761476
─────┼────────────────────────
14771477
1 │ 4 A\\nC 4.0"""
14781478

1479-
# Test two-argument show
1479+
# Test three-argument show
14801480
str1, dsize = capture_stdout() do
1481-
show(gd)
1481+
show(MIME("text/plain"), gd)
14821482
end
14831483
io = IOContext(IOBuffer(), :limit=>true, :displaysize=>dsize)
1484-
show(io, gd)
1484+
show(io, MIME("text/plain"), gd)
14851485
str2 = String(take!(io.io))
14861486
@test str1 == str2
14871487

14881488
# Test error when invalid keyword arguments are passed in text backend.
1489-
@test_throws ArgumentError show(stdout, gd, max_column_width="100px")
1489+
@test_throws ArgumentError show(stdout, MIME("text/plain"), gd, max_column_width="100px")
14901490

14911491
str = sprint(show, "text/html", gd)
14921492
@test str == "<p>" *
@@ -1591,7 +1591,7 @@ end
15911591
gd = groupby_checked(DataFrame(a=[Symbol("&")], b=["&"]), [1, 2])
15921592
summary_str = summary(gd)
15931593
@test summary_str == "GroupedDataFrame with 1 group based on keys: a, b"
1594-
@test sprint(show, gd) === """
1594+
@test sprint(show, MIME("text/plain"), gd) === """
15951595
$summary_str
15961596
Group 1 (1 row): a = :&, b = "&"
15971597
Row │ a b
@@ -1782,7 +1782,7 @@ end
17821782
@test isequal_typed(DataFrame(gdf), df)
17831783

17841784
# work around automatic trimming of trailing whitespace in most editors
1785-
@test sprint(show, groupby_checked(df, [])) ==
1785+
@test sprint(show, MIME("text/plain"), groupby_checked(df, [])) ==
17861786
"GroupedDataFrame with 1 group based on key: \n" *
17871787
"Group 1 (3 rows): \n" *
17881788
""" Row │ x1 x2 y

test/io.jl

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ end
14671467
nothing]
14681468

14691469
io = IOBuffer()
1470-
show(io, df)
1470+
show(io, MIME("text/plain"), df)
14711471
str = String(take!(io))
14721472

14731473
@test str == """
@@ -1488,7 +1488,7 @@ end
14881488
# TODO: update when https://github.com/KristofferC/Crayons.jl/issues/47 is resolved
14891489
if Base.get_have_color()
14901490
io = IOBuffer()
1491-
show(IOContext(io, :color => true), df)
1491+
show(IOContext(io, :color => true), MIME("text/plain"), df)
14921492
str = String(take!(io))
14931493
@test str == """
14941494
\e[1m9×2 DataFrame\e[0m
@@ -1755,16 +1755,6 @@ end
17551755
─────┼───────────────────────────────────
17561756
1 │ 01234567890123456789012345678901…"""
17571757

1758-
io = IOBuffer()
1759-
show(io, df)
1760-
str = String(take!(io))
1761-
@test str == """
1762-
1×1 DataFrame
1763-
Row │ x
1764-
│ String
1765-
─────┼───────────────────────────────────
1766-
1 │ 01234567890123456789012345678901…"""
1767-
17681758
# no truncation
17691759
io = IOBuffer()
17701760
show(io, df, truncate=0)

0 commit comments

Comments
 (0)