# Does indexing over a subset of rows in a DataFrame return a view or a copy?

**URL:** <https://discourse.julialang.org/t/does-indexing-over-a-subset-of-rows-in-a-dataframe-return-a-view-or-a-copy/46109>\
**Category:** New to Julia\
**Created:** [September 5, 2020, 4:03pm UTC](https://discourse.julialang.org/t/does-indexing-over-a-subset-of-rows-in-a-dataframe-return-a-view-or-a-copy/46109 "2020-09-05T16:03:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![conditionality](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@conditionality](https://discourse.julialang.org/u/conditionality)\
**Post date:** [September 5, 2020, 4:03pm UTC](https://discourse.julialang.org/t/does-indexing-over-a-subset-of-rows-in-a-dataframe-return-a-view-or-a-copy/46109/1 "2020-09-05T16:03:42Z")

</div>

According to the manual, [:, ColumnName] returns a read-only view of the columns. Does it also work when indexing over a subset of the rows [my\_rows\_of\_interest, :]? Does it matter if my\_rows\_of\_interest is contiguous or not?

---

<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:** [September 5, 2020, 5:57pm UTC](https://discourse.julialang.org/t/does-indexing-over-a-subset-of-rows-in-a-dataframe-return-a-view-or-a-copy/46109/2 "2020-09-05T17:57:35Z")

</div>

A copy.

You can do

```julia
@view df[my_rows_of_interest, :]

```

for a `view`.

---

<div class="post-metadata">

**Author:** ![conditionality](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@conditionality](https://discourse.julialang.org/u/conditionality)\
**Post date:** [September 5, 2020, 6:47pm UTC](https://discourse.julialang.org/t/does-indexing-over-a-subset-of-rows-in-a-dataframe-return-a-view-or-a-copy/46109/3 "2020-09-05T18:47:44Z")

</div>

Thanks! If I were to do

```julia
return @view cached_df |> @filter(_.date >= from && _.date <= to)

```

in a function (referring to a global df “cached\_df”), would it still be a view that is returned and used by the callee?

---

<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:** [September 5, 2020, 6:53pm UTC](https://discourse.julialang.org/t/does-indexing-over-a-subset-of-rows-in-a-dataframe-return-a-view-or-a-copy/46109/4 "2020-09-05T18:53:01Z")

</div>

No that isn’t feasible.`@view` isn’t that smart.

There is a PR for this [here](https://github.com/JuliaData/DataFrames.jl/pull/2386). It would have a slightly different syntax.

Note that even after that PR you would only do `view` for DataFrames functions, not those defined in Query.

---

<div class="post-metadata">

**Author:** ![conditionality](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@conditionality](https://discourse.julialang.org/u/conditionality)\
**Post date:** [September 5, 2020, 7:11pm UTC](https://discourse.julialang.org/t/does-indexing-over-a-subset-of-rows-in-a-dataframe-return-a-view-or-a-copy/46109/5 "2020-09-05T19:11:55Z")

</div>

Given that cached\_df is sorted w.r.t. :date, if I were to find the row numbers (which is a contiguous interval, because of the sorting) for which from \<= :date \<= to is satisfied, and instead write

```julia
return @view cached_df[row_start:row_end, :]

```

would that return a view to the callee? This link ([Indexing · DataFrames.jl](https://juliadata.github.io/DataFrames.jl/stable/lib/indexing/)), and also your initial response, seems to imply that, I just want to be sure that “return” does not do anything that changes that fact.

---

<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:** [September 5, 2020, 7:22pm UTC](https://discourse.julialang.org/t/does-indexing-over-a-subset-of-rows-in-a-dataframe-return-a-view-or-a-copy/46109/6 "2020-09-05T19:22:41Z")

</div>

Correct. You will get a `SubDataFrame` back from the function. `return` doesn’t do anything special.
