# DataFrame Matrix Constructor without Copying

**URL:** <https://discourse.julialang.org/t/dataframe-matrix-constructor-without-copying/71916>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [November 22, 2021, 9:42pm UTC](https://discourse.julialang.org/t/dataframe-matrix-constructor-without-copying/71916 "2021-11-22T21:42:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [November 22, 2021, 9:42pm UTC](https://discourse.julialang.org/t/dataframe-matrix-constructor-without-copying/71916/1 "2021-11-22T21:42:43Z")

</div>

I’m curious why the following method for creating a DataFrame using an existing matrix doesn’t exist. In some ways it seems like the simplest way to create a DataFrame.

```julia
using DataFrames
mat = rand(10000,3)
df = DataFrame(mat, [:a, :b, :c], copycols=false)

```

I have a large matrix and I’d like to use a DataFrame to label and operate on the columns inplace - either to avoid an expensive copy or just to make modifying the original matrix more convenient through the DataFrame API.

Btw, the following workaround seems to save the copy, but I’m wondering if there’s something I’m missing:

```julia
matcols = collect(eachcol(mat))
df = DataFrame(matcols, [:a, :b, :c], copycols=false);

```

---

<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 23, 2021, 2:23am UTC](https://discourse.julialang.org/t/dataframe-matrix-constructor-without-copying/71916/2 "2021-11-23T02:23:44Z")

</div>

In Julia a `Matrix` is not just a vector of vectors. They have different memory layouts, which means you have to copy.

---

<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:** [November 23, 2021, 8:19am UTC](https://discourse.julialang.org/t/dataframe-matrix-constructor-without-copying/71916/3 "2021-11-23T08:19:03Z")

</div>

This is actually working in the development version, it will be released in DataFrames 1.3…

@pdeffebach I think the memory layout is not a problem? Since `Matrix` is column-major, it is straightforward to have vectors that are views of the same data as the matrix.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [November 23, 2021, 8:42am UTC](https://discourse.julialang.org/t/dataframe-matrix-constructor-without-copying/71916/4 "2021-11-23T08:42:29Z")

</div>

> [@bmit](#):
>
> `DataFrame(mat, [:a, :b, :c], copycols=false)`

This will work in DataFrames 1.3 as @sijo explained.

However, one has to bear in mind that such an approach ha a limitation that many standard functions like `push!` or `append!` will not work correctly with such a data frame.

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [November 23, 2021, 3:33pm UTC](https://discourse.julialang.org/t/dataframe-matrix-constructor-without-copying/71916/5 "2021-11-23T15:33:30Z")

</div>

This is great! Thanks @bkamins and @sijo. The memory issue didn’t seem like it would be a problem since a dense `Matrix` is just a more memory layout constrained vector of vectors.

`push!` and `append!` are the sort of gotchas I was trying to think of with my workaround above. Not that I would use them in my use case, but I see how that could create major headaches in general. Could those two particular functions dispatch to `vcat` for the `Matrix` case?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [November 23, 2021, 3:59pm UTC](https://discourse.julialang.org/t/dataframe-matrix-constructor-without-copying/71916/6 "2021-11-23T15:59:13Z")

</div>

> [@bmit](#):
>
> Could those two particular functions dispatch to `vcat` for the `Matrix` case?

No, as it would break the contract for `push!` and `append!` in Julia Base.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [November 23, 2021, 6:08pm UTC](https://discourse.julialang.org/t/dataframe-matrix-constructor-without-copying/71916/7 "2021-11-23T18:08:30Z")

</div>

Why wouldn’t `push!` and `append!` work properly?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [November 23, 2021, 6:21pm UTC](https://discourse.julialang.org/t/dataframe-matrix-constructor-without-copying/71916/8 "2021-11-23T18:21:45Z")

</div>

Because we would create a new vector and `push!` and `append!` are functions that update existing vectors in-place.
