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

[Feature request] Support for converting single-column dataframes to Vectors #2526

Closed
jakewilliami opened this issue Nov 10, 2020 · 1 comment

Comments

@jakewilliami
Copy link

This might not be that useful—maybe it is just me that would like this. Let me know what you think (example below).

julia> df = DataFrame(A = [1, 2, 3, 4])
4×1 DataFrame
│ Row │ A     │
│     │ Int64 │
├─────┼───────┤
│ 11     │
│ 22     │
│ 33     │
│ 44     │

julia> Vector(df)
ERROR: MethodError: no method matching Array{T,1} where T(::DataFrame)
  ...

julia> function Vector(df::DataFrame)
       if isone(ncol(df))
       return vec(Matrix(df))
       else
       error("Cannot convert a multi-column dataframe to a vector.")
       end
       end
Array{T,1} where T

julia> Vector(df)
4-element Array{Int64,1}:
 1
 2
 3
 4
@bkamins
Copy link
Member

bkamins commented Nov 10, 2020

This fails for matrices:

julia> x = rand(2,2)
2×2 Array{Float64,2}:
 0.965899  0.90723
 0.653601  0.99919

julia> Vector(x)
ERROR: MethodError: no method matching Array{T,1} where T(::Array{Float64,2})

so I think it should fail for data frames.

A more natural pattern to get what you want is:

only(eachcol(df))

with the only difference that it will not copy the extracted vector as opposed to your proposal.

@bkamins bkamins closed this as completed Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants