@@ -632,11 +632,11 @@ Base.getindex(df::DataFrame, row_ind::typeof(!), col_inds::MultiColumnIndex) =
632
632
# #############################################################################
633
633
634
634
# 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)
637
638
throw (ArgumentError (" New columns must have the same length as old columns" ))
638
639
end
639
- dv = isa (v, AbstractRange) ? collect (v) : v
640
640
firstindex (dv) != 1 && _onebased_check_error ()
641
641
642
642
if haskey (index (df), col_ind)
@@ -664,24 +664,22 @@ function insert_single_entry!(df::DataFrame, v::Any, row_ind::Integer, col_ind::
664
664
end
665
665
end
666
666
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 )
669
669
insert_single_column! (df, v, col_ind)
670
670
return df
671
671
end
672
672
673
- # df.col = AbstractVector
673
+ # df.col = value
674
674
# separate methods are needed due to dispatch ambiguity
675
675
Base. setproperty! (df:: DataFrame , col_ind:: Symbol , v:: AbstractVector ) =
676
676
(df[! , col_ind] = v)
677
677
Base. setproperty! (df:: DataFrame , col_ind:: AbstractString , v:: AbstractVector ) =
678
678
(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)
685
683
686
684
# df[SingleRowIndex, SingleColumnIndex] = Single Item
687
685
function Base. setindex! (df:: DataFrame , v:: Any , row_ind:: Integer , col_ind:: ColumnIndex )
@@ -786,6 +784,28 @@ for T1 in (:AbstractVector, :Not, :Colon, :(typeof(!))),
786
784
end
787
785
end
788
786
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
+
789
809
"""
790
810
copy(df::DataFrame; copycols::Bool=true)
791
811
0 commit comments