Skip to content

Commit

Permalink
Fill existing arrays with scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb committed Sep 5, 2016
1 parent ae6c042 commit 60fd1c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,13 @@ function Base.setindex!(df::DataFrame,
end

# df[SingleColumnIndex] = Single Item (EXPANDS TO NROW(DF) if NCOL(DF) > 0)
function Base.setindex!(df::DataFrame,
v::Any,
col_ind::ColumnIndex)
insert_single_column!(df, upgrade_scalar(df, v), col_ind)
function Base.setindex!(df::DataFrame, v, col_ind::ColumnIndex)
if haskey(index(df), col_ind)
j = index(df)[col_ind]
fill!(df.columns[j], v)
else
insert_single_column!(df, upgrade_scalar(df, v), col_ind)
end
end

# df[MultiColumnIndex] = DataFrame
Expand Down Expand Up @@ -397,7 +400,7 @@ function Base.setindex!{T <: ColumnIndex}(df::DataFrame,
col_inds::AbstractVector{T})
dv = upgrade_vector(v)
for col_ind in col_inds
insert_single_column!(df, dv, col_ind)
df[col_ind] = dv
end
return df
end
Expand All @@ -406,14 +409,13 @@ end
function Base.setindex!(df::DataFrame,
val::Any,
col_inds::AbstractVector{Bool})
setindex!(df, val, find(col_inds))
df[find(col_inds)] = val
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
insert_single_column!(df, dv, col_ind)
df[col_ind] = val
end
return df
end
Expand Down
9 changes: 9 additions & 0 deletions test/mutation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
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

0 comments on commit 60fd1c2

Please sign in to comment.