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

make DataFrame constructor more flexible #2859

Merged
merged 5 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
(with `false` default) that specifies if columns should be inserted after
or before `col`.
([#2829](https://github.com/JuliaData/DataFrames.jl/pull/2829))
* the `DataFrame` constructor when column names are passed to it as a second
argument now determines if a passed vector of column names is valid based on
its contents and not element type
([#2829](https://github.com/JuliaData/DataFrames.jl/pull/2859))

## Bug fixes

Expand Down
20 changes: 13 additions & 7 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,26 @@ function DataFrame(columns::AbstractVector, cnames::AbstractVector{Symbol};
copycols=copycols)
end

DataFrame(columns::AbstractVector, cnames::AbstractVector{<:AbstractString};
function _name2symbol(str::AbstractVector)
if !(all(x -> x isa AbstractString, str) || all(x -> x isa Symbol, str))
throw(ArgumentError("All passed column names must be strings or Symbols"))
end
return Symbol[Symbol(s) for s in str]
end

DataFrame(columns::AbstractVector, cnames::AbstractVector;
makeunique::Bool=false, copycols::Bool=true) =
DataFrame(columns, Symbol.(cnames), makeunique=makeunique, copycols=copycols)
DataFrame(columns, _name2symbol(cnames), makeunique=makeunique, copycols=copycols)

DataFrame(columns::AbstractVector{<:AbstractVector}, cnames::AbstractVector{Symbol};
makeunique::Bool=false, copycols::Bool=true)::DataFrame =
DataFrame(collect(AbstractVector, columns),
Index(convert(Vector{Symbol}, cnames), makeunique=makeunique),
copycols=copycols)

DataFrame(columns::AbstractVector{<:AbstractVector}, cnames::AbstractVector{<:AbstractString};
DataFrame(columns::AbstractVector{<:AbstractVector}, cnames::AbstractVector;
makeunique::Bool=false, copycols::Bool=true) =
DataFrame(columns, Symbol.(cnames); makeunique=makeunique, copycols=copycols)
DataFrame(columns, _name2symbol(cnames); makeunique=makeunique, copycols=copycols)

function DataFrame(columns::AbstractVector, cnames::Symbol; copycols::Bool=true)
if cnames !== :auto
Expand All @@ -350,9 +357,9 @@ DataFrame(columns::AbstractMatrix, cnames::AbstractVector{Symbol}; makeunique::B
DataFrame(AbstractVector[columns[:, i] for i in 1:size(columns, 2)], cnames,
makeunique=makeunique, copycols=false)

DataFrame(columns::AbstractMatrix, cnames::AbstractVector{<:AbstractString};
DataFrame(columns::AbstractMatrix, cnames::AbstractVector;
makeunique::Bool=false) =
DataFrame(columns, Symbol.(cnames); makeunique=makeunique)
DataFrame(columns, _name2symbol(cnames); makeunique=makeunique)

function DataFrame(columns::AbstractMatrix, cnames::Symbol)
if cnames !== :auto
Expand Down Expand Up @@ -388,7 +395,6 @@ DataFrame(column_eltypes::AbstractVector{<:Type}, cnames::AbstractVector{<:Abstr
"not supported. Pass explicitly created columns to a " *
"`DataFrame` constructor instead."))


##############################################################################
##
## AbstractDataFrame interface
Expand Down
19 changes: 19 additions & 0 deletions test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ end
@test_throws ArgumentError DataFrame([Int, Float64], [:a, :b], 2)
@test_throws ArgumentError DataFrame([Int, Float64], ["a", "b"])
@test_throws ArgumentError DataFrame([Int, Float64], ["a", "b"], 2)
@test_throws ArgumentError DataFrame([Int, Float64], ["a", :b])
@test_throws MethodError DataFrame([Int, Float64], ["a", :b], 2)
end

@testset "threading correctness tests" begin
Expand All @@ -372,4 +374,21 @@ end
end
end

@testset "non-specific vector of column names" begin
ref = DataFrame(a=1:2, b=3:4)
for x in ([1 3; 2 4], [[1, 2], [3, 4]], [1:2, 3:4], Any[[1, 2], [3, 4]], Any[1:2, 3:4])
@test DataFrame(x, Any[:a, :b]) == ref
@test DataFrame(x, Any["a", "b"]) == ref
@test DataFrame(x, Union{String, Symbol}[:a, :b]) == ref
@test DataFrame(x, Union{String, Symbol}["a", "b"]) == ref
@test_throws ArgumentError DataFrame(x, Any["a", :b])
@test_throws ArgumentError DataFrame(x, Union{String, Symbol}["a", :b])
end
@test DataFrame([], []) == DataFrame()
@test DataFrame(fill(0, 0, 0), []) == DataFrame()
@test_throws ArgumentError DataFrame(Type[], Symbol[])
@test_throws ArgumentError DataFrame(Type[], String[])
@test_throws MethodError DataFrame(Type[], [])
end

end # module