Extracting specific months of multiple years in DataFrames

Hello, I have a DataFrame with weather data for every day over a span of 10 years. I’m trying to calculate the monthly average temperature for the month of June of each year, so that I end up with 10 temperature values I can plot in a diagram. I have already converted the given date format into DateTime yyyy-mm-dd so that it is easier to work with.

I’ll try to replicate the code so far here:

using GLMakie
using DataFrames, CSV, Statistics
using Dates  

#generate example dataset (not sure if this is how it works)
dates = DateTime(2011-01-01):Day(1):DateTime(2021-12-31)
values = rand(temperature(dates)) .*5
df = DataFrame(date = dates, temperature = values)

#create year, month and day columns 
df.Year = Year.(df.date)
df.month = month.(df.month)
df.day = day.(df.day)

#I've managed to calculate the entirety of average temperatures over the 10 years as follows
Yrly_avg = combine(groupby(df, :Year)) do subdf
                 (; Year = first(subdf.Year),
                 AverageT = mean(skipmissing(subdf.temperatures)))
end 

#... but I don't know how to alter this expression (if possible) to achieve the results I want

And this is where I get stuck. I’m aware there must be some way to filter the data by a specific month but I’m not sure how. And how to later calculate the average value for every month separately I’m also confused on. Any help is greatly appreciated :slight_smile:

I’m away from my computer rn, but you could try this:

  1. Group by year and month.
  2. Within each group, compute the average. You’ll end up with (year, month, average_temp) triplets. This will waste some resources computing averages for every single month, but that shouldn’t be too wasteful because there doesn’t seem to be that much data anyway.
  3. Filter/subset the result to extract only rows where month is June.

Or, in reverse order: filter out rows where month is not June, then group by year and month and compute within-group averages.