From 453b4464b996d918f1dbce5034e739be16bad0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sat, 15 Jul 2023 09:13:39 +0200 Subject: [PATCH] Improve error message when pushing/appending with promote=true (#3356) --- src/dataframe/insertion.jl | 34 +++++++++++++++++++--------------- test/insertion.jl | 29 ++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/dataframe/insertion.jl b/src/dataframe/insertion.jl index 8f71399854..ec148065c0 100644 --- a/src/dataframe/insertion.jl +++ b/src/dataframe/insertion.jl @@ -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 @@ -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) @@ -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 @@ -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 - diff --git a/test/insertion.jl b/test/insertion.jl index 0b2039f62c..19bc175e54 100644 --- a/test/insertion.jl +++ b/test/insertion.jl @@ -1,6 +1,6 @@ module TestInsertion -using DataFrames, Test, Logging, DataStructures +using DataFrames, Test, Logging, DataStructures, PooledArrays const ≅ = isequal @testset "push!(df, row)" begin @@ -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