# DataFrames type assignment inconsistency

**URL:** <https://discourse.julialang.org/t/dataframes-type-assignment-inconsistency/18030>\
**Category:** General Usage\
**Created:** [November 26, 2018, 7:59pm UTC](https://discourse.julialang.org/t/dataframes-type-assignment-inconsistency/18030 "2018-11-26T19:59:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [November 26, 2018, 7:59pm UTC](https://discourse.julialang.org/t/dataframes-type-assignment-inconsistency/18030/1 "2018-11-26T19:59:21Z")

</div>

I ran into this strange behavior and I’m not sure if it’s a problem with DataFrames or my interpretation with how it works.

When i generate my dataframe with an integer array and a float array, i get two float columns. But if i generate my dataframe with an integer array, float array and string array, i get an integer, float and string column.

Dataframes changes the type of data it assigned to the first column!

I’m still investigating, but I’m wondering if someone understands this behavior.

Thanks,

```julia
julia> x=[1,2,3,4,5]
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> df = DataFrame([x,[1.0,2.0,3.0,4.0,5.0]], [:A, :B])
5×2 DataFrame
¦ Row ¦ A ¦ B ¦
¦ ¦ Float64 ¦ Float64 ¦
+-----+---------+---------¦
¦ 1 ¦ 1.0 ¦ 1.0 ¦
¦ 2 ¦ 2.0 ¦ 2.0 ¦
¦ 3 ¦ 3.0 ¦ 3.0 ¦
¦ 4 ¦ 4.0 ¦ 4.0 ¦
¦ 5 ¦ 5.0 ¦ 5.0 ¦

df = DataFrame([x,[1.0,2.0,3.0,4.0,5.0],["1","2","3","4","5"]], [:A, :B, :C])
5×3 DataFrame
¦ Row ¦ A ¦ B ¦ C ¦
¦ ¦ Int64 ¦ Float64 ¦ String ¦
+-----+-------+---------+--------¦
¦ 1 ¦ 1 ¦ 1.0 ¦ 1 ¦
¦ 2 ¦ 2 ¦ 2.0 ¦ 2 ¦
¦ 3 ¦ 3 ¦ 3.0 ¦ 3 ¦
¦ 4 ¦ 4 ¦ 4.0 ¦ 4 ¦
¦ 5 ¦ 5 ¦ 5.0 ¦ 5 ¦

```

---

<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:** [November 26, 2018, 8:13pm UTC](https://discourse.julialang.org/t/dataframes-type-assignment-inconsistency/18030/2 "2018-11-26T20:13:04Z")

</div>

This is because your intermediate array `[x,[1.0,2.0,3.0,4.0,5.0]]` uses a Type promotion in its construction. Rather than `Vector{Union{Vector{Int64}, Vector{Float64}}` it just becomes `Vector{Vector{Float64}}`

However it can’t do that with a vector of strings, so it just reverts to `Vector{Vector{Any}}`.

`Any[x,[1.0,2.0,3.0,4.0,5.0]]` works, as does `Vector{<:Number}[x,[1.0,2.0,3.0,4.0,5.0]]`

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [November 26, 2018, 8:19pm UTC](https://discourse.julialang.org/t/dataframes-type-assignment-inconsistency/18030/3 "2018-11-26T20:19:54Z")

</div>

Thanks very much!

Is DataFrames attempting to avoid using Any for some (possibly related to efficiency) reason ?

---

<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:** [November 26, 2018, 8:23pm UTC](https://discourse.julialang.org/t/dataframes-type-assignment-inconsistency/18030/4 "2018-11-26T20:23:59Z")

</div>

No, this is outside of DataFrames. Julia evaluates the argument ` [x,[1.0,2.0,3.0,4.0,5.0]]` first, then DataFrames deals with it.

DataFrames has lots of constructors you can use, though. So if that one doesn’t suit your needs there are other ones that might be better suited.

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [November 26, 2018, 8:27pm UTC](https://discourse.julialang.org/t/dataframes-type-assignment-inconsistency/18030/5 "2018-11-26T20:27:20Z")

</div>

Thx again, that’s very helpful. I’ve just started using DataFrames and one of the things that I found a bit tricky is setting up a dataframe dynamically based on data I’m reading from a file, i.e. without knowing number of columns or column types ahead of time.

---

<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:** [November 26, 2018, 9:02pm UTC](https://discourse.julialang.org/t/dataframes-type-assignment-inconsistency/18030/6 "2018-11-26T21:02:02Z")

</div>

I would suggest making an empty DataFrame, `df = DataFrame()` and then adding columns iteratively with

```julia
df[var] = x

```
