# Converting Array of Named Tuples to DataFrames

**URL:** <https://discourse.julialang.org/t/converting-array-of-named-tuples-to-dataframes/19378>\
**Category:** General Usage\
**Created:** [January 8, 2019, 9:42am UTC](https://discourse.julialang.org/t/converting-array-of-named-tuples-to-dataframes/19378 "2019-01-08T09:42:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 8, 2019, 9:42am UTC](https://discourse.julialang.org/t/converting-array-of-named-tuples-to-dataframes/19378/1 "2019-01-08T09:42:52Z")

</div>

I have an array of NamedTuples that I populate in a for loop. Eventually, I want to convert it to a DataFrame. If I initialize the array as `NamedTuple[]`, the conversion fails. However, initializing as `Any[]` works, but I want to avoid `Any[]`. The code below provides a demonstration of this behavior. Why does it fail for `NamedTuple[]`, but not for `Any[]` and is this intended?

```julia
using DataFrames
#Works
x1 = [(x=rand(),y=rand([:a,:b])) for i in 1:10]
d1 = DataFrame(x1)

#Works
x2 = Any[(x=rand(),y=rand([:a,:b])) for i in 1:10]
d2 = DataFrame(x2)

#Does not work
x3 = NamedTuple[(x=rand(),y=rand([:a,:b])) for i in 1:10]
d3 = DataFrame(x3)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 8, 2019, 9:57am UTC](https://discourse.julialang.org/t/converting-array-of-named-tuples-to-dataframes/19378/2 "2019-01-08T09:57:29Z")

</div>

I think it falls through the cracks of the `Tables` interface:

```julia
julia> Tables.istable(typeof(x3))
true

```

but then the schema is not defined.

BTW, “fails” is not very informative, an error message is helpful:

```julia
julia> DataFrame(x3)
ERROR: MethodError: no method matching Tables.Schema(::Type{NamedTuple})
Closest candidates are:
  Tables.Schema(::Any, ::Nothing) at /home/tamas/.julia/packages/Tables/Icwxo/src/Tables.jl:156
  Tables.Schema(::Any, ::Any) at /home/tamas/.julia/packages/Tables/Icwxo/src/Tables.jl:157
  Tables.Schema(::Tuple{Vararg{Symbol,N} where N}, ::Type{T<:Tuple}) where T<:Tuple at /home/tamas/.julia/packages/Tables/Icwxo/src/Tables.jl:154
  ...
Stacktrace:
 [1] schema(::Tables.DataValueUnwrapper{Array{NamedTuple,1}}) at /home/tamas/.julia/packages/Tables/Icwxo/src/query.jl:29
 [2] columns at /home/tamas/.julia/packages/Tables/zyTIO/src/fallbacks.jl:152 [inlined]
 [3] DataFrame(::Array{NamedTuple,1}) at /home/tamas/.julia/packages/DataFrames/z2XOB/src/other/tables.jl:45
 [4] top-level scope at none:0

```

IMO a bug report for `DataFrames.jl` would be in order, but I am not sure what the best fix is.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 8, 2019, 10:14am UTC](https://discourse.julialang.org/t/converting-array-of-named-tuples-to-dataframes/19378/3 "2019-01-08T10:14:55Z")

</div>

Thanks for your explanation. I will submit an issue to DataFrames since it does not seem like desired behavior.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 8, 2019, 10:31am UTC](https://discourse.julialang.org/t/converting-array-of-named-tuples-to-dataframes/19378/4 "2019-01-08T10:31:21Z")

</div>

A link to the issue for future reference: [Converting to DataFrame fails for NamedTuple[] · Issue #1670 · JuliaData/DataFrames.jl · GitHub](https://github.com/JuliaData/DataFrames.jl/issues/1670)
