Error message prints entire dataframe

I encounter an error when i improperly attempt to use the length() function on a dataframe.
The error message ends up printing the entire dataframe like so:

Error: MethodError(length, (5189Γ—16 DataFrame
and then it prints over 5k rows of the dataframe.

How do i avoid printing the entire dataframe when the error is caught?

I can’t replicate

julia> using DataFrames

julia> df = DataFrame(rand(10_000, 10), :auto);

julia> length(df)
ERROR: MethodError: no method matching length(::DataFrame)
Closest candidates are:
  length(::Union{Base.KeySet, Base.ValueIterator}) at abstractdict.jl:58
  length(::Union{LinearAlgebra.Adjoint{T, S}, LinearAlgebra.Transpose{T, S}} where {T, S}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/adjtrans.jl:195
  length(::Union{DataStructures.OrderedRobinDict, DataStructures.RobinDict}) at /home/peterwd/.julia/packages/DataStructures/nBjdy/src/ordered_robin_dict.jl:86
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

Could you show exactly your length command?

EDIT: I can replicate

julia> df = DataFrame(x = [1, 2]);

julia> try 
           length(df)
       catch e
           println(e)
       end
MethodError(length, (2Γ—1 DataFrame
 Row β”‚ x     
     β”‚ Int64 
─────┼───────
   1 β”‚     1
   2 β”‚     2,), 0x00000000000073ca)

Not sure the best way to fix it.

1 Like

right, so as you can see when you print the error it ends up printing the entire dataframe argument.
i don’t want that since i’m dealing with very large dataframes (millions of rows potentially)

I made an issue here in the DataFrames.jl repo.

1 Like

thanks for opening the issue.
in searching for possible solutions, i encountered the ClearStacktrace.jl package. i don’t see how what they do there could help me. also the package is probably outdated since they overload Base.show_stacktrace which doesn’t seem to exist anymore.

As noted in the Julia issue here you can use the function showerror for better printing of errors.

1 Like