Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Iterators.partition for DataFrameRows #3299

Merged
merged 3 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# DataFrames.jl v1.6 Release Notes

## New functionalities

* Add `Iterators.partition` support for `DataFrameRows`
([#3299](https://github.com/JuliaData/DataFrames.jl/pull/3299))

# DataFrames.jl v1.5 Release Notes

## New functionalities
Expand Down
60 changes: 60 additions & 0 deletions src/abstractdataframe/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,66 @@ Compat.hasproperty(itr::DataFrameRows, s::AbstractString) = haskey(index(parent(
# Private fields are never exposed since they can conflict with column names
Base.propertynames(itr::DataFrameRows, private::Bool=false) = propertynames(parent(itr))

"""
Iterators.partition(dfr::DataFrameRows, n::Integer)

Iterate over `dfr` `DataFrameRows` `n` rows at a time, returning each block
bkamins marked this conversation as resolved.
Show resolved Hide resolved
as a `DataFraneRows` over a view of rows of parent of `dfr`.
bkamins marked this conversation as resolved.
Show resolved Hide resolved

# Examples

```jldoctest
julia> collect(Iterators.partition(eachrow(DataFrame(x=1:5)), 2))
3-element Vector{DataFrames.DataFrameRows{SubDataFrame{DataFrame, DataFrames.Index, UnitRange{Int64}}}}:
2×1 DataFrameRows
Row │ x
│ Int64
─────┼───────
1 │ 1
2 │ 2
2×1 DataFrameRows
Row │ x
│ Int64
─────┼───────
1 │ 3
2 │ 4
1×1 DataFrameRows
Row │ x
│ Int64
─────┼───────
1 │ 5
```
"""
function Iterators.partition(dfr::DataFrameRows, n::Integer)
n < 1 && throw(ArgumentError("cannot create partitions of length $n"))
return Iterators.PartitionIterator(dfr, Int(n))
end

# use autodetection of eltype
Base.IteratorEltype(::Type{<:Iterators.PartitionIterator{<:DataFrameRows}}) =
Base.EltypeUnknown()

# we do not need to be overly specific here as we rely on autodetection of eltype
# this method is needed only to override the fallback for `PartitionIterator`
Base.eltype(::Type{<:Iterators.PartitionIterator{<:DataFrameRows}}) =
DataFrameRows

IteratorSize(::Type{<:Iterators.PartitionIterator{<:DataFrameRows}}) =
bkamins marked this conversation as resolved.
Show resolved Hide resolved
Base.HasLength()

function Base.length(itr::Iterators.PartitionIterator{<:DataFrameRows})
l = nrow(parent(itr.c))
return cld(l, itr.n)
end

function Base.iterate(itr::Iterators.PartitionIterator{<:DataFrameRows}, state::Int=1)
df = parent(itr.c)
last_idx = nrow(df)
state > last_idx && return nothing
r = min(state + itr.n - 1, last_idx)
return eachrow(view(df, state:r, :)), r + 1
end

# Iteration by columns

const DATAFRAMECOLUMNS_DOCSTR = """
Expand Down
21 changes: 21 additions & 0 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2311,13 +2311,34 @@ end
@test all(v -> v isa SubDataFrame, res)
@test_throws ArgumentError Iterators.partition(df, false)
@test_throws ArgumentError Iterators.partition(df, -1)

dfr = eachrow(df)
p = Iterators.partition(dfr, 2)
@test p isa Iterators.PartitionIterator
@test Tables.partitions(p) === p
@test eltype(p) === DataFrames.DataFrameRows
@test Base.IteratorEltype(typeof(p)) === Base.EltypeUnknown()
@test length(p) == 3
@test Base.IteratorSize(typeof(p)) === Base.HasLength()
res = collect(p)
@test res == eachrow.([DataFrame(x=1:2), DataFrame(x=3:4), DataFrame(x=5)])
@test all(v -> v isa DataFrames.DataFrameRows, res)
@test_throws ArgumentError Iterators.partition(df, false)
@test_throws ArgumentError Iterators.partition(df, -1)
end
p = Iterators.partition(DataFrame(), 1)
@test p isa Iterators.PartitionIterator
@test Tables.partitions(p) === p
@test isempty(p)
@test length(p) == 0
@test eltype(collect(p)) <: SubDataFrame

p = Iterators.partition(eachrow(DataFrame()), 1)
@test p isa Iterators.PartitionIterator
@test Tables.partitions(p) === p
@test isempty(p)
@test length(p) == 0
@test eltype(collect(p)) <: DataFrames.DataFrameRows
end

end # module