/
show.jl
351 lines (302 loc) · 12.6 KB
/
show.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
Base.summary(df::AbstractDataFrame) =
@sprintf("%d×%d %s", size(df)..., nameof(typeof(df)))
Base.summary(io::IO, df::AbstractDataFrame) = print(io, summary(df))
"""
DataFrames.ourstrwidth(io::IO, x::Any, buffer::IOBuffer, truncstring::Int)
Determine the number of characters that would be used to print a value.
"""
function ourstrwidth(io::IO, x::Any, buffer::IOBuffer, truncstring::Int)
truncate(buffer, 0)
ourshow(IOContext(buffer, :compact=>get(io, :compact, true)), x, truncstring)
return textwidth(String(take!(buffer)))
end
function truncatestring(s::AbstractString, truncstring::Int)
truncstring <= 0 && return s
totalwidth = 0
for (i, c) in enumerate(s)
totalwidth += textwidth(c)
if totalwidth > truncstring
return first(s, i-1) * '…'
end
end
return s
end
"""
DataFrames.ourshow(io::IO, x::Any, truncstring::Int)
Render a value to an `IO` object compactly using print.
`truncstring` indicates the approximate number of text characters width to truncate
the output (if it is a non-positive value then no truncation is applied).
"""
function ourshow(io::IO, x::Any, truncstring::Int; styled::Bool=false)
io_ctx = IOContext(io, :compact=>get(io, :compact, true), :typeinfo=>typeof(x))
sx = sprint(print, x, context=io_ctx)
sx = escape_string(sx, ()) # do not escape "
sx = truncatestring(sx, truncstring)
styled ? printstyled(io_ctx, sx, color=:light_black) : print(io_ctx, sx)
end
const SHOW_TABULAR_TYPES = Union{AbstractDataFrame, DataFrameRow, DataFrameRows,
DataFrameColumns, GroupedDataFrame}
# workaround Julia 1.0 for Char
ourshow(io::IO, x::Char, truncstring::Int; styled::Bool=false) =
ourshow(io, string(x), styled=styled, truncstring)
ourshow(io::IO, x::Nothing, truncstring::Int; styled::Bool=false) =
ourshow(io, "", styled=styled, truncstring)
ourshow(io::IO, x::SHOW_TABULAR_TYPES, truncstring::Int; styled::Bool=false) =
ourshow(io, summary(x), truncstring, styled=styled)
function ourshow(io::IO, x::Markdown.MD, truncstring::Int)
r = repr(x)
truncstring <= 0 && return chomp(truncstring)
len = min(length(r, 1, something(findfirst(==('\n'), r), lastindex(r)+1)-1), truncstring)
return print(io, len < length(r) - 1 ? first(r, len)*'…' : first(r, len))
end
# AbstractChar: https://github.com/JuliaLang/julia/pull/34730 (1.5.0-DEV.261)
# Irrational: https://github.com/JuliaLang/julia/pull/34741 (1.5.0-DEV.266)
if VERSION < v"1.5.0-DEV.261" || VERSION < v"1.5.0-DEV.266"
function ourshow(io::IO, x::T, truncstring::Int) where T <: Union{AbstractChar, Irrational}
io = IOContext(io, :compact=>get(io, :compact, true), :typeinfo=>typeof(x))
show(io, x)
end
end
# For most data frames, especially wide, columns having the same element type
# occur multiple times. batch_compacttype ensures that we compute string
# representation of a specific column element type only once and then reuse it.
function batch_compacttype(types::Vector{Any}, maxwidths::Vector{Int})
@assert length(types) == length(maxwidths)
cache = Dict{Any, String}()
return map(types, maxwidths) do T, maxwidth
get!(cache, T) do
compacttype(T, maxwidth)
end
end
end
function batch_compacttype(types::Vector{Any}, maxwidth::Int=8)
cache = Dict{Type, String}()
return map(types) do T
get!(cache, T) do
compacttype(T, maxwidth)
end
end
end
"""
compacttype(T::Type, maxwidth::Int=8, initial::Bool=true)
Return compact string representation of type `T`.
For displaying data frame we do not want string representation of type to be
longer than `maxwidth`. This function implements rules how type names are
cropped if they are longer than `maxwidth`.
"""
function compacttype(T::Type, maxwidth::Int=8)
maxwidth = max(8, maxwidth)
T === Any && return "Any"
T === Missing && return "Missing"
sT = string(T)
textwidth(sT) ≤ maxwidth && return sT
if T >: Missing
T = nonmissingtype(T)
sT = string(T)
suffix = "?"
textwidth(sT) ≤ maxwidth && return sT * suffix
else
suffix = ""
end
maxwidth -= 1 # we will add "…" at the end
# This is only type display shortening so we
# are OK with any T whose name starts with CategoricalValue here
if startswith(sT, "CategoricalValue") || startswith(sT, "CategoricalArrays.CategoricalValue")
sT = string(nameof(T))
if textwidth(sT) ≤ maxwidth
return sT * "…" * suffix
else
return (maxwidth ≥ 11 ? "Categorical…" : "Cat…") * suffix
end
elseif T isa Union
return "Union…" * suffix
else
sT = string(nameof(T))
end
cumwidth = 0
stop = 0
for (i, c) in enumerate(sT)
cumwidth += textwidth(c)
if cumwidth ≤ maxwidth
stop = i
else
break
end
end
return first(sT, stop) * "…" * suffix
end
function _show(io::IO,
df::AbstractDataFrame;
allrows::Bool = !get(io, :limit, false),
allcols::Bool = !get(io, :limit, false),
rowlabel::Symbol = :Row,
summary::Bool = true,
eltypes::Bool = true,
rowid = nothing,
truncate::Int = 32,
kwargs...)
_check_consistency(df)
names_str = names(df)
names_len = Int[textwidth(n) for n in names_str]
maxwidth = Int[max(9, nl) for nl in names_len]
types = Any[eltype(c) for c in eachcol(df)]
types_str = batch_compacttype(types, maxwidth)
if allcols && allrows
crop = :none
elseif allcols
crop = :vertical
elseif allrows
crop = :horizontal
else
crop = :both
end
# For consistency, if `kwargs` has `compact_printng`, we must use it.
compact_printing::Bool = get(kwargs, :compact_printing, get(io, :compact, true))
num_rows, num_cols = size(df)
# By default, we align the columns to the left unless they are numbers,
# which is checked in the following.
alignment = fill(:l, num_cols)
# Create the dictionary with the anchor regex that is used to align the
# floating points.
alignment_anchor_regex = Dict{Int, Vector{Regex}}()
# Columns composed of numbers are printed aligned to the right.
alignment_regex_vec = [r"\."]
for i = 1:num_cols
type_i = nonmissingtype(types[i])
if type_i <: Number
alignment_anchor_regex[i] = alignment_regex_vec
alignment[i] = :r
end
end
# Make sure that `truncate` does not hide the type and the column name.
maximum_columns_width = Int[truncate == 0 ? 0 : max(truncate + 1, l, textwidth(t))
for (l, t) in zip(names_len, types_str)]
# Check if the user wants to display a summary about the DataFrame that is
# being printed. This will be shown using the `title` option of
# `pretty_table`.
title = summary ? Base.summary(df) : ""
# If `rowid` is not `nothing`, then we are printing a data row. In this
# case, we will add this information using the row name column of
# PrettyTables.jl. Otherwise, we can just use the row number column.
if (rowid === nothing) || (ncol(df) == 0)
show_row_number::Bool = get(kwargs, :show_row_number, true)
row_names = nothing
# If the columns with row numbers is not shown, then we should not
# display a vertical line after the first column.
vlines = fill(1, show_row_number)
else
nrow(df) != 1 &&
throw(ArgumentError("rowid may be passed only with a single row data frame"))
# In this case, if the user does not want to show the row number, then
# we must hide the row name column, which is used to display the
# `rowid`.
if !get(kwargs, :show_row_number, true)
row_names = nothing
vlines = Int[]
else
row_names = [string(rowid)]
vlines = Int[1]
end
show_row_number = false
end
# Print the table with the selected options.
pretty_table(io, df;
alignment = alignment,
alignment_anchor_fallback = :r,
alignment_anchor_regex = alignment_anchor_regex,
compact_printing = compact_printing,
crop = crop,
crop_num_lines_at_beginning = 2,
ellipsis_line_skip = 3,
formatters = (_pretty_tables_general_formatter,),
header = (names_str, types_str),
header_alignment = :l,
hlines = [:header],
highlighters = (_PRETTY_TABLES_HIGHLIGHTER,),
maximum_columns_width = maximum_columns_width,
newline_at_end = false,
nosubheader = !eltypes,
row_name_alignment = :r,
row_name_crayon = Crayon(),
row_name_column_title = string(rowlabel),
row_names = row_names,
row_number_alignment = :r,
row_number_column_title = string(rowlabel),
show_row_number = show_row_number,
title = title,
vcrop_mode = :middle,
vlines = vlines,
kwargs...)
return nothing
end
"""
show([io::IO, ]df::AbstractDataFrame;
allrows::Bool = !get(io, :limit, false),
allcols::Bool = !get(io, :limit, false),
allgroups::Bool = !get(io, :limit, false),
rowlabel::Symbol = :Row,
summary::Bool = true,
eltypes::Bool = true,
truncate::Int = 32,
kwargs...)
Render a data frame to an I/O stream. The specific visual
representation chosen depends on the width of the display.
If `io` is omitted, the result is printed to `stdout`,
and `allrows`, `allcols` and `allgroups` default to `false`.
# Arguments
- `io::IO`: The I/O stream to which `df` will be printed.
- `df::AbstractDataFrame`: The data frame to print.
- `allrows::Bool `: Whether to print all rows, rather than
a subset that fits the device height. By default this is the case only if
`io` does not have the `IOContext` property `limit` set.
- `allcols::Bool`: Whether to print all columns, rather than
a subset that fits the device width. By default this is the case only if
`io` does not have the `IOContext` property `limit` set.
- `allgroups::Bool`: Whether to print all groups rather than
the first and last, when `df` is a `GroupedDataFrame`.
By default this is the case only if `io` does not have the `IOContext` property
`limit` set.
- `rowlabel::Symbol = :Row`: The label to use for the column containing row numbers.
- `summary::Bool = true`: Whether to print a brief string summary of the data frame.
- `eltypes::Bool = true`: Whether to print the column types under column names.
- `truncate::Int = 32`: the maximal display width the output can use before
being truncated (in the `textwidth` sense, excluding `…`).
If `truncate` is 0 or less, no truncation is applied.
- `kwargs...`: Any keyword argument supported by the function `pretty_table` of
PrettyTables.jl can be passed here to customize the output.
# Examples
```jldoctest
julia> using DataFrames
julia> df = DataFrame(A = 1:3, B = ["x", "y", "z"]);
julia> show(df, show_row_number=false)
3×2 DataFrame
A B
Int64 String
───────────────
1 x
2 y
3 z
```
"""
Base.show(io::IO,
df::AbstractDataFrame;
allrows::Bool = !get(io, :limit, false),
allcols::Bool = !get(io, :limit, false),
rowlabel::Symbol = :Row,
summary::Bool = true,
eltypes::Bool = true,
truncate::Int = 32,
kwargs...) =
_show(io, df; allrows=allrows, allcols=allcols, rowlabel=rowlabel,
summary=summary, eltypes=eltypes, truncate=truncate, kwargs...)
Base.show(df::AbstractDataFrame;
allrows::Bool = !get(stdout, :limit, true),
allcols::Bool = !get(stdout, :limit, true),
rowlabel::Symbol = :Row,
summary::Bool = true,
eltypes::Bool = true,
truncate::Int = 32,
kwargs...) =
show(stdout, df;
allrows=allrows, allcols=allcols, rowlabel=rowlabel, summary=summary,
eltypes=eltypes, truncate=truncate, kwargs...)