@@ -110,16 +110,23 @@ Base.@propagate_inbounds function SubDataFrame(parent::DataFrame, rows::Abstract
110110 return SubDataFrame (parent, convert (Vector{Int}, rows), cols)
111111end
112112
113- Base. @propagate_inbounds SubDataFrame (sdf:: SubDataFrame , rowind, cols) =
114- SubDataFrame (parent (sdf), rows (sdf)[rowind], parentcols (index (sdf), cols))
113+ Base. @propagate_inbounds function SubDataFrame (sdf:: SubDataFrame , rowind, cols)
114+ new_rows = ncol (sdf) == 0 ? (Int[])[rowind] : rows (sdf)[rowind]
115+ return SubDataFrame (parent (sdf), new_rows, parentcols (index (sdf), cols))
116+ end
117+
115118Base. @propagate_inbounds SubDataFrame (sdf:: SubDataFrame , rowind:: Bool , cols) =
116119 throw (ArgumentError (" invalid row index of type Bool" ))
117- Base. @propagate_inbounds SubDataFrame (sdf:: SubDataFrame , rowind, :: Colon ) =
120+
121+ Base. @propagate_inbounds function SubDataFrame (sdf:: SubDataFrame , rowind, :: Colon )
122+ new_rows = ncol (sdf) == 0 ? (Int[])[rowind] : rows (sdf)[rowind]
118123 if index (sdf) isa Index # sdf was created using : as row selector
119- SubDataFrame (parent (sdf), rows (sdf)[rowind] , :)
124+ return SubDataFrame (parent (sdf), new_rows , :)
120125 else
121- SubDataFrame (parent (sdf), rows (sdf)[rowind] , parentcols (index (sdf), :))
126+ return SubDataFrame (parent (sdf), new_rows , parentcols (index (sdf), :))
122127 end
128+ end
129+
123130Base. @propagate_inbounds SubDataFrame (sdf:: SubDataFrame , rowind:: Bool , :: Colon ) =
124131 throw (ArgumentError (" invalid row index of type Bool" ))
125132Base. @propagate_inbounds SubDataFrame (sdf:: SubDataFrame , :: Colon , cols) =
@@ -157,27 +164,30 @@ index(sdf::SubDataFrame) = getfield(sdf, :colindex)
157164nrow (sdf:: SubDataFrame ) = ncol (sdf) > 0 ? length (rows (sdf)):: Int : 0
158165
159166Base. @propagate_inbounds Base. getindex (sdf:: SubDataFrame , rowind:: Integer , colind:: ColumnIndex ) =
160- parent (sdf)[rows (sdf)[rowind] , parentcols (index (sdf), colind)]
167+ parent (sdf)[! , parentcols (index (sdf), colind)][ rows (sdf)[rowind] ]
161168Base. @propagate_inbounds Base. getindex (sdf:: SubDataFrame , rowind:: Bool , colind:: ColumnIndex ) =
162169 throw (ArgumentError (" invalid row index of type Bool" ))
163-
164170Base. @propagate_inbounds Base. getindex (sdf:: SubDataFrame , rowinds:: Union{AbstractVector, Not} ,
165171 colind:: ColumnIndex ) =
166- parent (sdf)[rows (sdf)[rowinds] , parentcols (index (sdf), colind)]
172+ parent (sdf)[! , parentcols (index (sdf), colind)][ rows (sdf)[rowinds] ]
167173Base. @propagate_inbounds Base. getindex (sdf:: SubDataFrame , :: Colon , colind:: ColumnIndex ) =
168174 parent (sdf)[rows (sdf), parentcols (index (sdf), colind)]
169175Base. @propagate_inbounds Base. getindex (sdf:: SubDataFrame , :: typeof (! ), colind:: ColumnIndex ) =
170176 view (parent (sdf), rows (sdf), parentcols (index (sdf), colind))
171177
172- Base. @propagate_inbounds Base. getindex (sdf:: SubDataFrame , rowinds:: Union{AbstractVector, Not} ,
173- colinds:: MultiColumnIndex ) =
174- parent (sdf)[rows (sdf)[rowinds], parentcols (index (sdf), colinds)]
178+ Base. @propagate_inbounds function Base. getindex (sdf:: SubDataFrame ,
179+ rowinds:: Union{AbstractVector, Not} ,
180+ colinds:: MultiColumnIndex )
181+ new_rows = ncol (sdf) == 0 ? (Int[])[rowinds] : rows (sdf)[rowinds]
182+ return parent (sdf)[new_rows, parentcols (index (sdf), colinds)]
183+ end
184+
175185Base. @propagate_inbounds Base. getindex (sdf:: SubDataFrame , :: Colon ,
176186 colinds:: MultiColumnIndex ) =
177187 parent (sdf)[rows (sdf), parentcols (index (sdf), colinds)]
178- Base. @propagate_inbounds Base. getindex (df :: SubDataFrame , row_ind:: typeof (! ),
188+ Base. @propagate_inbounds Base. getindex (sdf :: SubDataFrame , row_ind:: typeof (! ),
179189 col_inds:: MultiColumnIndex ) =
180- select (df , index (df )[col_inds], copycols= false )
190+ select (sdf , index (sdf )[col_inds], copycols= false )
181191
182192Base. @propagate_inbounds function Base. setindex! (sdf:: SubDataFrame , val:: Any , idx:: CartesianIndex{2} )
183193 return setindex! (sdf, val, idx[1 ], idx[2 ])
0 commit comments