# View and @view in DataFrames

**URL:** https://discourse.julialang.org/t/view-and-view-in-dataframes/76645
**Category:** Data
**Tags:** dataframes, views
**Created:** [February 17, 2022, 6:45pm UTC](https://discourse.julialang.org/t/view-and-view-in-dataframes/76645 "2022-02-17T18:45:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)
#### Post date: [February 17, 2022, 6:45pm UTC](https://discourse.julialang.org/t/view-and-view-in-dataframes/76645/1 "2022-02-17T18:45:13Z")

</div>

I was surprised that `view` and `@view` work differently when we work with subsets of the data.  
The following does not modify the parent dataframe

```julia
df = DataFrame(a = [0, 0, 0, 0], b = [1, 1, -1, missing], c = [1, -1, missing, 1])

dfv = dropmissing(df, [:b,:c], view = true)
dfv = view(dfv[dfv.b.>0, :], :, :)
dfv[1, 1] = 100 #it does not change df

display(df)
4×3 DataFrame
 Row │ a b c       
     │ Int64 Int64? Int64?  
─────┼─────────────────────────
   1 │ 0 1 1
   2 │ 0 1 -1
   3 │ 0 -1 missing 
   4 │ 0 missing 1

```

But the following does change the parent DataFrame

```julia
df = DataFrame(a = [0, 0, 0, 0], b = [1, 1, -1, missing], c = [1, -1, missing, 1])

dfv = dropmissing(df, [:b,:c], view = true)
dfv = @view dfv[dfv.b.>0, :]
dfv[1, 1] = 100 #it changes df

display(df)
4×3 DataFrame
 Row │ a b c       
     │ Int64 Int64? Int64?  
─────┼─────────────────────────
   1 │ 100 1 1
   2 │ 0 1 -1
   3 │ 0 -1 missing 
   4 │ 0 missing 1

```

I know that this could be done in other ways (e.g., with `subset` and `skipmissing=true`), but I was wondering if this is expected behavior.

---

<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: [February 17, 2022, 6:48pm UTC](https://discourse.julialang.org/t/view-and-view-in-dataframes/76645/2 "2022-02-17T18:48:28Z")

</div>

> [@alfaromartino](#):
>
> `dfv = view(dfv[dfv.b.>0, :], :, :)`

This is expected behavior as in this expression `dfv[dfv.b.>0, :]` makes a copy first, of which you make a view.

While `@view dfv[dfv.b.>0, :]` does not make a copy, but just makes a view.

`dfv = view(dfv[dfv.b.>0, :], :, :)` should be `dfv = view(view(dfv,dfv.b.>0, :), :, :)` to make a view (actually then you can drop outer `view` call)

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 17, 2022, 9:20pm UTC](https://discourse.julialang.org/t/view-and-view-in-dataframes/76645/3 "2022-02-17T21:20:59Z")

</div>

To see:

```julia
julia> @macroexpand(@view df[x1 .> 0.1, :])
:(true && (view)(df, x1 .> 0.1, :))

```
