@@ -95,6 +95,66 @@ Compat.hasproperty(itr::DataFrameRows, s::AbstractString) = haskey(index(parent(
9595# Private fields are never exposed since they can conflict with column names
9696Base. propertynames (itr:: DataFrameRows , private:: Bool = false ) = propertynames (parent (itr))
9797
98+ """
99+ Iterators.partition(dfr::DataFrameRows, n::Integer)
100+
101+ Iterate over `DataFrameRows` `dfr` `n` rows at a time, returning each block
102+ as a `DataFrameRows` over a view of rows of parent of `dfr`.
103+
104+ # Examples
105+
106+ ```jldoctest
107+ julia> collect(Iterators.partition(eachrow(DataFrame(x=1:5)), 2))
108+ 3-element Vector{DataFrames.DataFrameRows{SubDataFrame{DataFrame, DataFrames.Index, UnitRange{Int64}}}}:
109+ 2×1 DataFrameRows
110+ Row │ x
111+ │ Int64
112+ ─────┼───────
113+ 1 │ 1
114+ 2 │ 2
115+ 2×1 DataFrameRows
116+ Row │ x
117+ │ Int64
118+ ─────┼───────
119+ 1 │ 3
120+ 2 │ 4
121+ 1×1 DataFrameRows
122+ Row │ x
123+ │ Int64
124+ ─────┼───────
125+ 1 │ 5
126+ ```
127+ """
128+ function Iterators. partition (dfr:: DataFrameRows , n:: Integer )
129+ n < 1 && throw (ArgumentError (" cannot create partitions of length $n " ))
130+ return Iterators. PartitionIterator (dfr, Int (n))
131+ end
132+
133+ # use autodetection of eltype
134+ Base. IteratorEltype (:: Type{<:Iterators.PartitionIterator{<:DataFrameRows}} ) =
135+ Base. EltypeUnknown ()
136+
137+ # we do not need to be overly specific here as we rely on autodetection of eltype
138+ # this method is needed only to override the fallback for `PartitionIterator`
139+ Base. eltype (:: Type{<:Iterators.PartitionIterator{<:DataFrameRows}} ) =
140+ DataFrameRows
141+
142+ Base. IteratorSize (:: Type{<:Iterators.PartitionIterator{<:DataFrameRows}} ) =
143+ Base. HasLength ()
144+
145+ function Base. length (itr:: Iterators.PartitionIterator{<:DataFrameRows} )
146+ l = nrow (parent (itr. c))
147+ return cld (l, itr. n)
148+ end
149+
150+ function Base. iterate (itr:: Iterators.PartitionIterator{<:DataFrameRows} , state:: Int = 1 )
151+ df = parent (itr. c)
152+ last_idx = nrow (df)
153+ state > last_idx && return nothing
154+ r = min (state + itr. n - 1 , last_idx)
155+ return eachrow (view (df, state: r, :)), r + 1
156+ end
157+
98158# Iteration by columns
99159
100160const DATAFRAMECOLUMNS_DOCSTR = """
0 commit comments