Skip to content

Commit

Permalink
Improve error message when pushing/appending with promote=true (#3356)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Jul 15, 2023
1 parent 02d1c99 commit 453b446
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
34 changes: 19 additions & 15 deletions src/dataframe/insertion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,12 @@ function _append_or_prepend!(df1::DataFrame, df2::AbstractDataFrame; cols::Symbo
deleteat!(col, 1:length(col) - nrow1)
end
end
@error "Error adding value to column :$(_names(df1)[current_col]). " *
"Maybe it was forgotten to require column " *
"element type promotion, which can be done " *
"by passing the promote=true keyword argument."
if promote
@error "Error adding value to column :$(_names(df1)[current_col])."
else
@error "Error adding value to column :$(_names(df1)[current_col]). " *
"Maybe you forgot passing `promote=true`?"
end
rethrow(err)
end

Expand Down Expand Up @@ -689,9 +691,12 @@ function _row_inserter!(df::DataFrame, loc::Integer, row::Any,
end
@assert length(col2) == nrows
end
@error "Error adding value to column :$(_names(df)[current_col]). " *
"Maybe it was forgotten to ask for column element type promotion, " *
"which can be done by passing the promote=true keyword argument."
if promote
@error "Error adding value to column :$(_names(df)[current_col])."
else
@error "Error adding value to column :$(_names(df)[current_col]). " *
"Maybe you forgot passing `promote=true`?"
end
rethrow(err)
end
_drop_all_nonnote_metadata!(df)
Expand Down Expand Up @@ -866,9 +871,7 @@ function _row_inserter!(df::DataFrame, loc::Integer,
@assert length(col2) == nrows
end
@error "Error adding value to column :$colname. " *
"Maybe it was forgotten to ask for column " *
"element type promotion, which can be done " *
"by passing the promote=true keyword argument."
"Maybe you forgot passing `promote=true`?"
rethrow(err)
end
else
Expand Down Expand Up @@ -972,13 +975,14 @@ function _row_inserter!(df::DataFrame, loc::Integer,
end
@assert length(col2) == nrows
end
@error "Error adding value to column :$(_names(df)[current_col]). " *
"Maybe it was forgotten to ask for column " *
"element type promotion, which can be done " *
"by passing the promote=true keyword argument."
if promote
@error "Error adding value to column :$(_names(df)[current_col])."
else
@error "Error adding value to column :$(_names(df)[current_col]). " *
"Maybe you forgot passing `promote=true`?"
end
rethrow(err)
end
_drop_all_nonnote_metadata!(df)
return df
end

29 changes: 28 additions & 1 deletion test/insertion.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TestInsertion

using DataFrames, Test, Logging, DataStructures
using DataFrames, Test, Logging, DataStructures, PooledArrays
const = isequal

@testset "push!(df, row)" begin
Expand Down Expand Up @@ -1343,4 +1343,31 @@ end
end
end

@testset "PooledArray error #3356" begin
buf = IOBuffer()
sl = SimpleLogger(buf)

with_logger(sl) do
@test_throws ErrorException append!(DataFrame(x=PooledArray(1:255, UInt8)),
DataFrame(x=PooledArray(256:500, UInt8)))
@test_throws ErrorException append!(DataFrame(x=PooledArray(1:255, UInt8)),
DataFrame(x=PooledArray(256:500, UInt8)),
promote=true)
@test_throws ErrorException push!(DataFrame(x=PooledArray(1:255, UInt8)), [1000])
@test_throws ErrorException push!(DataFrame(x=PooledArray(1:255, UInt8)), [1000],
promote=true)
@test_throws ErrorException push!(DataFrame(x=PooledArray(1:255, UInt8)), (x=1000,))
@test_throws ErrorException push!(DataFrame(x=PooledArray(1:255, UInt8)), (x=1000,),
promote=true)
@test_throws ErrorException push!(DataFrame(x=PooledArray(1:255, UInt8)), (x=1000,),
cols=:union, promote=true)
@test_throws ErrorException push!(DataFrame(x=PooledArray(1:255, UInt8)), (x=1000,),
cols=:union, promote=false)
@test_throws ErrorException push!(DataFrame(x=PooledArray(1:255, UInt8)), (x="x",),
cols=:union, promote=true)
@test_throws MethodError push!(DataFrame(x=PooledArray(1:255, UInt8)), (x="x",),
cols=:union, promote=false)
end
end

end # module

0 comments on commit 453b446

Please sign in to comment.