Skip to content

Commit

Permalink
Deprecate haskey and get for AbstractDataFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Jun 3, 2019
1 parent 2100d40 commit 41acf5d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 22 deletions.
14 changes: 3 additions & 11 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ function Base.size(df::AbstractDataFrame, i::Integer)
end
end

Base.isempty(df::AbstractDataFrame) = size(df, 1) == 0 || size(df, 2) == 0

Base.lastindex(df::AbstractDataFrame) = ncol(df)
Base.lastindex(df::AbstractDataFrame, i::Integer) = last(axes(df, i))
Base.axes(df::AbstractDataFrame, i::Integer) = Base.OneTo(size(df, i))
Expand Down Expand Up @@ -276,16 +278,6 @@ function Base.isequal(df1::AbstractDataFrame, df2::AbstractDataFrame)
return true
end

##############################################################################
##
## Associative methods
##
##############################################################################

Base.haskey(df::AbstractDataFrame, key::Any) = haskey(index(df), key)
Base.get(df::AbstractDataFrame, key::Any, default::Any) = haskey(df, key) ? df[key] : default
Base.isempty(df::AbstractDataFrame) = size(df, 1) == 0 || size(df, 2) == 0

##############################################################################
##
## Description
Expand Down Expand Up @@ -1140,7 +1132,7 @@ function _vcat(dfs::AbstractVector{<:AbstractDataFrame};
all_cols = Vector{AbstractVector}(undef, length(header))
for (i, name) in enumerate(header)
newcols = map(dfs) do df
if haskey(df, name)
if haskey(index(df), name)
return df[name]
else
Iterators.repeated(missing, nrow(df))
Expand Down
2 changes: 1 addition & 1 deletion src/abstractdataframe/join.jl
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ function Base.join(df1::AbstractDataFrame,
indicatorcol = CategoricalArray{String,1}(refs, CategoricalPool{String,UInt8}(["left_only", "right_only", "both"]))
unique_indicator = indicator
try_idx = 0
while haskey(joined, unique_indicator)
while haskey(index(joined), unique_indicator)
try_idx += 1
unique_indicator = Symbol(string(indicator, "_", try_idx))
end
Expand Down
4 changes: 2 additions & 2 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,14 +767,14 @@ function insertcols!(df::DataFrame, col_ind::Int, name_col::Pair{Symbol, <:Abstr
0 < col_ind <= ncol(df) + 1 || throw(BoundsError())
size(df, 1) == length(item) || size(df, 2) == 0 || error("number of rows does not match")

if haskey(df, name)
if haskey(index(df), name)
if makeunique
k = 1
while true
# we only make sure that new column name is unique
# if df originally had duplicates in names we do not fix it
nn = Symbol("$(name)_$k")
if !haskey(df, nn)
if !haskey(index(df), nn)
name = nn
break
end
Expand Down
6 changes: 6 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1394,3 +1394,9 @@ import Base: convert
@deprecate colwise(fns::Union{AbstractVector, Tuple}, d::AbstractDataFrame) [f(col) for f in fns, col in eachcol(d)]
@deprecate colwise(f, gd::GroupedDataFrame) [[f(col) for col in eachcol(d)] for d in gd]
@deprecate colwise(fns::Union{AbstractVector, Tuple}, gd::GroupedDataFrame) [[f(col) for f in fns, col in eachcol(d)] for d in gd]

import Base: get
@deprecate get(df::AbstractDataFrame, key::Any, default::Any) key in names(df) ? df[key] : default

import Base: haskey
@deprecate haskey(df::AbstractDataFrame, key::Any) key in names(df)
8 changes: 0 additions & 8 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,9 @@ end

@testset "Associative methods" begin
df = DataFrame(a=[1, 2], b=[3.0, 4.0])
@test haskey(df, :a)
@test !haskey(df, :c)
@test haskey(df, 1)
@test_throws MethodError haskey(df, 1.5)
@test_throws ArgumentError haskey(df, true)
@test get(df, :a, -1) === eachcol(df)[1]
@test get(df, :c, -1) == -1
@test !isempty(df)

dfv = view(df, 1:2, 1:2)
@test get(df, :a, -1) === eachcol(df)[1]

@test empty!(df) === df
@test isempty(eachcol(df))
Expand Down

0 comments on commit 41acf5d

Please sign in to comment.