Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to pass a reference to where to add a column by its name #2365

Merged
merged 8 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ end
##############################################################################

"""
insertcols!(df::DataFrame, [ind::Int], (name=>col)::Pair...;
insertcols!(df::DataFrame, [ref], (name=>col)::Pair...;
makeunique::Bool=false, copycols::Bool=true)

Insert a column into a data frame in place. Return the updated `DataFrame`.
Expand All @@ -612,7 +612,8 @@ If `ind` is omitted it is set to `ncol(df)+1`

# Arguments
- `df` : the DataFrame to which we want to add columns
- `ind` : a position at which we want to insert a column
- `ref` : a position at which we want to insert a column, passed as an integer
bkamins marked this conversation as resolved.
Show resolved Hide resolved
or a column name (a string or a `Symbol`)
- `name` : the name of the new column
- `col` : an `AbstractVector` giving the contents of the new column or a value of any
type other than `AbstractArray` which will be repeated to fill a new vector;
Expand Down Expand Up @@ -655,8 +656,9 @@ julia> insertcols!(d, 2, :c => 2:4, :c => 3:5, makeunique=true)
│ 3 │ 'c' │ 4 │ 5 │ 3 │
```
"""
function insertcols!(df::DataFrame, col_ind::Int, name_cols::Pair{Symbol,<:Any}...;
function insertcols!(df::DataFrame, ref::ColumnIndex, name_cols::Pair{Symbol,<:Any}...;
makeunique::Bool=false, copycols::Bool=true)
col_ind = Int(ref isa SymbolOrString ? columnindex(df, ref) : ref)
if !(0 < col_ind <= ncol(df) + 1)
throw(ArgumentError("attempt to insert a column to a data frame with " *
"$(ncol(df)) columns at index $col_ind"))
Expand Down Expand Up @@ -744,9 +746,9 @@ function insertcols!(df::DataFrame, col_ind::Int, name_cols::Pair{Symbol,<:Any}.
return df
end

insertcols!(df::DataFrame, col_ind::Int, name_cols::Pair{<:AbstractString,<:Any}...;
insertcols!(df::DataFrame, ref::ColumnIndex, name_cols::Pair{<:AbstractString,<:Any}...;
makeunique::Bool=false, copycols::Bool=true) =
insertcols!(df, col_ind, (Symbol(n) => v for (n,v) in name_cols)...,
insertcols!(df, ref, (Symbol(n) => v for (n,v) in name_cols)...,
makeunique=makeunique, copycols=copycols)

insertcols!(df::DataFrame, name_cols::Pair{Symbol,<:Any}...;
Expand All @@ -758,10 +760,10 @@ insertcols!(df::DataFrame, name_cols::Pair{<:AbstractString,<:Any}...;
insertcols!(df, (Symbol(n) => v for (n,v) in name_cols)...,
makeunique=makeunique, copycols=copycols)

function insertcols!(df::DataFrame, col_ind::Int=ncol(df)+1; makeunique::Bool=false, name_cols...)
if !(0 < col_ind <= ncol(df) + 1)
function insertcols!(df::DataFrame, ref::Int=ncol(df)+1; makeunique::Bool=false, name_cols...)
if !(0 < ref <= ncol(df) + 1)
throw(ArgumentError("attempt to insert a column to a data frame with " *
"$(ncol(df)) columns at index $col_ind"))
"$(ncol(df)) columns at index $ref"))
end
if !isempty(name_cols)
# an explicit error is thrown as keyword argument was supported in the past
Expand Down
8 changes: 8 additions & 0 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,16 @@ end
df = DataFrame(a=Union{Int, Missing}[1, 2], b=Union{Float64, Missing}[3.0, 4.0])
@test_throws ArgumentError insertcols!(df, 5, :newcol => ["a", "b"])
@test_throws ArgumentError insertcols!(df, 0, :newcol => ["a", "b"])
@test_throws ArgumentError insertcols!(df, :z, :newcol => ["a", "b"])
@test_throws ArgumentError insertcols!(df, "z", :newcol => ["a", "b"])
@test_throws MethodError insertcols!(df, true, :newcol => ["a", "b"])
@test_throws DimensionMismatch insertcols!(df, 1, :newcol => ["a"])
@test_throws DimensionMismatch insertcols!(df, :a, :newcol => ["a"])
@test_throws DimensionMismatch insertcols!(df, "a", :newcol => ["a"])
ref1 = insertcols!(copy(df), :a, :newcol => ["a", "b"])
ref2 = insertcols!(copy(df), "a", :newcol => ["a", "b"])
@test insertcols!(df, 1, :newcol => ["a", "b"]) == df
@test ref1 == ref2 == df
@test names(df) == ["newcol", "a", "b"]
@test df.a == [1, 2]
@test df.b == [3.0, 4.0]
Expand Down