# Extracting specific months of multiple years in DataFrames

**URL:** <https://discourse.julialang.org/t/extracting-specific-months-of-multiple-years-in-dataframes/138707>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 9, 2026, 1:49pm UTC](https://discourse.julialang.org/t/extracting-specific-months-of-multiple-years-in-dataframes/138707 "2026-08-09T13:49:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![omgplshelp](https://avatars.discourse-cdn.com/v4/letter/o/e9c0ed/32.png) [@omgplshelp](https://discourse.julialang.org/u/omgplshelp)\
**Post date:** [August 9, 2026, 1:49pm UTC](https://discourse.julialang.org/t/extracting-specific-months-of-multiple-years-in-dataframes/138707/1 "2026-08-09T13:49:42Z")

</div>

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:

```julia-auto
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 🙂

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [August 9, 2026, 2:06pm UTC](https://discourse.julialang.org/t/extracting-specific-months-of-multiple-years-in-dataframes/138707/2 "2026-08-09T14:06:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)\
**Post date:** [August 9, 2026, 7:01pm UTC](https://discourse.julialang.org/t/extracting-specific-months-of-multiple-years-in-dataframes/138707/3 "2026-08-09T19:01:41Z")

</div>

This will get you what you want:

```julia
june_df = filter(:time => d -> month(d) == Dates.June, df)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 9, 2026, 8:23pm UTC](https://discourse.julialang.org/t/extracting-specific-months-of-multiple-years-in-dataframes/138707/4 "2026-08-09T20:23:41Z")

</div>

Once you have created the `year`and `month` columns:

```julia-auto
df.year = year.(df.date)
df.month = month.(df.date)

```

You can then use combine as follows:

```julia-auto
dg = combine(groupby(df, [:year, :month]), :temperature => mean => :avg_temp)

```

Then filter any month:

```julia-auto
filter(r -> r.month == 6, dg)

```

---

<div class="post-metadata">

**Author:** ![omgplshelp](https://avatars.discourse-cdn.com/v4/letter/o/e9c0ed/32.png) [@omgplshelp](https://discourse.julialang.org/u/omgplshelp)\
**Post date:** [August 10, 2026, 1:16pm UTC](https://discourse.julialang.org/t/extracting-specific-months-of-multiple-years-in-dataframes/138707/5 "2026-08-10T13:16:33Z")

</div>

@rafael.guerra thank you so much, that gave me the exact results I needed!
