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

Names for DataFrames constructed from vector of rows (of tuples) #3315

Closed
Keno opened this issue Apr 18, 2023 · 5 comments · Fixed by #3320
Closed

Names for DataFrames constructed from vector of rows (of tuples) #3315

Keno opened this issue Apr 18, 2023 · 5 comments · Fixed by #3320
Labels
Milestone

Comments

@Keno
Copy link
Contributor

Keno commented Apr 18, 2023

I tried constructing a DataFrame from a vector of rows (of tuples), I have, which works beautifully.
However, neither of the mechanisms I tried to give it names works. I understand I could make
the array one of NamedTuples, but it seems like there should be a convenient way to pass
the names as extra metadata. Or if there is a good way, consider this a request to add it
to the DataFrame doc string ;).

julia> A = Any[(1, "A", 3); (4, "B", 6)]
2-element Vector{Any}:
 (1, "A", 3)
 (4, "B", 6)

julia> DataFrame(A)
2×3 DataFrame
 Row │ 1      2       3
     │ Int64  String  Int64
─────┼──────────────────────
   1 │     1  A           3
   2 │     4  B           6

julia> DataFrame(A, ["A", "B", "C"])
ERROR: ArgumentError: columns argument must be a vector of AbstractVector objects
Stacktrace:
 [1] DataFrame(columns::Vector{Any}, cnames::Vector{Symbol}; makeunique::Bool, copycols::Bool)
   @ DataFrames ~/.julia/packages/DataFrames/LteEl/src/dataframe/dataframe.jl:333
 [2] #DataFrame#225
   @ ~/.julia/packages/DataFrames/LteEl/src/dataframe/dataframe.jl:348 [inlined]
 [3] DataFrame(columns::Vector{Any}, cnames::Vector{String})
   @ DataFrames ~/.julia/packages/DataFrames/LteEl/src/dataframe/dataframe.jl:348
 [4] top-level scope
   @ REPL[34]:1

julia> DataFrame(A, names = ["A", "B", "C"])
ERROR: MethodError: no method matching DataFrame(::Vector{Any}; names::Vector{String})

Closest candidates are:
  DataFrame(::Union{Vector{Any}, Vector{AbstractVector}}, ::DataFrames.Index; copycols) got unsupported keyword argument "names"
   @ DataFrames ~/.julia/packages/DataFrames/LteEl/src/dataframe/dataframe.jl:187
  DataFrame(::AbstractVector, ::AbstractVector{Symbol}; makeunique, copycols) got unsupported keyword argument "names"
   @ DataFrames ~/.julia/packages/DataFrames/LteEl/src/dataframe/dataframe.jl:330
  DataFrame(::AbstractVector, ::AbstractVector; makeunique, copycols) got unsupported keyword argument "names"
   @ DataFrames ~/.julia/packages/DataFrames/LteEl/src/dataframe/dataframe.jl:348
  ...

Stacktrace:
 [1] kwerr(::NamedTuple{(:names,), Tuple{Vector{String}}}, ::Type, ::Vector{Any})
   @ Base ./error.jl:165
 [2] kwcall(::NamedTuple{(:names,), Tuple{Vector{String}}}, ::Type{DataFrame}, x::Vector{Any})
   @ DataFrames ~/.julia/packages/DataFrames/LteEl/src/other/tables.jl:48
 [3] top-level scope
   @ REPL[35]:1
@bkamins bkamins added this to the 1.6 milestone Apr 18, 2023
@bkamins
Copy link
Member

bkamins commented Apr 18, 2023

The reason you observe this behavior is that you are using the Tables.jl tables fallback constructor. That is, your A is considered to be a table in the first place:

julia> A = Any[(1, "A", 3); (4, "B", 6)]
2-element Vector{Any}:
 (1, "A", 3)
 (4, "B", 6)

julia> Tables.columntable(A)
(var"1" = [1, 4], var"2" = ["A", "B"], var"3" = [3, 6])

So the issue is that your request is to change the column names that Tables.jl table already gave (that is 1, 2, and 3 symbols).

Currently this can be done like this:

julia> rename!(DataFrame(A), ["A", "B", "C"])
2×3 DataFrame
 Row │ A      B       C
     │ Int64  String  Int64
─────┼──────────────────────
   1 │     1  A           3
   2 │     4  B           6

What you request is to create a new constructor:

DataFrame(table, new_column_names) = rename!(DataFrame(table), new_column_names)

Do you think it would be useful to have it, or you are fine with the rename! solution?

@Keno
Copy link
Contributor Author

Keno commented Apr 18, 2023

I'm fine with the rename! solution, but if the constructor you suggest is unambiguous might as well add it. If you decide not to add the functionality, I would still suggest to implement the constructor to give an error message that points users to the rename! function, since the difference between the tables fallback and the columns constructor may not be obvious to DataFrames-naive users (like myself).

@bkamins
Copy link
Member

bkamins commented Apr 18, 2023

if the constructor you suggest is unambiguous

It should be unambiguous. I think we can add it.

@nalimilan, @oxinabox - do you have any comments on this?

@quinnj
Copy link
Member

quinnj commented Apr 18, 2023

I think the additional constructor sounds like a good idea.

@bkamins
Copy link
Member

bkamins commented Apr 20, 2023

added in #3320

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants