# Calculate deseasonalized anomalies

**URL:** https://discourse.julialang.org/t/calculate-deseasonalized-anomalies/58753
**Category:** General Usage
**Tags:** question, scientific-computing
**Created:** [April 7, 2021, 1:47pm UTC](https://discourse.julialang.org/t/calculate-deseasonalized-anomalies/58753 "2021-04-07T13:47:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mixstam1453](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mixstam1453/32/23731_2.png) [@mixstam1453](https://discourse.julialang.org/u/mixstam1453)
#### Post date: [April 7, 2021, 1:47pm UTC](https://discourse.julialang.org/t/calculate-deseasonalized-anomalies/58753/1 "2021-04-07T13:47:54Z")

</div>

Hi to everyone!  
I am new in Julia and i have monthly values of a climate variable. I calculate the annual cycle but i can’t find the way to compute the anomalies. Here is the part of the code:

using DataFrames ; using CSV ; using Statistics  
tmp= CSV.read(“/home/michael/tmp.txt”,DataFrame)

monthly=combine(groupby(dropmissing(tmp),[:year,:month]), :tmpr =\> mean)

456×3 DataFrame  
Row │ year month tmpr\_mean  
│ Int64 Int64 Float64  
─────┼─────────────────────────  
1 │ 1980 1 78.1718  
2 │ 1980 2 110.061  
3 │ 1980 3 144.611  
…  
454 │ 2017 11 76.494  
455 │ 2017 12 59.1205  
456 │ 2018 1 79.8317

climatology=combine(groupby(dropmissing(tmp),:month), :tmpr=\>mean) #i.e. annual cycle

12×2 DataFrame  
Row │ month tmpr\_mean  
│ Int64 Float64  
─────┼──────────────────  
1 │ 1 75.6246  
2 │ 2 104.963  
3 │ 3 145.721  
…  
But trying to compute the deseasonalized anomalies (thinking similar to Python’s way)  
julia\> groupby(monthly,:month) .- climatology  
i take this error  
ERROR: ArgumentError: broadcasting over `GroupedDataFrame`s is reserved  
So, which is the Julia’s easy way (similar to python) to find this ?

Thanks a lot!!

---

<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: [April 7, 2021, 2:42pm UTC](https://discourse.julialang.org/t/calculate-deseasonalized-anomalies/58753/2 "2021-04-07T14:42:54Z")

</div>

I think you probably have to `leftjoin(monthly, climatology, on = :month, makeunique = true)` first and then subtract the two columns in the joined DataFrame, then possible group again by month (I’m not 100% sure what you would expect that last line to do)

---

<div class="post-metadata">

### Author: ![mixstam1453](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mixstam1453/32/23731_2.png) [@mixstam1453](https://discourse.julialang.org/u/mixstam1453)
#### Post date: [April 7, 2021, 3:35pm UTC](https://discourse.julialang.org/t/calculate-deseasonalized-anomalies/58753/3 "2021-04-07T15:35:16Z")

</div>

Thank you, that worked! I wanted the monthly anomalies so i don’t need to group again.  
Cheers,  
Michael

---

<div class="post-metadata">

### Author: ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)
#### Post date: [April 7, 2021, 5:56pm UTC](https://discourse.julialang.org/t/calculate-deseasonalized-anomalies/58753/4 "2021-04-07T17:56:56Z")

</div>

Welcome to the Julia community!  
If you didn’t want to keep the climatology, you could also mimick the transform function in [Pandas](https://stackoverflow.com/a/66086241).

```julia
transform!(groupby(df, :month), :tmpr=>(x->x.-mean(x))=>:tmpranom)

```

`?transform` for more info.

---

<div class="post-metadata">

### Author: ![mixstam1453](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mixstam1453/32/23731_2.png) [@mixstam1453](https://discourse.julialang.org/u/mixstam1453)
#### Post date: [April 8, 2021, 6:47am UTC](https://discourse.julialang.org/t/calculate-deseasonalized-anomalies/58753/5 "2021-04-08T06:47:00Z")

</div>

That’s interesting, Thank you!
