Skip to content

Commit

Permalink
Make indexing of eachrow return the object of the same type on a view…
Browse files Browse the repository at this point in the history
… of the parent (#3037)
  • Loading branch information
bkamins authored Apr 9, 2022
1 parent c0ed2f9 commit 0c79493
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@

## Performance

* Speed up `permute!` and `invpermute!` (and therefore sorting) 2x-8x
* Speed up `permute!` and `invpermute!` (and therefore sorting) 2x-8x
for large tables by using cycle notation
([#3035](https://github.com/JuliaData/DataFrames.jl/pull/3035))
* Make one-dimensional multi-element indexing of `DataFrameRows` return
`DataFrameRows`
([#3037](https://github.com/JuliaData/DataFrames.jl/pull/3037))

# DataFrames.jl v1.3.2 Patch Release Notes

Expand Down
2 changes: 2 additions & 0 deletions src/abstractdataframe/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Base.IndexStyle(::Type{<:DataFrameRows}) = Base.IndexLinear()
Base.size(itr::DataFrameRows) = (size(parent(itr), 1), )

Base.@propagate_inbounds Base.getindex(itr::DataFrameRows, i::Int) = parent(itr)[i, :]
Base.@propagate_inbounds Base.getindex(itr::DataFrameRows, idx) =
eachrow(@view parent(itr)[idx, :])

# separate methods are needed due to dispatch ambiguity
Base.getproperty(itr::DataFrameRows, col_ind::Symbol) =
Expand Down
23 changes: 23 additions & 0 deletions test/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,27 @@ end
@test findall(col -> eltype(col) <: Int, cols) == [1, 3]
end

@testset "multirow indexing of DataFrameRows" begin
df = DataFrame(a=1:4, b=11:14)
er = eachrow(df)
for sel in (1:2, Not(3:4), [true, true, false, false])
er2 = er[sel]
@test er2 isa DataFrames.DataFrameRows
@test parent(er2) isa SubDataFrame
@test parent(parent(er2)) === df
@test collect(er2) == [er[1], er[2]]
end
er2 = er[:]
@test er2 == er
@test er2 isa DataFrames.DataFrameRows
er2 = er[2:1]
@test length(er2) == 0
@test isempty(parent(er2))

# this is still allowed
er2 = er[1:2, 1]
@test er2 isa Vector{DataFrameRow}
@test er2 == er[1:2]
end

end # module

0 comments on commit 0c79493

Please sign in to comment.