Calculate deseasonalized anomalies

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 GroupedDataFrames is reserved
So, which is the Julia’s easy way (similar to python) to find this ?

Thanks a lot!!

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)

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

Welcome to the Julia community!
If you didn’t want to keep the climatology, you could also mimick the transform function in Pandas.

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

?transform for more info.

That’s interesting, Thank you!