Python Pandas: How to Get All Sundays of a Year Using Pandas
Finding all occurrences of a specific weekday within a year is a common task in scheduling, reporting, and time-series analysis. Whether you need to count business days, plan weekly events, or build a calendar-based dataset, Pandas' date_range() function handles the calendar logic automatically with weekly frequency codes. It correctly accounts for years that have 52 versus 53 occurrences of a given weekday, so you never have to worry about edge cases.
In this guide, you will learn how to generate all Sundays (or any other weekday) in a year, format the output, and apply the results in practical scenarios.
Basic Usage with date_range()
Use the freq='W-SUN' frequency code to generate all Sundays within a date range:
import pandas as pd
sundays = pd.date_range(start='2024-01-01', end='2024-12-31', freq='W-SUN')
print(f"Number of Sundays in 2024: {len(sundays)}")
print(sundays[:5])
Output:
Number of Sundays in 2024: 52
DatetimeIndex(['2024-01-07', '2024-01-14', '2024-01-21', '2024-01-28',
'2024-02-04'],
dtype='datetime64[ns]', freq='W-SUN')
The first Sunday of 2024 falls on January 7th, and Pandas generates every subsequent Sunday through the end of the year.
Frequency Codes for Any Weekday
The W- prefix followed by a three-letter day abbreviation lets you target any day of the week:
| Code | Day |
|---|---|
W-SUN | Sunday |
W-MON | Monday |
W-TUE | Tuesday |
W-WED | Wednesday |
W-THU | Thursday |
W-FRI | Friday |
W-SAT | Saturday |
import pandas as pd
# All Fridays in 2024
fridays = pd.date_range('2024-01-01', '2024-12-31', freq='W-FRI')
print(f"Fridays in 2024: {len(fridays)}")
# All Mondays in 2024
mondays = pd.date_range('2024-01-01', '2024-12-31', freq='W-MON')
print(f"Mondays in 2024: {len(mondays)}")
Output:
Fridays in 2024: 52
Mondays in 2024: 53
Why Use start/end Instead of periods
Some years have 53 occurrences of a particular weekday while others have 52. Using date boundaries ensures you always get the correct count:
import pandas as pd
# 2023 has 53 Sundays
sundays_2023 = pd.date_range('2023-01-01', '2023-12-31', freq='W-SUN')
print(f"Sundays in 2023: {len(sundays_2023)}")
# 2024 has 52 Sundays
sundays_2024 = pd.date_range('2024-01-01', '2024-12-31', freq='W-SUN')
print(f"Sundays in 2024: {len(sundays_2024)}")
Output:
Sundays in 2023: 53
Sundays in 2024: 52
Avoid using periods=52 to generate a year's worth of weekdays. In years with 53 occurrences, you would miss the last one. Specifying start and end dates lets Pandas determine the correct count automatically.
Formatting the Output
Convert the DatetimeIndex to formatted strings for display or export:
import pandas as pd
sundays = pd.date_range('2024-01-01', '2024-12-31', freq='W-SUN')
# ISO format
iso_dates = sundays.strftime('%Y-%m-%d').tolist()
print(iso_dates[:3])
# Human-readable format
readable = sundays.strftime('%A, %B %d, %Y').tolist()
print(readable[0])
Output:
['2024-01-07', '2024-01-14', '2024-01-21']
Sunday, January 07, 2024
Practical Examples
Building a Sunday Calendar DataFrame
import pandas as pd
sundays = pd.date_range('2024-01-01', '2024-12-31', freq='W-SUN')
df = pd.DataFrame({
'Date': sundays,
'Week_Number': sundays.isocalendar().week,
'Month': sundays.month_name()
})
print(df.head())
Output:
Date Week_Number Month
2024-01-07 2024-01-07 1 January
2024-01-14 2024-01-14 2 January
2024-01-21 2024-01-21 3 January
2024-01-28 2024-01-28 4 January
2024-02-04 2024-02-04 5 February
Counting Sundays per Month
import pandas as pd
sundays = pd.date_range('2024-01-01', '2024-12-31', freq='W-SUN')
monthly_counts = sundays.to_series().groupby(sundays.month_name()).count()
print(monthly_counts)
Output:
April 4
August 4
December 5
February 4
January 4
July 4
June 5
March 5
May 4
November 4
October 4
September 5
dtype: int64
Filtering to a Specific Month
import pandas as pd
sundays = pd.date_range('2024-01-01', '2024-12-31', freq='W-SUN')
# Sundays in December only
december_sundays = sundays[sundays.month == 12]
for d in december_sundays:
print(d.strftime('%B %d, %Y'))
Output:
December 01, 2024
December 08, 2024
December 15, 2024
December 22, 2024
December 29, 2024
Reusable Function for Any Weekday
import pandas as pd
def get_weekdays_in_year(year, day_code='W-SUN'):
"""Get all occurrences of a specific weekday in a given year."""
return pd.date_range(
start=f'{year}-01-01',
end=f'{year}-12-31',
freq=day_code
)
# All Wednesdays in 2025
wednesdays = get_weekdays_in_year(2025, 'W-WED')
print(f"Wednesdays in 2025: {len(wednesdays)}")
print(wednesdays[:3].strftime('%Y-%m-%d').tolist())
Output:
Wednesdays in 2025: 53
['2025-01-01', '2025-01-08', '2025-01-15']
Quick Reference
| Goal | Code |
|---|---|
| All Sundays | pd.date_range(start, end, freq='W-SUN') |
| All Mondays | pd.date_range(start, end, freq='W-MON') |
| Format as strings | .strftime('%Y-%m-%d') |
| Convert to list | .tolist() |
| Count occurrences | len(result) |
| Filter by month | result[result.month == 12] |
Use pd.date_range(start='YYYY-01-01', end='YYYY-12-31', freq='W-SUN') to get all Sundays in a year.
The W- frequency pattern works for any weekday by appending the three-letter abbreviation.
Always use date boundaries (start and end) rather than periods to correctly handle years with varying numbers of a given weekday.