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

[BREAKING] add empty and empty! #2262

Merged
merged 3 commits into from
May 18, 2020
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
2 changes: 2 additions & 0 deletions docs/src/lib/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ sortperm
pairs
parent
issorted
empty
empty!
```
8 changes: 8 additions & 0 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,14 @@ function Base.similar(df::AbstractDataFrame, rows::Integer = size(df, 1))
copycols=false)
end

"""
empty(df::AbstractDataFrame)

Create a new `DataFrame` with the same column names and column element types
as `df` but with zero rows.
"""
Base.empty(df::AbstractDataFrame) = similar(df, 0)

##############################################################################
##
## Equality
Expand Down
10 changes: 10 additions & 0 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,16 @@ end

Base.delete!(df::DataFrame, inds::Not) = delete!(df, axes(df, 1)[inds])

"""
empty!(df::DataFrame)

Remove all rows from `df`, making each of its columns empty.
"""
function Base.empty!(df::DataFrame)
foreach(empty!, eachcol(df))
return df
end

##############################################################################
##
## Hcat specialization
Expand Down
33 changes: 0 additions & 33 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,62 +29,29 @@ function DataFrame(column_eltypes::AbstractVector{T}, cnames::AbstractVector{Sym
return DataFrame(updated_types, cnames, nrows, makeunique=makeunique)
end

import Base: insert!
@deprecate insert!(df::DataFrame, df2::AbstractDataFrame) (foreach(col -> df[!, col] = df2[!, col], names(df2)); df)

import Base: show
@deprecate show(io::IO, df::AbstractDataFrame, allcols::Bool, rowlabel::Symbol, summary::Bool) show(io, df, allcols=allcols, rowlabel=rowlabel, summary=summary)
@deprecate show(io::IO, df::AbstractDataFrame, allcols::Bool, rowlabel::Symbol) show(io, df, allcols=allcols, rowlabel=rowlabel)
@deprecate show(io::IO, df::AbstractDataFrame, allcols::Bool) show(io, df, allcols=allcols)
@deprecate show(df::AbstractDataFrame, allcols::Bool, rowlabel::Symbol, summary::Bool) show(df, allcols=allcols, rowlabel=rowlabel, summary=summary)
@deprecate show(df::AbstractDataFrame, allcols::Bool, rowlabel::Symbol) show(df, allcols=allcols, rowlabel=rowlabel)
@deprecate show(df::AbstractDataFrame, allcols::Bool) show(df, allcols=allcols)

@deprecate showall(io::IO, df::AbstractDataFrame, allcols::Bool, rowlabel::Symbol, summary::Bool) show(io, df, allrows=true, allcols=allcols, rowlabel=rowlabel, summary=summary)
@deprecate showall(io::IO, df::AbstractDataFrame, allcols::Bool, rowlabel::Symbol) show(io, df, allrows=true, allcols=allcols, rowlabel=rowlabel)
@deprecate showall(io::IO, df::AbstractDataFrame, allcols::Bool = true) show(io, df, allrows=true, allcols=allcols)
@deprecate showall(df::AbstractDataFrame, allcols::Bool, rowlabel::Symbol, summary::Bool) show(df, allrows=true, allcols=allcols, rowlabel=rowlabel, summary=summary)
@deprecate showall(df::AbstractDataFrame, allcols::Bool, rowlabel::Symbol) show(df, allrows=true, allcols=allcols, rowlabel=rowlabel)
@deprecate showall(df::AbstractDataFrame, allcols::Bool = true) show(df, allrows=true, allcols=allcols)

@deprecate showall(io::IO, dfvec::AbstractVector{T}) where {T <: AbstractDataFrame} foreach(df->show(io, df, allrows=true, allcols=true), dfvec)
@deprecate showall(dfvec::AbstractVector{T}) where {T <: AbstractDataFrame} foreach(df->show(df, allrows=true, allcols=true), dfvec)

@deprecate showall(io::IO, df::GroupedDataFrame) show(io, df, allgroups=true)
@deprecate showall(df::GroupedDataFrame) show(df, allgroups=true)

import Base: insert!, merge!

@deprecate insert!(df::DataFrame, col_ind::Int, item, name::Symbol; makeunique::Bool=false) insertcols!(df, col_ind, name => item; makeunique=makeunique)
@deprecate merge!(df1::DataFrame, df2::AbstractDataFrame) (foreach(col -> df1[!, col] = df2[!, col], names(df2)); df1)

import Base: map
@deprecate map(f::Function, sdf::SubDataFrame) f(sdf)

@deprecate head(df::AbstractDataFrame) first(df, 6)
@deprecate tail(df::AbstractDataFrame) last(df, 6)
@deprecate head(df::AbstractDataFrame, n::Integer) first(df, n)
@deprecate tail(df::AbstractDataFrame, n::Integer) last(df, n)

@deprecate SubDataFrame(df::AbstractDataFrame, rows::AbstractVector{<:Integer}) SubDataFrame(df, rows, :)
@deprecate SubDataFrame(df::AbstractDataFrame, ::Colon) SubDataFrame(df, :, :)

@deprecate colwise(f, d::AbstractDataFrame) [f(col) for col in eachcol(d)]
@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::Symbol) hasproperty(df, key)
@deprecate haskey(df::AbstractDataFrame, key::Integer) key in 1:ncol(df)
@deprecate haskey(df::AbstractDataFrame, key::Any) key in 1:ncol(df) || key in names(df)

import Base: empty!
@deprecate empty!(df::DataFrame) select!(df, Int[])

@deprecate deletecols!(df::DataFrame, inds) select!(df, Not(inds))
@deprecate deletecols(df::DataFrame, inds; copycols::Bool=true) select(df, Not(inds), copycols=copycols)

Expand Down
15 changes: 15 additions & 0 deletions test/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,19 @@ end
end
end

@testset "empty and empty!" begin
df = DataFrame(a=1, b="x")
df1 = empty(df)
@test df == DataFrame(a=1, b="x")
@test names(df1) == ["a", "b"]
@test nrow(df1) == 0
@test eltype(df1.a) <: Int
@test eltype(df1.b) <: String
@test empty!(df) === df
@test names(df) == ["a", "b"]
@test nrow(df) == 0
@test eltype(df.a) <: Int
@test eltype(df.b) <: String
end

end # module
23 changes: 0 additions & 23 deletions test/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,6 @@ df = DataFrame(Union{Int, Missing}, 2, 2)
end
end

@testset "empty!" begin
df = DataFrame(a=[1, 2], b=[3.0, 4.0])
@test !isempty(df)

dfv = view(df, 1:2, 1:2)

@test empty!(df) === df
@test isempty(eachcol(df))
@test isempty(df)
@test isempty(DataFrame(a=[], b=[]))
end

@testset "deletecols and deletecols!" begin
df = DataFrame(a=[1,2], b=[3.0, 4.0])
@test deletecols(df, :a) == DataFrame(b=[3.0, 4.0])
Expand All @@ -205,17 +193,6 @@ end
@test deletecols(df, Not([])) == DataFrame()
end

@testset "haskey" begin
df = DataFrame(x=1:3)
@test haskey(df, 1)
@test !haskey(DataFrame(), 1)
@test !haskey(df, 2)
@test !haskey(df, 0)
@test haskey(df, :x)
@test !haskey(df, :a)
@test !haskey(df, "a")
end

@testset "df[col] and df[col] for getindex, view, and setindex" begin
@testset "getindex DataFrame" begin
df = DataFrame(a=1:3, b=4:6, c=7:9)
Expand Down