Difference between rename! and rename with new DataFrame in Julia? -
is there difference between rename , rename! when constructing new dataframe in julia? believe in general when modifying existing dataframe rename! ideal since modifies arguments without generating new object in memory.
but since no object exists in memory yet rename seems appropriate.
using dataframes df1 = rename( dataframe(rand((100,2))), [:x1, :x2], [:x, :y]) df2 = rename!(dataframe(rand((100,2))), [:x1, :x2], [:x, :y]) # ideal formulation not using rename (currently no matching method) df0 = dataframe(rand((100,2)), [:x, :y])
the rename! form modifies argument data frame whereas rename version constructs new data frame new column names. since you're constructing data frame , renaming columns, it's safe , more efficient use rename!. if reference inner data frame exists don't want modify in visible way, want use non-mutating rename function. difference can seen here:
julia> df0 = dataframe(rand((3,2))) 3×2 dataframes.dataframe │ row │ x1 │ x2 │ ├─────┼──────────┼──────────┤ │ 1 │ 0.625971 │ 0.401812 │ │ 2 │ 0.316224 │ 0.208431 │ │ 3 │ 0.331206 │ 0.466665 │ julia> df1 = rename(df0, [:x1, :x2], [:x, :y]) 3×2 dataframes.dataframe │ row │ x │ y │ ├─────┼──────────┼──────────┤ │ 1 │ 0.625971 │ 0.401812 │ │ 2 │ 0.316224 │ 0.208431 │ │ 3 │ 0.331206 │ 0.466665 │ julia> df0 3×2 dataframes.dataframe │ row │ x1 │ x2 │ ├─────┼──────────┼──────────┤ │ 1 │ 0.625971 │ 0.401812 │ │ 2 │ 0.316224 │ 0.208431 │ │ 3 │ 0.331206 │ 0.466665 │ julia> df0 === df1 false julia> df2 = rename!(df0, [:x1, :x2], [:x, :y]) 3×2 dataframes.dataframe │ row │ x │ y │ ├─────┼──────────┼──────────┤ │ 1 │ 0.625971 │ 0.401812 │ │ 2 │ 0.316224 │ 0.208431 │ │ 3 │ 0.331206 │ 0.466665 │ julia> df0 3×2 dataframes.dataframe │ row │ x │ y │ ├─────┼──────────┼──────────┤ │ 1 │ 0.625971 │ 0.401812 │ │ 2 │ 0.316224 │ 0.208431 │ │ 3 │ 0.331206 │ 0.466665 │ julia> df0 === df2 true the data frame returned rename new data frame different column labels – i.e. df1 !== df0 – whereas data frame returned rename! same data frame passed in modified column name – i.e. df1 === df0. might want start discussion on julia's discourse forum convenience methods constructing data frames.
Comments
Post a Comment