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

make sure getindex on DataFrameRows does not alias passed selector #3192

Merged
merged 4 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# DataFrames.jl v1.4.1 Patch Release Notes

## Bug fixes

* Make sure we always copy the indexing value when calling `getindex` on
`DataFrameRows` object
([#3192](https://github.com/JuliaData/DataFrames.jl/issues/3192))

# DataFrames.jl v1.4 Release Notes

## Julia compatibility change
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DataFrames"
uuid = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
version = "1.4.0"
version = "1.4.1"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
2 changes: 1 addition & 1 deletion src/abstractdataframe/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ 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, :])
eachrow(@view parent(itr)[idx isa AbstractVector{Int} ? copy(idx) : idx, :])
bkamins marked this conversation as resolved.
Show resolved Hide resolved

# separate methods are needed due to dispatch ambiguity
Base.getproperty(itr::DataFrameRows, col_ind::Symbol) =
Expand Down
26 changes: 26 additions & 0 deletions test/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,30 @@ end
@test er2 == er[1:2]
end

@testset "test unaliasing of index" begin
for idx in ([2, 3], [0x2, 0x3], 2:3, Not([1, 4]),
[false, true, true, false], big.([2, 3]), big.(2:3), :)
df = DataFrame(a=1:4)
er = eachrow(df)
er2 = er[idx]
len = length(er2)
@test len == (idx === Colon() ? 4 : 2)
p = parent(er2)
@test p isa SubDataFrame
@test parentindices(p)[1] == (1:4)[idx]
if !(idx isa UnitRange)
@test parentindices(p)[1] !== idx
end
if idx isa Vector
empty!(idx)
end
@test length(er2) == len

er3 = filter(x -> x.a <= 2, er)
@test length(er3) == 2
@test parent(er3) isa SubDataFrame
@test parentindices(parent(er3))[1] == 1:2
end
end

end # module