Skip to content

Commit

Permalink
make sure getindex on DataFrameRows does not alias passed selector (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Oct 6, 2022
1 parent 897a8cb commit 8f726a6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
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 && !(eltype(idx) <: Bool) ? copy(idx) : idx, :])

# 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

2 comments on commit 8f726a6

@bkamins
Copy link
Member Author

@bkamins bkamins commented on 8f726a6 Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/69645

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.1 -m "<description of version>" 8f726a6af65c64427822f75f5adaaa3df90e277a
git push origin v1.4.1

Please sign in to comment.