# Broadcasting: Difference between = and .= for DataFrames

**URL:** <https://discourse.julialang.org/t/broadcasting-difference-between-and-for-dataframes/14955>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [September 14, 2018, 1:43pm UTC](https://discourse.julialang.org/t/broadcasting-difference-between-and-for-dataframes/14955 "2018-09-14T13:43:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [September 14, 2018, 1:43pm UTC](https://discourse.julialang.org/t/broadcasting-difference-between-and-for-dataframes/14955/1 "2018-09-14T13:43:17Z")

</div>

Hi,  
I wanted to change some rows of a DataFrame with a similar code to the following:

```julia
df = DataFrame([1:5,6:10])
df[df[:x1] .< 3 ,:x2] .= 99
julia> df
5×2 DataFrame
│ Row │ x1 │ x2 │
├─────┼────┼────┤
│ 1 │ 1 │ 6 │
│ 2 │ 2 │ 7 │
│ 3 │ 3 │ 8 │
│ 4 │ 4 │ 9 │
│ 5 │ 5 │ 10 │

```

But this did not change the value of the DataFrame. I had to remove the broadcasting operator for the assignment to change it.

```julia
df[df[:x1] .< 3 ,:x2] = 99
julia> df
5×2 DataFrame
│ Row │ x1 │ x2 │
├─────┼────┼────┤
│ 1 │ 1 │ 99 │
│ 2 │ 2 │ 99 │
│ 3 │ 3 │ 8 │
│ 4 │ 4 │ 9 │
│ 5 │ 5 │ 10 │

```

Why does the addition of the broadcast operator change the behavior? Because for Arrays, only the broadcasted assignments is not deprecated?

Thanks.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 14, 2018, 2:17pm UTC](https://discourse.julialang.org/t/broadcasting-difference-between-and-for-dataframes/14955/2 "2018-09-14T14:17:00Z")

</div>

> [@uwechsler](#):
>
> Why does the addition of the broadcast operator change the behavior? Because for Arrays, only the broadcasted assignments is not deprecated?

`df[indices] .= y` effectively calls `broadcast!(identity, df[indices], y)`: that is, the broadcast operation (`.=`) acts on `df[indices]`, which is a _copy_ of the dataframes data rather than the original data.

Technically, the way to fix this is to define a [`Base.dotview` function for `DataFrame`](https://github.com/JuliaLang/julia/blob/06dd840d1b704f3cd1da93d518d0d42e3483d547/base/broadcast.jl#L1060-L1066).

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [September 14, 2018, 2:35pm UTC](https://discourse.julialang.org/t/broadcasting-difference-between-and-for-dataframes/14955/3 "2018-09-14T14:35:30Z")

</div>

I’d vote that this should probably behave as @uwechsler originally expected. Opened [issue](https://github.com/JuliaData/DataFrames.jl/issues/1507).

---

<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 14, 2018, 2:44pm UTC](https://discourse.julialang.org/t/broadcasting-difference-between-and-for-dataframes/14955/4 "2018-09-14T14:44:53Z")

</div>

Kind of similar, because DataFramesMeta works with keyword arguments, there are some inconsistencies with the way `.=` works.

```julia
 @transform(df, x1 .= 5)

```

and

```julia
 @transform(df, x1 = 5)

```

have the same behavior in DataFramesMeta.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [September 14, 2018, 3:01pm UTC](https://discourse.julialang.org/t/broadcasting-difference-between-and-for-dataframes/14955/5 "2018-09-14T15:01:49Z")

</div>

I might even go as far as to say that `df[idx, n] .= x` shoulld work but `df[idx, n] = x` should throw an _error_. That would be consistent with the behavior of `AbstractArray`.
