Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when pushing/appending with promote=true #3356

Merged
merged 6 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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