Is there a method to copy all of the metadata from one DataFrame to another?
for anyone that comes next:
md = DataFrames.metadata(df1)
for k in keys(md)
DataFrames.metadata!(df2, k, md[k])
end
1 Like
Just in case you werenβt aware, there are two βstylesβ of metadata in Julia.
There is the :note
style and the :default
style.
In the :default
style, metadata is dropped after every transformations. So something like
julia> df = DataFrame(a = 1);
julia> colmetadata!(df, :a, "label", "Label for a")
1Γ1 DataFrame
Row β a
β Int64
ββββββΌβββββββ
1 β 1
julia> colmetadata(df)
Dict{Symbol, Dict{String, String}} with 1 entry:
:a => Dict("label"=>"Label for a")
julia> df2 = transform(df, :a => ByRow(t -> t + 1) => :b);
julia> colmetadata(df2)
Dict{Any, Any}()
You might be struggling with this. The solution is to set the colmetadatastyle
to :note
julia> df = DataFrame(a = 1);
julia> colmetadata!(df, :a, "label", "Label for a")
1Γ1 DataFrame
Row β a
β Int64
ββββββΌβββββββ
1 β 1
julia> setcolmetadatastyle!(df; style =:note);
julia> df2 = transform(df, :a => ByRow(t -> t + 1) => :b);
julia> colmetadata(df)
Dict{Symbol, Dict{String, String}} with 1 entry:
:a => Dict("label"=>"Label for a")
Two solutions
- TableMetaDataTools.jl adds some convenience functions
- DataFramesMeta.jl adds even more convenience functions. All metadata created with DataFramesMeta.jl is with the
:note
style. So they are forwarded.