DataFrames.jl icon indicating copy to clipboard operation
DataFrames.jl copied to clipboard

Document custom generation of column names in manual

Open schlichtanders opened this issue 3 months ago • 9 comments

I am looking for a fix or workaround for how to use AsTable in combination with several columns which should be transformed, i.e. .=>.

I always get ERROR: ArgumentError: Duplicate column name(s) returned:

df = DataFrame(a = 1:10, b = 4:13)
function myextrema(a)
    ex = extrema(a)
    (min=ex[1], max=ex[2])
end

combine(df, :a => myextrema => AsTable)  # works 
combine(df, [:a, :b] .=> myextrema .=> AsTable)  # fails

throws the following error

ERROR: ArgumentError: Duplicate column name(s) returned: :min, :max
Stacktrace:
[1] select_transform!(::Base.RefValue{…}, df::DataFrame, newdf::DataFrame, transformed_cols::Set{…}, copycols::Bool, allow_resizing_newdf::Base.RefValue{…}, column_to_copy::BitVector)
@ DataFrames ~/.julia/packages/DataFrames/58MUJ/src/abstractdataframe/selection.jl:838
[2] _manipulate(df::DataFrame, normalized_cs::Vector{Any}, copycols::Bool, keeprows::Bool)
@ DataFrames ~/.julia/packages/DataFrames/58MUJ/src/abstractdataframe/selection.jl:1778
[3] manipulate(::DataFrame, ::Any, ::Vararg{Any}; copycols::Bool, keeprows::Bool, renamecols::Bool)
@ DataFrames ~/.julia/packages/DataFrames/58MUJ/src/abstractdataframe/selection.jl:1698
[4] #manipulate#599
@ ~/.julia/packages/DataFrames/58MUJ/src/abstractdataframe/selection.jl:1833 [inlined]
[5] combine(df::DataFrame, args::Any; renamecols::Bool, threads::Bool)
@ DataFrames ~/.julia/packages/DataFrames/58MUJ/src/abstractdataframe/selection.jl:1669
[6] top-level scope
@ REPL[125]:1

My ideal behaviour would be that AsTable prepends the column name, but of course this would be breaking. Maybe there could be a PrependColName(AsTable) wrapper or something similar?

schlichtanders avatar Mar 08 '24 10:03 schlichtanders