Skip to content

Commit

Permalink
Deprecate eltypes
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Sep 3, 2019
1 parent cbfef8d commit f849348
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 101 deletions.
1 change: 0 additions & 1 deletion src/DataFrames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export AbstractDataFrame,
disallowmissing!,
dropmissing,
dropmissing!,
eltypes,
groupby,
groupindices,
groupvars,
Expand Down
28 changes: 1 addition & 27 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ The following are normally implemented for AbstractDataFrames:
* `names` : columns names
* [`names!`](@ref) : set columns names
* [`rename!`](@ref) : rename columns names based on keyword arguments
* [`eltypes`](@ref) : `eltype` of each column
* `length` : number of columns
* `size` : (nrows, ncols)
* [`first`](@ref) : first `n` rows
Expand Down Expand Up @@ -171,31 +170,6 @@ rename!(df, Dict(:i =>: A, :x => :X))
"""
(rename!, rename)

"""
Return element types of columns
```julia
eltypes(df::AbstractDataFrame)
```
**Arguments**
* `df` : the AbstractDataFrame
**Result**
* `::Vector{Type}` : the element type of each column
**Examples**
```julia
df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
eltypes(df)
```
"""
eltypes(df::AbstractDataFrame) = eltype.(eachcol(df))

Base.size(df::AbstractDataFrame) = (nrow(df), ncol(df))
function Base.size(df::AbstractDataFrame, i::Integer)
if i == 1
Expand Down Expand Up @@ -790,7 +764,7 @@ Base.filter!(f, df::AbstractDataFrame) =
deleterows!(df, findall(collect(!f(r)::Bool for r in eachrow(df))))

function Base.convert(::Type{Matrix}, df::AbstractDataFrame)
T = reduce(promote_type, eltypes(df))
T = reduce(promote_type, (eltype(v) for v in eachcol(df)))
convert(Matrix{T}, df)
end
function Base.convert(::Type{Matrix{T}}, df::AbstractDataFrame) where T
Expand Down
4 changes: 2 additions & 2 deletions src/abstractdataframe/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function _show(io::IO, ::MIME"text/latex", df::AbstractDataFrame; rowid=nothing)
write(io, "\\\\\n")
write(io, "\t\\hline\n")
write(io, "\t& ")
header = join(map(c -> latex_escape(string(compacttype(c))), eltypes(df)[1:mxcol]), " & ")
header = join(map(c -> latex_escape(string(compacttype(c))), eltype.(eachcol(df)[1:mxcol])), " & ")
write(io, header)
mxcol < size(df, 2) && write(io, " & ")
write(io, "\\\\\n")
Expand Down Expand Up @@ -352,7 +352,7 @@ function printtable(io::IO,
missingstring::AbstractString = "missing")
_check_consistency(df)
n, p = size(df)
etypes = eltypes(df)
etypes = eltype.(eachcol(df))
if header
cnames = _names(df)
for j in 1:p
Expand Down
2 changes: 1 addition & 1 deletion src/abstractdataframe/reshape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ end
# no vars specified, by default select only numeric columns
numeric_vars(df::AbstractDataFrame) =
[T <: AbstractFloat || (T >: Missing && Missings.T(T) <: AbstractFloat)
for T in eltypes(df)]
for T in eltype.(eachcol(df))]

function stack(df::AbstractDataFrame, measure_vars = numeric_vars(df);
variable_name::Symbol=:variable, value_name::Symbol=:value)
Expand Down
4 changes: 2 additions & 2 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ julia> categorical!(df)
│ 1 │ a │ 1 │ p │
│ 2 │ b │ 2 │ q │
julia> eltypes(df)
julia> eltype.(eachcol(df))
3-element Array{DataType,1}:
CategoricalString{UInt32}
Int64
Expand All @@ -1074,7 +1074,7 @@ julia> categorical!(df, :Y, compress=true)
│ 1 │ a │ 1 │ p │
│ 2 │ b │ 2 │ q │
julia> eltypes(df)
julia> eltype.(eachcol(df))
3-element Array{DataType,1}:
String
CategoricalValue{Int64,UInt8}
Expand Down
2 changes: 1 addition & 1 deletion src/dataframerow/dataframerow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ end
Base.IteratorEltype(::Type{<:DataFrameRow}) = Base.EltypeUnknown()

function Base.convert(::Type{Vector}, dfr::DataFrameRow)
T = reduce(promote_type, eltypes(parent(dfr)))
T = reduce(promote_type, (eltype(v) for v in eachcol(parent(dfr))))
convert(Vector{T}, dfr)
end
Base.convert(::Type{Vector{T}}, dfr::DataFrameRow) where T =
Expand Down
2 changes: 2 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1535,3 +1535,5 @@ end
import Base: setproperty!
@deprecate setproperty!(df::DataFrame, col_ind::Symbol, v) (df[!, col_ind] .= v)
@deprecate setproperty!(df::SubDataFrame, col_ind::Symbol, v) (df[:, col_ind] .= v)

@deprecate eltypes(df::AbstractDataFrame) eltype.(eachcol(df))
4 changes: 2 additions & 2 deletions src/groupeddataframe/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -754,13 +754,13 @@ function _combine_with_first(first::Union{NamedTuple, DataFrameRow, AbstractData
incols::Union{Nothing, AbstractVector, NamedTuple})
if first isa AbstractDataFrame
n = 0
eltys = eltypes(first)
eltys = eltype.(eachcol(first))
elseif first isa NamedTuple{<:Any, <:Tuple{Vararg{AbstractVector}}}
n = 0
eltys = map(eltype, first)
elseif first isa DataFrameRow
n = length(gd)
eltys = eltypes(parent(first))
eltys = eltype.(eachcol(parent(first)))
else # NamedTuple giving a single row
n = length(gd)
eltys = map(typeof, first)
Expand Down
2 changes: 1 addition & 1 deletion src/other/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tables.columns(df::AbstractDataFrame) = df
Tables.rowaccess(::Type{<:AbstractDataFrame}) = true
Tables.rows(df::AbstractDataFrame) = Tables.rows(columntable(df))

Tables.schema(df::AbstractDataFrame) = Tables.Schema(names(df), eltypes(df))
Tables.schema(df::AbstractDataFrame) = Tables.Schema(names(df), eltype.(eachcol(df)))
Tables.materializer(df::AbstractDataFrame) = DataFrame

getvector(x::AbstractVector) = x
Expand Down
10 changes: 5 additions & 5 deletions test/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ end
@test df (x->x).(df)
df3 = coalesce.(df, nothing)
@test df2 == df3
@test eltypes(df2) == eltypes(df3)
@test eltype.(eachcol(df2)) == eltype.(eachcol(df3))
for i in axes(df, 2)
@test typeof(df2[!, i]) == typeof(df3[!, i])
end
Expand All @@ -127,9 +127,9 @@ end
@test identity.(df5) == df5
@test (x->x).(df5) == df5
@test df5 .+ 1 == DataFrame(Matrix(df5) .+ 1, names(df5))
@test eltypes(identity.(df5)) == [Int, BigFloat]
@test eltypes((x->x).(df5)) == [Int, BigFloat]
@test eltypes(df5 .+ 1) == [Int, BigFloat]
@test eltype.(eachcol(identity.(df5))) == [Int, BigFloat]
@test eltype.(eachcol((x->x).(df5))) == [Int, BigFloat]
@test eltype.(eachcol(df5 .+ 1)) == [Int, BigFloat]
end

@testset "normal data frame and data frame row in broadcasted assignment - one column" begin
Expand Down Expand Up @@ -753,7 +753,7 @@ end
@test_throws InexactError convert.(Int, df)
df2 = convert.(Int, floor.(df))
@test df2 == DataFrame(zeros(Int, 2, 3))
@test eltypes(df2) == [Int, Int, Int]
@test eltype.(eachcol(df2)) == [Int, Int, Int]
end

@testset "scalar on assignment side" begin
Expand Down
4 changes: 2 additions & 2 deletions test/cat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ end
@test vcat(missing_df, missing_df) == DataFrame()
@test vcat(missing_df, df) == df
@test vcat(df, missing_df) == df
@test eltypes(vcat(df, df)) == Type[Float64, Float64, Int]
@test eltype.(eachcol(vcat(df, df))) == Type[Float64, Float64, Int]
@test size(vcat(df, df)) == (size(df, 1) * 2, size(df, 2))
res = vcat(df, df)
@test res[1:size(df, 1), :] == df
@test res[(1+size(df, 1)):end, :] == df
res = vcat(df, df, df)
@test eltypes(res) == Type[Float64, Float64, Int]
@test eltype.(eachcol(res)) == Type[Float64, Float64, Int]
@test size(res) == (size(df, 1) * 3, size(df, 2))

s = size(df, 1)
Expand Down
6 changes: 3 additions & 3 deletions test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ end
df = DataFrame(Dict(:A => 1:3, :B => 4:6))
@inferred DataFrame(Dict(:A => 1:3, :B => 4:6))
@test df == DataFrame(A = 1:3, B = 4:6)
@test eltypes(df) == [Int, Int]
@test eltype.(eachcol(df)) == [Int, Int]

a=[1,2,3]
df = DataFrame(Dict(:a=>a, :b=>1, :c=>1:3))
Expand Down Expand Up @@ -372,12 +372,12 @@ end

df = DataFrame([Union{Int, Missing}, Union{Float64, Missing}], [:x1, :x2], 2)
@test size(df) == (2, 2)
@test eltypes(df) == [Union{Int, Missing}, Union{Float64, Missing}]
@test eltype.(eachcol(df)) == [Union{Int, Missing}, Union{Float64, Missing}]

@test_throws ArgumentError DataFrame!([Union{Int, Missing}, Union{Float64, Missing}],
[:x1, :x2], 2)
@test size(df) == (2, 2)
@test eltypes(df) == [Union{Int, Missing}, Union{Float64, Missing}]
@test eltype.(eachcol(df)) == [Union{Int, Missing}, Union{Float64, Missing}]

df = DataFrame([Union{Int, Missing}, Union{Float64, Missing}, Union{String, Missing}],
[:A, :B, :C])
Expand Down
50 changes: 25 additions & 25 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1040,46 +1040,46 @@ end

@testset "categorical!" begin
df = DataFrame([["a", "b"], ['a', 'b'], [true, false], 1:2, ["x", "y"]])
@test all(map(<:, eltypes(categorical!(deepcopy(df))),
@test all(map(<:, eltype.(eachcol(categorical!(deepcopy(df)))),
[CategoricalArrays.CategoricalString{UInt32},
Char, Bool, Int,
CategoricalArrays.CategoricalString{UInt32}]))
@test all(map(<:, eltypes(categorical!(deepcopy(df), :)),
@test all(map(<:, eltype.(eachcol(categorical!(deepcopy(df), :))),
[CategoricalArrays.CategoricalString{UInt32},
CategoricalArrays.CategoricalValue{Char,UInt32},
CategoricalArrays.CategoricalValue{Bool,UInt32},
CategoricalArrays.CategoricalValue{Int,UInt32},
CategoricalArrays.CategoricalString{UInt32}]))
@test all(map(<:, eltypes(categorical!(deepcopy(df), compress=true)),
@test all(map(<:, eltype.(eachcol(categorical!(deepcopy(df), compress=true))),
[CategoricalArrays.CategoricalString{UInt8},
Char, Bool, Int,
CategoricalArrays.CategoricalString{UInt8}]))
@test all(map(<:, eltypes(categorical!(deepcopy(df), names(df))),
@test all(map(<:, eltype.(eachcol(categorical!(deepcopy(df), names(df)))),
[CategoricalArrays.CategoricalString{UInt32},
CategoricalArrays.CategoricalValue{Char,UInt32},
CategoricalArrays.CategoricalValue{Bool,UInt32},
CategoricalArrays.CategoricalValue{Int,UInt32},
CategoricalArrays.CategoricalString{UInt32}]))
@test all(map(<:, eltypes(categorical!(deepcopy(df), names(df), compress=true)),
@test all(map(<:, eltype.(eachcol(categorical!(deepcopy(df), names(df), compress=true))),
[CategoricalArrays.CategoricalString{UInt8},
CategoricalArrays.CategoricalValue{Char,UInt8},
CategoricalArrays.CategoricalValue{Bool,UInt8},
CategoricalArrays.CategoricalValue{Int,UInt8},
CategoricalArrays.CategoricalString{UInt8}]))
@test all(map(<:, eltypes(categorical!(deepcopy(df), Not(1:0))),
@test all(map(<:, eltype.(eachcol(categorical!(deepcopy(df), Not(1:0)))),
[CategoricalArrays.CategoricalString{UInt32},
CategoricalArrays.CategoricalValue{Char,UInt32},
CategoricalArrays.CategoricalValue{Bool,UInt32},
CategoricalArrays.CategoricalValue{Int,UInt32},
CategoricalArrays.CategoricalString{UInt32}]))
@test all(map(<:, eltypes(categorical!(deepcopy(df), Not(1:0), compress=true)),
@test all(map(<:, eltype.(eachcol(categorical!(deepcopy(df), Not(1:0), compress=true))),
[CategoricalArrays.CategoricalString{UInt8},
CategoricalArrays.CategoricalValue{Char,UInt8},
CategoricalArrays.CategoricalValue{Bool,UInt8},
CategoricalArrays.CategoricalValue{Int,UInt8},
CategoricalArrays.CategoricalString{UInt8}]))

@test all(map(<:, eltypes(categorical!(deepcopy(df), Integer)),
@test all(map(<:, eltype.(eachcol(categorical!(deepcopy(df), Integer))),
[String, Char,
CategoricalArrays.CategoricalValue{Bool,UInt32},
CategoricalArrays.CategoricalValue{Int,UInt32},
Expand Down Expand Up @@ -1217,15 +1217,15 @@ end
CategoricalArray(string.('a':'j'))])
@test allowmissing!(df) === df
@test all(x->x <: CategoricalVector, typeof.(eachcol(df)))
@test eltypes(df)[1] <: Union{CategoricalValue{Int}, Missing}
@test eltypes(df)[2] <: Union{CategoricalString, Missing}
@test eltype(df[!, 1]) <: Union{CategoricalValue{Int}, Missing}
@test eltype(df[!, 2]) <: Union{CategoricalString, Missing}
df[1,2] = missing
@test_throws MissingException disallowmissing!(df)
df[1,2] = "a"
@test disallowmissing!(df) === df
@test all(x->x <: CategoricalVector, typeof.(eachcol(df)))
@test eltypes(df)[1] <: CategoricalValue{Int}
@test eltypes(df)[2] <: CategoricalString
@test eltype(df[!, 1]) <: CategoricalValue{Int}
@test eltype(df[!, 2]) <: CategoricalString

df = DataFrame(b=[1,2], c=[1,2], d=[1,2])
@test allowmissing!(df, [:b, :c]) === df
Expand Down Expand Up @@ -1257,7 +1257,7 @@ end
@test x.x !== y.x
@test x.y !== y.y
@test x.z !== y.z
@test eltypes(y) == [Int, Int, Int]
@test eltype.(eachcol(y)) == [Int, Int, Int]

for colsel in [:, names(x), [1,2,3], [true,true,true], r"", Not(r"a")]
y = disallowmissing(x, colsel)
Expand All @@ -1266,7 +1266,7 @@ end
@test x.x !== y.x
@test x.y !== y.y
@test x.z !== y.z
@test eltypes(y) == [Int, Int, Int]
@test eltype.(eachcol(y)) == [Int, Int, Int]
end

for colsel in [:x, 1, [:x], [1], [true, false, false], r"x", Not(2:3)]
Expand All @@ -1276,7 +1276,7 @@ end
@test x.x !== y.x
@test x.y !== y.y
@test x.z !== y.z
@test eltypes(y) == [Int, Union{Missing, Int}, Int]
@test eltype.(eachcol(y)) == [Int, Union{Missing, Int}, Int]
end

for colsel in [:z, 3, [:z], [3], [false, false, true], r"z", Not(1:2)]
Expand All @@ -1286,7 +1286,7 @@ end
@test x.x !== y.x
@test x.y !== y.y
@test x.z !== y.z
@test eltypes(y) == [Union{Int, Missing}, Union{Int, Missing}, Int]
@test eltype.(eachcol(y)) == [Union{Int, Missing}, Union{Int, Missing}, Int]
end

for colsel in [Int[], Symbol[], [false, false, false], r"a", Not(:)]
Expand All @@ -1296,7 +1296,7 @@ end
@test x.x !== y.x
@test x.y !== y.y
@test x.z !== y.z
@test eltypes(y) == [Union{Int, Missing}, Union{Int, Missing}, Int]
@test eltype.(eachcol(y)) == [Union{Int, Missing}, Union{Int, Missing}, Int]
end
end

Expand All @@ -1315,7 +1315,7 @@ end
@test x.x !== y.x
@test x.y !== y.y
@test x.z !== y.z
@test eltypes(y) == fill(Union{Missing, Int}, 3)
@test eltype.(eachcol(y)) == fill(Union{Missing, Int}, 3)

for colsel in [:, names(x), [1,2,3], [true,true,true], r"", Not(r"a")]
y = allowmissing(x, colsel)
Expand All @@ -1324,7 +1324,7 @@ end
@test x.x !== y.x
@test x.y !== y.y
@test x.z !== y.z
@test eltypes(y) == fill(Union{Missing, Int}, 3)
@test eltype.(eachcol(y)) == fill(Union{Missing, Int}, 3)
end

for colsel in [:x, 1, [:x], [1], [true, false, false], r"x", Not(2:3)]
Expand All @@ -1334,7 +1334,7 @@ end
@test x.x !== y.x
@test x.y !== y.y
@test x.z !== y.z
@test eltypes(y) == [Union{Missing, Int}, Int, Int]
@test eltype.(eachcol(y)) == [Union{Missing, Int}, Int, Int]
end

for colsel in [:z, 3, [:z], [3], [false, false, true], r"z", Not(1:2)]
Expand All @@ -1344,7 +1344,7 @@ end
@test x.x !== y.x
@test x.y !== y.y
@test x.z !== y.z
@test eltypes(y) == [Union{Int, Missing}, Int, Union{Missing, Int}]
@test eltype.(eachcol(y)) == [Union{Int, Missing}, Int, Union{Missing, Int}]
end

for colsel in [Int[], Symbol[], [false, false, false], r"a", Not(:)]
Expand All @@ -1354,7 +1354,7 @@ end
@test x.x !== y.x
@test x.y !== y.y
@test x.z !== y.z
@test eltypes(y) == [Union{Int, Missing}, Int, Int]
@test eltype.(eachcol(y)) == [Union{Int, Missing}, Int, Int]
end
end
end
Expand Down Expand Up @@ -1439,15 +1439,15 @@ end
b = CategoricalArray(["foo"]),
c = [0.0],
d = CategoricalArray([0.0]))
@test eltypes(similar(df)) == eltypes(df)
@test eltype.(eachcol(similar(df))) == eltype.(eachcol(df))
@test size(similar(df)) == size(df)

rows = size(df, 1) + 5
@test size(similar(df, rows)) == (rows, size(df, 2))
@test eltypes(similar(df, rows)) == eltypes(df)
@test eltype.(eachcol(similar(df, rows))) == eltype.(eachcol(df))

@test size(similar(df, 0)) == (0, size(df, 2))
@test eltypes(similar(df, 0)) == eltypes(df)
@test eltype.(eachcol(similar(df, 0))) == eltype.(eachcol(df))

e = @test_throws ArgumentError similar(df, -1)
@test e.value.msg == "the number of rows must be non-negative"
Expand Down
Loading

0 comments on commit f849348

Please sign in to comment.