Problem assigning type to DataFrame

The following code results in an error:

using MAT

directory = "C:/MyData/"
files = readdir(directory)

global tournaments =  Vector{String}(undef,10)

for i = 1:10
    event = matread(directory*files[i])
    tournaments[i] = event["tournament"][1]
end
infoType = Array{DataType,1}([String])
DataFrame([tournaments], types = infoType)

The error is the following:

MethodError: no method matching DataFrame(::Array{Array{String,1},1}, ::Array{Symbol,1}; types=DataType[String])

I am having problems understanding what is wrong. Does the error have to do with the .mat import?

The types keyword argument no longer exists, I think you have to do something else.

What is the DataFrame you want to create exactly?

The code is just a reduced version of something longer. So, the DataFrame I am trying to create is just an example to show the error produced.

The best way to createa a data frame with no rows, but specified types, is like this

julia> DataFrame(["a", "b"] .=> [Int[], Int[]])
0×2 DataFrame
2 Likes

Or maybe sligtly less allocating:

DataFrame(("a", "b") .=> (Int[], Int[]))
2 Likes

Or like this (if you know the names):

DataFrame(a=Int[], b=Int[])

or like this:

DataFrame([Int, Int], [:a, :b])

(this one is nice if you want to create n columns, you can use fill(Int, n) for the types).

4 Likes

I just realized that DataFrame([Int, Int], [:a, :b]) is deprecated, so here’s another way to define the columns programmatically:

names = [:a, :b]
types = [Int, String]

DataFrame(names .=> [T[] for T in types]);

# or shorter but more cryptic:
DataFrame(names .=> getindex.(types))
2 Likes