@@ -3097,7 +3097,7 @@ julia> insertcols!(df, :b, :d => 7:9, after=true)
30973097 3 │ c 9 4 5 3
30983098```
30993099"""
3100- function insertcols! (df:: AbstractDataFrame , col:: ColumnIndex , name_cols:: Pair{Symbol, <:Any } ...;
3100+ function insertcols! (df:: AbstractDataFrame , col:: ColumnIndex , name_cols:: Pair{Symbol} ...;
31013101 after:: Bool = false , makeunique:: Bool = false , copycols:: Bool = true )
31023102 if ! is_column_insertion_allowed (df)
31033103 throw (ArgumentError (" insertcols! is only supported for DataFrame, or for " *
@@ -3222,31 +3222,47 @@ function insertcols!(df::AbstractDataFrame, col::ColumnIndex, name_cols::Pair{Sy
32223222 return df
32233223end
32243224
3225- insertcols! (df:: AbstractDataFrame , col:: ColumnIndex , name_cols:: Pair{<:AbstractString, <:Any } ...;
3225+ insertcols! (df:: AbstractDataFrame , col:: ColumnIndex , name_cols:: Pair{<:AbstractString} ...;
32263226 after:: Bool = false , makeunique:: Bool = false , copycols:: Bool = true ) =
32273227 insertcols! (df, col, (Symbol (n) => v for (n, v) in name_cols). .. ,
32283228 after= after, makeunique= makeunique, copycols= copycols)
32293229
3230- insertcols! (df:: AbstractDataFrame , name_cols:: Pair{Symbol, <:Any } ...;
3230+ insertcols! (df:: AbstractDataFrame , name_cols:: Pair{Symbol} ...;
32313231 after:: Bool = false , makeunique:: Bool = false , copycols:: Bool = true ) =
32323232 insertcols! (df, ncol (df)+ 1 , name_cols... , after= after,
32333233 makeunique= makeunique, copycols= copycols)
32343234
3235- insertcols! (df:: AbstractDataFrame , name_cols:: Pair{<:AbstractString, <:Any } ...;
3235+ insertcols! (df:: AbstractDataFrame , name_cols:: Pair{<:AbstractString} ...;
32363236 after:: Bool = false , makeunique:: Bool = false , copycols:: Bool = true ) =
32373237 insertcols! (df, (Symbol (n) => v for (n, v) in name_cols). .. ,
32383238 after= after, makeunique= makeunique, copycols= copycols)
32393239
3240- function insertcols! (df:: AbstractDataFrame , col:: Int = ncol (df)+ 1 ; makeunique:: Bool = false , name_cols... )
3241- if ! (0 < col <= ncol (df) + 1 )
3242- throw (ArgumentError (" attempt to insert a column to a data frame with " *
3243- " $(ncol (df)) columns at index $col " ))
3240+ function insertcols! (df:: AbstractDataFrame , col:: ColumnIndex ; after:: Bool = false ,
3241+ makeunique:: Bool = false , copycols:: Bool = true )
3242+ if col isa SymbolOrString
3243+ col_ind = Int (columnindex (df, col))
3244+ if col_ind == 0
3245+ throw (ArgumentError (" column $col does not exist in data frame" ))
3246+ end
3247+ else
3248+ col_ind = Int (col)
32443249 end
3245- if ! isempty (name_cols)
3246- # an explicit error is thrown as keyword argument was supported in the past
3247- throw (ArgumentError (" inserting columns using a keyword argument is not supported, " *
3248- " pass a Pair as a positional argument instead" ))
3250+
3251+ if after
3252+ col_ind += 1
32493253 end
3254+
3255+ if ! (0 < col_ind <= ncol (df) + 1 )
3256+ throw (ArgumentError (" attempt to insert a column to a data frame with " *
3257+ " $(ncol (df)) columns at index $col_ind " ))
3258+ end
3259+
3260+ _drop_all_nonnote_metadata! (parent (df))
3261+ return df
3262+ end
3263+
3264+ function insertcols! (df:: AbstractDataFrame ; after:: Bool = false ,
3265+ makeunique:: Bool = false , copycols:: Bool = true )
32503266 _drop_all_nonnote_metadata! (parent (df))
32513267 return df
32523268end
0 commit comments