Skip to content

Commit 77909b4

Browse files
committed
Update show.jl
1 parent bef931f commit 77909b4

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/abstractdataset/show.jl

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function compacttype(T::Type, maxwidth::Int=8)
157157
end
158158

159159
function _show(io::IO,
160-
df::AbstractDataset;
160+
ds::AbstractDataset;
161161
allrows::Bool = !get(io, :limit, false),
162162
allcols::Bool = !get(io, :limit, false),
163163
rowlabel::Symbol = :Row,
@@ -167,13 +167,13 @@ function _show(io::IO,
167167
truncate::Int = 32,
168168
kwargs...)
169169

170-
_check_consistency(df)
170+
_check_consistency(ds)
171171

172-
names_str = names(df)
173-
if typeof(df) <: SubDataset
174-
column_formats = _getformats_for_show(df)
172+
names_str = names(ds)
173+
if typeof(ds) <: SubDataset
174+
column_formats = _getformats_for_show(ds)
175175
else
176-
column_formats = _getformats(df)
176+
column_formats = _getformats(ds)
177177
end
178178
names_format = fill("identity", length(names_str))
179179
_pt_formmatters_ = Function[]
@@ -186,7 +186,7 @@ function _show(io::IO,
186186
pt_formatter = ntuple(i->_pt_formmatters_[i], length(_pt_formmatters_))
187187
names_len = Int[textwidth(n) for n in names_str]
188188
maxwidth = Int[max(9, nl) for nl in names_len]
189-
types = Any[eltype(c) for c in eachcol(df)]
189+
types = Any[eltype(c) for c in eachcol(ds)]
190190
types_str = batch_compacttype(types, maxwidth)
191191

192192
if allcols && allrows
@@ -202,7 +202,7 @@ function _show(io::IO,
202202
# For consistency, if `kwargs` has `compact_printng`, we must use it.
203203
compact_printing::Bool = get(kwargs, :compact_printing, get(io, :compact, true))
204204

205-
num_rows, num_cols = size(df)
205+
num_rows, num_cols = size(ds)
206206

207207
# By default, we align the columns to the left unless they are numbers,
208208
# which is checked in the following.
@@ -242,20 +242,20 @@ function _show(io::IO,
242242
# Check if the user wants to display a summary about the DataFrame that is
243243
# being printed. This will be shown using the `title` option of
244244
# `pretty_table`.
245-
title = summary ? Base.summary(df) : ""
245+
title = summary ? Base.summary(ds) : ""
246246

247247
# If `rowid` is not `nothing`, then we are printing a data row. In this
248248
# case, we will add this information using the row name column of
249249
# PrettyTables.jl. Otherwise, we can just use the row number column.
250-
if (rowid === nothing) || (ncol(df) == 0)
250+
if (rowid === nothing) || (ncol(ds) == 0)
251251
show_row_number::Bool = get(kwargs, :show_row_number, true)
252252
row_names = nothing
253253

254254
# If the columns with row numbers is not shown, then we should not
255255
# display a vertical line after the first column.
256256
vlines = fill(1, show_row_number)
257257
else
258-
nrow(df) != 1 &&
258+
nrow(ds) != 1 &&
259259
throw(ArgumentError("rowid may be passed only with a single row data frame"))
260260

261261
# In this case, if the user does not want to show the row number, then
@@ -271,13 +271,17 @@ function _show(io::IO,
271271

272272
show_row_number = false
273273
end
274-
# if isgrouped(df)
275-
# extrahlines = view(index(df).starts,1:index(df).ngroups[]) .- 1
274+
# if isgrouped(ds)
275+
# extrahlines = view(index(ds).starts,1:index(ds).ngroups[]) .- 1
276276
# else
277277
extrahlines = [0]
278278
# end
279279
# Print the table with the selected options.
280-
pretty_table(io, df;
280+
# currently pretty_table is very slow for large tables, the workaround is to use only few rows
281+
if allrows && nrow(ds)>1000
282+
@warn "Datasets only shows maximum of 1000 rows"
283+
end
284+
pretty_table(io, view(ds, 1:min(1000, nrow(ds)), :);
281285
alignment = alignment,
282286
alignment_anchor_fallback = :r,
283287
alignment_anchor_regex = alignment_anchor_regex,
@@ -302,15 +306,15 @@ function _show(io::IO,
302306
row_number_column_title = string(rowlabel),
303307
show_row_number = show_row_number,
304308
title = title,
305-
vcrop_mode = :middle,
309+
# vcrop_mode = :middle,
306310
vlines = vlines,
307311
kwargs...)
308312

309313
return nothing
310314
end
311315

312316
"""
313-
show([io::IO, ]df::AbstractDataset;
317+
show([io::IO, ]ds::AbstractDataset;
314318
allrows::Bool = !get(io, :limit, false),
315319
allcols::Bool = !get(io, :limit, false),
316320
allgroups::Bool = !get(io, :limit, false),
@@ -327,16 +331,16 @@ If `io` is omitted, the result is printed to `stdout`,
327331
and `allrows`, `allcols` and `allgroups` default to `false`.
328332
329333
# Arguments
330-
- `io::IO`: The I/O stream to which `df` will be printed.
331-
- `df::AbstractDataset`: The data frame to print.
334+
- `io::IO`: The I/O stream to which `ds` will be printed.
335+
- `ds::AbstractDataset`: The data frame to print.
332336
- `allrows::Bool `: Whether to print all rows, rather than
333337
a subset that fits the device height. By default this is the case only if
334338
`io` does not have the `IOContext` property `limit` set.
335339
- `allcols::Bool`: Whether to print all columns, rather than
336340
a subset that fits the device width. By default this is the case only if
337341
`io` does not have the `IOContext` property `limit` set.
338342
- `allgroups::Bool`: Whether to print all groups rather than
339-
the first and last, when `df` is a `GroupedDataFrame`.
343+
the first and last, when `ds` is a `GroupedDataFrame`.
340344
By default this is the case only if `io` does not have the `IOContext` property
341345
`limit` set.
342346
- `rowlabel::Symbol = :Row`: The label to use for the column containing row numbers.
@@ -352,9 +356,9 @@ and `allrows`, `allcols` and `allgroups` default to `false`.
352356
```jldoctest
353357
julia> using DataFrames
354358
355-
julia> df = DataFrame(A = 1:3, B = ["x", "y", "z"]);
359+
julia> ds = DataFrame(A = 1:3, B = ["x", "y", "z"]);
356360
357-
julia> show(df, show_row_number=false)
361+
julia> show(ds, show_row_number=false)
358362
3×2 DataFrame
359363
A B
360364
Int64 String
@@ -364,26 +368,28 @@ julia> show(df, show_row_number=false)
364368
3 z
365369
```
366370
"""
367-
Base.show(io::IO,
368-
df::AbstractDataset;
371+
function Base.show(io::IO,
372+
ds::AbstractDataset;
369373
allrows::Bool = !get(io, :limit, false),
370374
allcols::Bool = !get(io, :limit, false),
371375
rowlabel::Symbol = :Row,
372376
summary::Bool = true,
373377
eltypes::Bool = true,
374378
truncate::Int = 32,
375-
kwargs...) =
376-
_show(io, df; allrows=allrows, allcols=allcols, rowlabel=rowlabel,
379+
kwargs...)
380+
_show(io, ds; allrows=allrows, allcols=allcols, rowlabel=rowlabel,
377381
summary=summary, eltypes=eltypes, truncate=truncate, kwargs...)
382+
end
378383

379-
Base.show(df::AbstractDataset;
384+
function Base.show(ds::AbstractDataset;
380385
allrows::Bool = !get(stdout, :limit, true),
381386
allcols::Bool = !get(stdout, :limit, true),
382387
rowlabel::Symbol = :Row,
383388
summary::Bool = true,
384389
eltypes::Bool = true,
385390
truncate::Int = 32,
386-
kwargs...) =
387-
show(stdout, df;
391+
kwargs...)
392+
show(stdout, ds;
388393
allrows=allrows, allcols=allcols, rowlabel=rowlabel, summary=summary,
389394
eltypes=eltypes, truncate=truncate, kwargs...)
395+
end

0 commit comments

Comments
 (0)