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 haskey and get support for DataFrameColumns #3282

Merged
merged 1 commit into from
Feb 5, 2023
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 @@ -20,6 +20,9 @@
* Add `keep` keyword argument to `nonunique`, `unique`, and `unique!`
allowing to specify which duplicate rows should be kept
([#3260](https://github.com/JuliaData/DataFrames.jl/pull/3260))
* Add `haskey` and `get` methods to `DataFrameColumns`
to make it support dictionary interface more completely
([#3282](https://github.com/JuliaData/DataFrames.jl/pull/3282))

## Bug fixes

Expand Down
8 changes: 8 additions & 0 deletions src/abstractdataframe/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ with the corresponding column vector, i.e. `name => col`
where `name` is the column name of the column `col`.
"""
Base.pairs(itr::DataFrameColumns) = Base.Iterators.Pairs(itr, keys(itr))

Base.haskey(itr::DataFrameColumns, col::Union{AbstractString, Symbol}) =
columnindex(parent(itr), col) > 0
Base.haskey(itr::DataFrameColumns, col::Union{Signed, Unsigned}) =
0 < col <= ncol(parent(itr))
Base.get(itr::DataFrameColumns, col::ColumnIndex, default) =
haskey(itr, col) ? itr[col] : default

Base.findnext(f::Function, itr::DataFrameColumns, i::Integer) =
findnext(f, values(itr), i)
Base.findnext(f::Function, itr::DataFrameColumns, i::Union{Symbol, AbstractString}) =
Expand Down
33 changes: 33 additions & 0 deletions test/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,39 @@ end
@test parent(er3) isa SubDataFrame
@test parentindices(parent(er3))[1] == 1:2
end

end

@testset "haskey and get for DataFrameColumns" begin
df_ref = DataFrame(a=1:3, b=2:4, c=3:5)
for df in (df_ref, @view df_ref[1:3, 1:2])
dfc = eachcol(df)
@test !haskey(dfc, 0)
@test haskey(dfc, 1)
@test haskey(dfc, 2)
@test !haskey(dfc, 4)
@test !haskey(dfc, 0x0)
@test haskey(dfc, 0x1)
@test !haskey(dfc, :x)
@test !haskey(dfc, "x")
@test !haskey(dfc, Test.GenericString("x"))
@test haskey(dfc, :a)
@test haskey(dfc, "a")
@test haskey(dfc, Test.GenericString("a"))

@test get(dfc, 0, "error") == "error"
@test get(dfc, 1, "error") == 1:3
@test get(dfc, 4, "error") == "error"
@test get(dfc, 0x0, "error") == "error"
@test get(dfc, 0x1, "error") == 1:3
@test get(dfc, :x, "error") == "error"
@test get(dfc, "x", "error") == "error"
@test get(dfc, Test.GenericString("x"), "error") == "error"
@test get(dfc, :a, "error") == 1:3
@test get(dfc, "a", "error") == 1:3
@test get(dfc, Test.GenericString("a"), "error") == 1:3
@test_throws MethodError get(dfc, "a")
end
end

end # module