I have a kind of complicated dataframe df
, and I want to append one of its rows to the end, i.e., append!(df, df[1,:])
. When I try this, I get an error
julia> append!(df, DataFrame(df[1,:]))
┌ Error: Error adding value to column :ETE_FP.
└ @ DataFrames ~/.julia/packages/DataFrames/BM4OQ/src/dataframe/dataframe.jl:1423
ERROR: AssertionError: length(col) == targetrows
Stacktrace:
[1] append!(df1::DataFrame, df2::DataFrame; cols::Symbol, promote::Bool)
@ DataFrames ~/.julia/packages/DataFrames/BM4OQ/src/dataframe/dataframe.jl:1407
[2] append!(df1::DataFrame, df2::DataFrame)
@ DataFrames ~/.julia/packages/DataFrames/BM4OQ/src/dataframe/dataframe.jl:1315
[3] top-level scope
@ REPL[290]:1
I tried differently:
julia> append!(df, df[1,:])
ERROR: ArgumentError: 'DataFrameRow{DataFrame, DataFrames.Index}' iterates 'String' values, which doesn't satisfy the Tables.jl `AbstractRow` interface
Stacktrace:
[1] invalidtable(#unused#::DataFrameRow{DataFrame, DataFrames.Index}, #unused#::String)
@ Tables ~/.julia/packages/Tables/OWzlh/src/tofromdatavalues.jl:42
[2] iterate
@ ~/.julia/packages/Tables/OWzlh/src/tofromdatavalues.jl:48 [inlined]
[3] buildcolumns
@ ~/.julia/packages/Tables/OWzlh/src/fallbacks.jl:199 [inlined]
[4] columns
@ ~/.julia/packages/Tables/OWzlh/src/fallbacks.jl:262 [inlined]
[5] DataFrame(x::DataFrameRow{DataFrame, DataFrames.Index}; copycols::Bool)
@ DataFrames ~/.julia/packages/DataFrames/BM4OQ/src/other/tables.jl:58
[6] #append!#785
@ ~/.julia/packages/DataFrames/BM4OQ/src/other/tables.jl:69 [inlined]
[7] append!(df::DataFrame, table::DataFrameRow{DataFrame, DataFrames.Index})
@ DataFrames ~/.julia/packages/DataFrames/BM4OQ/src/other/tables.jl:65
[8] top-level scope
@ REPL[293]:1
The column “ETE_FP” is just a vector of Int
s. I was confused by this error, so I tried to create a smaller example. I couldn’t get the same error, but I got another one…
I created a simple dataframe using Julia 1.6:
julia> mydf = DataFrame("mycol" => [257; 258])
2×1 DataFrame
Row │ mycol
│ Int64
─────┼────────
1 │ 257
2 │ 258
and I want to append the first row to mydf
, but the following four methods (2 for append!
and 2 for push!
) are giving errors except for the fourth method. Why does append!
not work?
1: julia> append!(mydf, mydf[1,!])
ERROR: MethodError: no method matching getindex(::DataFrame, ::Int64, ::typeof(!))
Closest candidates are:
getindex(::AbstractDataFrame, ::Integer, ::Colon) at /Users/jakeroth/.julia/packages/DataFrames/BM4OQ/src/dataframerow/dataframerow.jl:210
getindex(::AbstractDataFrame, ::Integer, ::Union{Colon, Regex, AbstractVector{T} where T, All, Between, Cols, InvertedIndex}) at /Users/jakeroth/.julia/packages/DataFrames/BM4OQ/src/dataframerow/dataframerow.jl:208
getindex(::DataFrame, ::Integer, ::Union{AbstractString, Symbol}) at /Users/jakeroth/.julia/packages/DataFrames/BM4OQ/src/dataframe/dataframe.jl:488
...
Stacktrace:
[1] top-level scope
@ REPL[283]:1
2: julia> append!(mydf, mydf[1,:])
ERROR: ArgumentError: 'DataFrameRow{DataFrame, DataFrames.Index}' iterates 'Int64' values, which doesn't satisfy the Tables.jl `AbstractRow` interface
Stacktrace:
[1] invalidtable(#unused#::DataFrameRow{DataFrame, DataFrames.Index}, #unused#::Int64)
@ Tables ~/.julia/packages/Tables/OWzlh/src/tofromdatavalues.jl:42
[2] iterate
@ ~/.julia/packages/Tables/OWzlh/src/tofromdatavalues.jl:48 [inlined]
[3] buildcolumns
@ ~/.julia/packages/Tables/OWzlh/src/fallbacks.jl:199 [inlined]
[4] columns
@ ~/.julia/packages/Tables/OWzlh/src/fallbacks.jl:262 [inlined]
[5] DataFrame(x::DataFrameRow{DataFrame, DataFrames.Index}; copycols::Bool)
@ DataFrames ~/.julia/packages/DataFrames/BM4OQ/src/other/tables.jl:58
[6] #append!#785
@ ~/.julia/packages/DataFrames/BM4OQ/src/other/tables.jl:69 [inlined]
[7] append!(df::DataFrame, table::DataFrameRow{DataFrame, DataFrames.Index})
@ DataFrames ~/.julia/packages/DataFrames/BM4OQ/src/other/tables.jl:65
[8] top-level scope
@ REPL[277]:1
3: julia> push!(mydf,mydf[1,!])
ERROR: MethodError: no method matching getindex(::DataFrame, ::Int64, ::typeof(!))
Closest candidates are:
getindex(::AbstractDataFrame, ::Integer, ::Colon) at /Users/jakeroth/.julia/packages/DataFrames/BM4OQ/src/dataframerow/dataframerow.jl:210
getindex(::AbstractDataFrame, ::Integer, ::Union{Colon, Regex, AbstractVector{T} where T, All, Between, Cols, InvertedIndex}) at /Users/jakeroth/.julia/packages/DataFrames/BM4OQ/src/dataframerow/dataframerow.jl:208
getindex(::DataFrame, ::Integer, ::Union{AbstractString, Symbol}) at /Users/jakeroth/.julia/packages/DataFrames/BM4OQ/src/dataframe/dataframe.jl:488
...
Stacktrace:
[1] top-level scope
@ REPL[280]:1
4: julia> push!(mydf, mydf[1,:])
3×1 DataFrame
Row │ mycol
│ Int64
─────┼───────
1 │ 257
2 │ 258
3 │ 257