# Problem assigning type to DataFrame

**URL:** <https://discourse.julialang.org/t/problem-assigning-type-to-dataframe/57966>\
**Category:** New to Julia\
**Created:** [March 25, 2021, 7:18pm UTC](https://discourse.julialang.org/t/problem-assigning-type-to-dataframe/57966 "2021-03-25T19:18:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [March 25, 2021, 7:18pm UTC](https://discourse.julialang.org/t/problem-assigning-type-to-dataframe/57966/1 "2021-03-25T19:18:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 25, 2021, 7:28pm UTC](https://discourse.julialang.org/t/problem-assigning-type-to-dataframe/57966/2 "2021-03-25T19:28:51Z")

</div>

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

What is the `DataFrame` you want to create exactly?

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [March 25, 2021, 7:31pm UTC](https://discourse.julialang.org/t/problem-assigning-type-to-dataframe/57966/3 "2021-03-25T19:31:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 25, 2021, 7:40pm UTC](https://discourse.julialang.org/t/problem-assigning-type-to-dataframe/57966/4 "2021-03-25T19:40:38Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 25, 2021, 8:03pm UTC](https://discourse.julialang.org/t/problem-assigning-type-to-dataframe/57966/5 "2021-03-25T20:03:52Z")

</div>

> [@pdeffebach](#):
>
> ```julia
> DataFrame(["a", "b"] .=> [Int[], Int[]])
> 
> ```

Or maybe sligtly less allocating:

```julia
DataFrame(("a", "b") .=> (Int[], Int[]))

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 25, 2021, 10:07pm UTC](https://discourse.julialang.org/t/problem-assigning-type-to-dataframe/57966/6 "2021-03-25T22:07:42Z")

</div>

Or like this (if you know the names):

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

```

or like this:

```julia
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).

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 29, 2021, 9:19am UTC](https://discourse.julialang.org/t/problem-assigning-type-to-dataframe/57966/7 "2021-03-29T09:19:29Z")

</div>

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

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

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

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

```
