@@ -632,11 +632,11 @@ Base.getindex(df::DataFrame, row_ind::typeof(!), col_inds::MultiColumnIndex) =
632632# #############################################################################
633633
634634# Will automatically add a new column if needed
635- function insert_single_column! (df:: DataFrame , v:: AbstractVector , col_ind:: ColumnIndex )
636- if ncol (df) != 0 && nrow (df) != length (v)
635+ function insert_single_column! (df:: DataFrame , v:: Any , col_ind:: ColumnIndex ; copycols = false )
636+ dv = _preprocess_column (v, nrow (df), copycols)
637+ if ncol (df) != 0 && nrow (df) != length (dv)
637638 throw (ArgumentError (" New columns must have the same length as old columns" ))
638639 end
639- dv = isa (v, AbstractRange) ? collect (v) : v
640640 firstindex (dv) != 1 && _onebased_check_error ()
641641
642642 if haskey (index (df), col_ind)
@@ -664,24 +664,22 @@ function insert_single_entry!(df::DataFrame, v::Any, row_ind::Integer, col_ind::
664664 end
665665end
666666
667- # df[!, SingleColumnIndex] = AbstractVector
668- function Base. setindex! (df:: DataFrame , v:: AbstractVector , :: typeof (! ), col_ind:: ColumnIndex )
667+ # df[!, SingleColumnIndex] = value
668+ function Base. setindex! (df:: DataFrame , v:: Any , :: typeof (! ), col_ind:: ColumnIndex )
669669 insert_single_column! (df, v, col_ind)
670670 return df
671671end
672672
673- # df.col = AbstractVector
673+ # df.col = value
674674# separate methods are needed due to dispatch ambiguity
675675Base. setproperty! (df:: DataFrame , col_ind:: Symbol , v:: AbstractVector ) =
676676 (df[! , col_ind] = v)
677677Base. setproperty! (df:: DataFrame , col_ind:: AbstractString , v:: AbstractVector ) =
678678 (df[! , col_ind] = v)
679- Base. setproperty! (:: DataFrame , col_ind:: Symbol , v:: Any ) =
680- throw (ArgumentError (" It is only allowed to pass a vector as a column of a DataFrame. " *
681- " Instead use `df[!, col_ind] .= v` if you want to use broadcasting." ))
682- Base. setproperty! (:: DataFrame , col_ind:: AbstractString , v:: Any ) =
683- throw (ArgumentError (" It is only allowed to pass a vector as a column of a DataFrame. " *
684- " Instead use `df[!, col_ind] .= v` if you want to use broadcasting." ))
679+ Base. setproperty! (df:: DataFrame , col_ind:: Symbol , v:: Any ) =
680+ (df[! , col_ind] = v)
681+ Base. setproperty! (df:: DataFrame , col_ind:: AbstractString , v:: Any ) =
682+ (df[! , col_ind] = v)
685683
686684# df[SingleRowIndex, SingleColumnIndex] = Single Item
687685function Base. setindex! (df:: DataFrame , v:: Any , row_ind:: Integer , col_ind:: ColumnIndex )
@@ -786,6 +784,28 @@ for T1 in (:AbstractVector, :Not, :Colon, :(typeof(!))),
786784 end
787785end
788786
787+ for T1 in (:(typeof (! )),),
788+ T2 in MULTICOLUMNINDEX_TUPLE
789+ @eval function Base. setindex! (df:: DataFrame ,
790+ v:: AbstractVector ,
791+ row_inds:: $T1 ,
792+ col_inds:: $T2 )
793+ throw (ArgumentError (" a vector can not be assigned to multiple rows and columns, consider reshaping to a matrix first" ))
794+ end
795+
796+ @eval function Base. setindex! (df:: DataFrame ,
797+ v:: Any ,
798+ row_inds:: $T1 ,
799+ col_inds:: $T2 )
800+ idxs = index (df)[col_inds]
801+ for col in idxs
802+ # this will drop metadata appropriately
803+ df[row_inds, col] = v
804+ end
805+ return df
806+ end
807+ end
808+
789809"""
790810 copy(df::DataFrame; copycols::Bool=true)
791811
0 commit comments