# Various constructors and equality for DataFrame

**URL:** https://discourse.julialang.org/t/various-constructors-and-equality-for-dataframe/1551
**Category:** Data
**Tags:** question
**Created:** [January 17, 2017, 8:44pm UTC](https://discourse.julialang.org/t/various-constructors-and-equality-for-dataframe/1551 "2017-01-17T20:44:54Z")
**Posts on this page:** 6
**Page:** 1

<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 17, 2017, 8:44pm UTC](https://discourse.julialang.org/t/various-constructors-and-equality-for-dataframe/1551/1 "2017-01-17T20:44:54Z")

</div>

I came across this problem when writing tests for a package, for a function that produces dataframes using the

```julia
DataFrames(columns::AbstractArray{T<:Any,1}, cnames::AbstractArray{Symbol,1})

```

constructor. Consider this MWE:

```julia
using DataFrames
a = collect(1:5)
b = string.(a)
df1 = DataFrame([a,b], [:a,:b])
df2 = DataFrame(a=a,b=b)

```

First, `df1` prints funny:

```julia
julia> df1
5×2 DataFrames.DataFrame
│ Row │ a │ b │
├─────┼───┼─────┤
│ 1 │ 1 │ 1 │
│ 2 │ 2 │ 2 │
│ 3 │ 3 │ 3 │
│ 4 │ 4 │ 4 │
│ 5 │ 5 │ 5 │

```

Second,

```julia
julia> df1 == df2
false

```

I think the issue is that one has `NullableArray`s, the other plain vanilla `Array`s. So should I **not** be using the constructor above? Or always make the arguments `NullableArray`? Or use a different function for comparison if I want equality?

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [January 17, 2017, 9:24pm UTC](https://discourse.julialang.org/t/various-constructors-and-equality-for-dataframe/1551/2 "2017-01-17T21:24:05Z")

</div>

If you use the first form of the constructor, you’re responsible for choosing the appropriate column type. This is likely to change at some point though, in which case you’ll get a standard `Array` in both cases (see [this issue](https://github.com/JuliaStats/DataFrames.jl/issues/1119)).

The printing issue was [just fixed](https://github.com/JuliaStats/DataFrames.jl/pull/1145).

---

<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 18, 2017, 10:15am UTC](https://discourse.julialang.org/t/various-constructors-and-equality-for-dataframe/1551/3 "2017-01-18T10:15:36Z")

</div>

Thank you — I read the issues, but I am still not sure what the “appropriate column type” is until #1119 is resolved. Would using the constructor as

```julia
DataFrame(map(NullableArray, [a,b]), [:a,:b])

```

be the recommended solution for now?

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [January 18, 2017, 1:24pm UTC](https://discourse.julialang.org/t/various-constructors-and-equality-for-dataframe/1551/4 "2017-01-18T13:24:01Z")

</div>

The most appropriate/default column type is `DataArray` for DataFrames 0.8.x and `NullableArray` for git master. If you just want to use this type, then use the keyword argument constructor. The other one is only useful when you really want to preserve the original type, which IIUC isn’t your case at all.

We also need to solve the question of whether `==` and `isequal` should consider `NullableArray` and `Array` equal when they have the same contents. See [this issue](https://github.com/JuliaStats/NullableArrays.jl/issues/82) and [this one](https://github.com/JuliaStats/NullableArrays.jl/issues/85).

---

<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 18, 2017, 1:44pm UTC](https://discourse.julialang.org/t/various-constructors-and-equality-for-dataframe/1551/5 "2017-01-18T13:44:12Z")

</div>

Thanks! So if the column names and values are the result of some other computation, and the names their number is not known in advance, is the recommended constructor something like

```julia
DataFrame(; [Pair(key_column...) for key_column in zip(keys,columns)]...)

```

?

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [January 18, 2017, 2:20pm UTC](https://discourse.julialang.org/t/various-constructors-and-equality-for-dataframe/1551/6 "2017-01-18T14:20:19Z")

</div>

Or even just `DataFrame(; zip(keys, columns)...)`.
