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

Avoid aliasing columns on assignment #1052

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ function insert_multiple_entries!{T <: Real}(df::DataFrame,
end
end

upgrade_vector(v::Vector) = DataArray(v, falses(length(v)))
upgrade_vector(v::Range) = DataArray([v;], falses(length(v)))
upgrade_vector(v::BitVector) = DataArray(convert(Array{Bool}, v), falses(length(v)))
upgrade_vector(adv::AbstractDataArray) = adv
upgrade_vector(v::Vector) = DataArray(copy(v), falses(length(v)))
upgrade_vector(v::Union{Range,BitVector}) =
DataArray(collect(v), falses(length(v)))
upgrade_vector(adv::AbstractDataArray) = copy(adv)
function upgrade_scalar(df::DataFrame, v::AbstractArray)
msg = "setindex!(::DataFrame, ...) only broadcasts scalars, not arrays"
throw(ArgumentError(msg))
Expand Down Expand Up @@ -395,8 +395,8 @@ end
function Base.setindex!{T <: ColumnIndex}(df::DataFrame,
v::AbstractVector,
col_inds::AbstractVector{T})
dv = upgrade_vector(v)
for col_ind in col_inds
dv = upgrade_vector(v)
insert_single_column!(df, dv, col_ind)
end
return df
Expand All @@ -411,8 +411,8 @@ end
function Base.setindex!{T <: ColumnIndex}(df::DataFrame,
val::Any,
col_inds::AbstractVector{T})
dv = upgrade_scalar(df, val)
for col_ind in col_inds
dv = upgrade_scalar(df, val)
insert_single_column!(df, dv, col_ind)
end
return df
Expand Down
21 changes: 21 additions & 0 deletions test/mutation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module TestMutation
using Base.Test, DataFrames

# columns should not alias if scalar broadcasted
df = DataFrame(A=[0],B=[0])
df[1:end] = 0.0
df[1,:A] = 1.0
@test df[1,:B] === 0.0

# columns should not alias if vector assigned
df = DataFrame(A=[0],B=[0])
df[1:end] = [0.0]
df[1,:A] = 1.0
@test df[1,:B] === 0.0

# columns should not alias if DataVector assigned
df = DataFrame(A=[0],B=[0])
df[:A] = df[:B]
df[1,:A] = 1
@test df[1,:B] === 0
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ my_tests = ["utils.jl",
"duplicates.jl",
"show.jl",
"statsmodel.jl",
"contrasts.jl"]
"contrasts.jl",
"mutation.jl"]

println("Running tests:")

Expand Down