Cannot convert an object of type DataFrame to an object of type Array

Hi,
I have a Data Frame that I want to convert into an Array but Julia gives me the following error:
MethodError: Cannot convert an object of type
DataFrame to an object of type
Array

I believe that there has something to do with the new version of the DataFrames package but I can’t find anything in the documentation. I would appreciate any help.

Ps. I can’t upload the data because it’s confidential but would appreciate any help :slight_smile:

1 Like

You’ll get more help if you can reduce your problem down to a minimal example. But what you may want is passing your dataframe to the Matrix constructor, which will convert it to a 2d array, possibly of an abstract type if necessary

julia> df = DataFrame(a=[1, 2], b=[3, 4])
2Γ—2 DataFrame
 Row β”‚ a      b     
     β”‚ Int64  Int64 
─────┼──────────────
   1 β”‚     1      3
   2 β”‚     2      4

julia> Matrix(df)
2Γ—2 Matrix{Int64}:
 1  3
 2  4

julia> df = DataFrame(a=[1, 2], b=["some", "strings"])
2Γ—2 DataFrame
 Row β”‚ a      b       
     β”‚ Int64  String  
─────┼────────────────
   1 β”‚     1  some
   2 β”‚     2  strings

julia> Matrix(df)
2Γ—2 Matrix{Any}:
 1  "some"
 2  "strings"
3 Likes

Thanks! this helped