Skip to content

Commit

Permalink
[BREAKING] deprecate DataFrame constructors (#2464)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Nov 6, 2020
1 parent e0599a9 commit 55533d1
Show file tree
Hide file tree
Showing 23 changed files with 647 additions and 575 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -91,6 +91,8 @@
## Deprecated

* `DataFrame!` is now deprecated ([#2338](https://github.com/JuliaData/DataFrames.jl/pull/2338))
* several in-standard `DataFrame` constructors are now deprecated
([#2464](https://github.com/JuliaData/DataFrames.jl/pull/2464))
* all old deprecations now throw an error
([#2350](https://github.com/JuliaData/DataFrames.jl/pull/2350))

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Expand Up @@ -44,5 +44,5 @@ Missings = "0.4.2"
PooledArrays = "0.5"
Reexport = "0.1, 0.2"
SortingAlgorithms = "0.1, 0.2, 0.3"
Tables = "1"
Tables = "1.1"
TableTraits = "0.4, 1"
319 changes: 183 additions & 136 deletions src/dataframe/dataframe.jl

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions src/deprecated.jl
Expand Up @@ -105,3 +105,43 @@ function categorical!(df::DataFrame, cols::Union{Type, Nothing}=nothing;
end
return transform!(df, names(df, cols) .=> (x -> categorical(x, compress=compress)), renamecols=false)
end

@deprecate DataFrame(pairs::NTuple{N, Pair}; makeunique::Bool=false,
copycols::Bool=true) where {N} DataFrame(pairs..., makeunique=makeunique, copycols=copycols)
@deprecate DataFrame(columns::NTuple{N, AbstractVector}, cnames::NTuple{N, Symbol}; makeunique::Bool=false,
copycols::Bool=true) where {N} DataFrame(collect(columns), collect(cnames);
makeunique=makeunique, copycols=copycols)
@deprecate DataFrame(columns::NTuple{N, AbstractVector}, cnames::NTuple{N, AbstractString}; makeunique::Bool=false,
copycols::Bool=true) where {N} DataFrame(collect(columns), [Symbol(c) for c in cnames];
makeunique=makeunique, copycols=copycols)
@deprecate DataFrame(columns::NTuple{N, AbstractVector};
copycols::Bool=true) where {N} DataFrame(collect(columns),
Symbol.(:x, 1:length(columns)), copycols=copycols)

# this deprecation is very important, becuase without it users will
# get strange results with old code as described in https://github.com/JuliaData/Tables.jl/issues/208
@deprecate DataFrame(columns::AbstractVector{<:AbstractVector}; makeunique::Bool=false,
copycols::Bool=true) DataFrame(columns, :auto, copycols=copycols)

@deprecate DataFrame(columns::AbstractMatrix) DataFrame(columns, :auto)

function DataFrame(column_eltypes::AbstractVector{T}, cnames::AbstractVector{Symbol},
nrows::Integer=0; makeunique::Bool=false)::DataFrame where T<:Type
Base.depwarn("`DataFrame` constructor with passed eltypes is deprecated. " *
"Pass explicitly created columns to a `DataFrame` constructor instead.",
:DataFrame)
columns = AbstractVector[elty >: Missing ?
fill!(Tables.allocatecolumn(elty, nrows), missing) :
Tables.allocatecolumn(elty, nrows)
for elty in column_eltypes]
return DataFrame(columns, Index(convert(Vector{Symbol}, cnames),
makeunique=makeunique), copycols=false)
end

DataFrame(column_eltypes::AbstractVector{<:Type},
cnames::AbstractVector{<:AbstractString},
nrows::Integer=0; makeunique::Bool=false) =
DataFrame(column_eltypes, Symbol.(cnames), nrows; makeunique=makeunique)

import Base: convert
@deprecate convert(::Type{DataFrame}, A::AbstractMatrix) DataFrame(Tables.table(A, header=Symbol.(:x, axes(A, 2))))
10 changes: 5 additions & 5 deletions src/other/tables.jl
Expand Up @@ -43,11 +43,11 @@ fromcolumns(x, names; copycols::Bool=true) =
copycols=copycols)

function DataFrame(x::T; copycols::Bool=true) where {T}
if !Tables.istable(x)
if x isa AbstractVector && all(col -> isa(col, AbstractVector), x)
return DataFrame(Vector{AbstractVector}(x), copycols=copycols)
elseif (x isa AbstractVector || x isa Tuple) &&
all(v -> v isa Pair{Symbol, <:AbstractVector}, x)
if !Tables.istable(x) && x isa AbstractVector && !isempty(x)
# here we handle eltypes not specific enough to be dispatched
# to other DataFrames constructors taking vector of `Pair`s
if all(v -> v isa Pair{Symbol, <:AbstractVector}, x) ||
all(v -> v isa Pair{<:AbstractString, <:AbstractVector}, x)
return DataFrame(AbstractVector[last(v) for v in x], [first(v) for v in x],
copycols=copycols)
end
Expand Down

0 comments on commit 55533d1

Please sign in to comment.