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

Fix eachrow and eachcol indexing with CartesianIndex #3413

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
* Correctly return `Bool[]` in the `nonunique` function applied to a data frame
with a pulled column that has zero levels in the pool
([#3393](https://github.com/JuliaData/DataFrames.jl/pull/3393))
* Correctly index `eachrow` and `eachcol` with `CartesianIndex`
([#3413](https://github.com/JuliaData/DataFrames.jl/issues/3413))


# DataFrames.jl v1.6.1 Release Notes

Expand Down
3 changes: 3 additions & 0 deletions src/abstractdataframe/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ 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, i::CartesianIndex{1}) = itr[i[1]]
Base.@propagate_inbounds Base.getindex(itr::DataFrameRows, idx) =
eachrow(@view parent(itr)[idx isa AbstractVector && !(eltype(idx) <: Bool) ? copy(idx) : idx, :])

Expand Down Expand Up @@ -263,6 +264,8 @@ Base.iterate(itr::DataFrameColumns, i::Integer=1) =
i <= length(itr) ? (itr[i], i + 1) : nothing
Base.@propagate_inbounds Base.getindex(itr::DataFrameColumns, idx::ColumnIndex) =
parent(itr)[!, idx]
Base.@propagate_inbounds Base.getindex(itr::DataFrameColumns, idx::CartesianIndex{1}) =
itr[idx[1]]
Base.@propagate_inbounds Base.getindex(itr::DataFrameColumns, idx::MultiColumnIndex) =
eachcol(parent(itr)[!, idx])
Base.:(==)(itr1::DataFrameColumns, itr2::DataFrameColumns) =
Expand Down
4 changes: 4 additions & 0 deletions test/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ using Test, DataFrames
@test sprint(summary, eachrow(df)) == "2-element DataFrameRows"
@test Base.IndexStyle(eachrow(df)) == IndexLinear()
@test eachrow(df)[1] == DataFrameRow(df, 1, :)
@test eachrow(df)[CartesianIndex(1)] == DataFrameRow(df, 1, :)
@test_throws MethodError eachrow(df)[CartesianIndex(1, 1)]
@test collect(eachrow(df)) isa Vector{<:DataFrameRow}
@test eltype(eachrow(df)) <: DataFrameRow
for row in eachrow(df)
Expand All @@ -35,6 +37,8 @@ using Test, DataFrames
@test_throws ArgumentError size(eachcol(df), 2)
@test_throws ArgumentError size(eachcol(df), 0)
@test eachcol(df)[1] == df[:, 1]
@test eachcol(df)[CartesianIndex(1)] == df[:, 1]
@test_throws MethodError eachcol(df)[CartesianIndex(1, 1)]
@test eachcol(df)[:A] === df[!, :A]
@test eachcol(df)[All()] == eachcol(df)
@test eachcol(df)[Cols(:)] == eachcol(df)
Expand Down
Loading