Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to display the days of the week for a particular year using Pandas?
Pandas is a powerful Python library for data manipulation and time-series analysis. When working with date data, you often need to find all occurrences of a specific weekday in a given year. Pandas provides the date_range() function to generate these dates efficiently.
Understanding date_range() Function
The pd.date_range() function creates a sequence of dates based on specified parameters. For weekly frequencies, it uses the format 'W-XXX' where XXX is the three-letter day abbreviation.
Syntax
range_of_dates = pd.date_range(start, periods, freq) result = pd.Series(range_of_dates)
Parameters
start ? The starting date of the range (e.g., '2023-01-01')
periods ? Number of dates to generate (typically 52 for weekly occurrences)
freq ? Frequency string like 'W-MON' for weekly Monday occurrences
Example 1: Finding All Mondays in 2023
This example generates all Monday dates for the year 2023 ?
import pandas as pd day = "monday" year = "2023" range_of_dates = pd.date_range(year + '-01-01', periods=52, freq='W-' + day[:3].upper()) result = pd.Series(range_of_dates) print(result)
0 2023-01-02 1 2023-01-09 2 2023-01-16 3 2023-01-23 4 2023-01-30 5 2023-02-06 6 2023-02-13 7 2023-02-20 8 2023-02-27 9 2023-03-06 10 2023-03-13 11 2023-03-20 12 2023-03-27 13 2023-04-03 14 2023-04-10 15 2023-04-17 16 2023-04-24 17 2023-05-01 18 2023-05-08 19 2023-05-15 20 2023-05-22 21 2023-05-29 22 2023-06-05 23 2023-06-12 24 2023-06-19 25 2023-06-26 26 2023-07-03 27 2023-07-10 28 2023-07-17 29 2023-07-24 30 2023-07-31 31 2023-08-07 32 2023-08-14 33 2023-08-21 34 2023-08-28 35 2023-09-04 36 2023-09-11 37 2023-09-18 38 2023-09-25 39 2023-10-02 40 2023-10-09 41 2023-10-16 42 2023-10-23 43 2023-10-30 44 2023-11-06 45 2023-11-13 46 2023-11-20 47 2023-11-27 48 2023-12-04 49 2023-12-11 50 2023-12-18 51 2023-12-25 dtype: datetime64[ns]
Example 2: Finding All Fridays in 2023
This example demonstrates how to find all Friday occurrences in 2023 ?
import pandas as pd day = "FRIDAY" year = "2023" range_of_dates = pd.date_range(year + '-01-01', periods=52, freq='W-' + day[:3].upper()) result = pd.Series(range_of_dates) print(result)
0 2023-01-06 1 2023-01-13 2 2023-01-20 3 2023-01-27 4 2023-02-03 5 2023-02-10 6 2023-02-17 7 2023-02-24 8 2023-03-03 9 2023-03-10 10 2023-03-17 11 2023-03-24 12 2023-03-31 13 2023-04-07 14 2023-04-14 15 2023-04-21 16 2023-04-28 17 2023-05-05 18 2023-05-12 19 2023-05-19 20 2023-05-26 21 2023-06-02 22 2023-06-09 23 2023-06-16 24 2023-06-23 25 2023-06-30 26 2023-07-07 27 2023-07-14 28 2023-07-21 29 2023-07-28 30 2023-08-04 31 2023-08-11 32 2023-08-18 33 2023-08-25 34 2023-09-01 35 2023-09-08 36 2023-09-15 37 2023-09-22 38 2023-09-29 39 2023-10-06 40 2023-10-13 41 2023-10-20 42 2023-10-27 43 2023-11-03 44 2023-11-10 45 2023-11-17 46 2023-11-24 47 2023-12-01 48 2023-12-08 49 2023-12-15 50 2023-12-22 51 2023-12-29 dtype: datetime64[ns]
How the Frequency Code Works
The frequency string 'W-XXX' tells Pandas to generate weekly dates ending on a specific weekday:
W-MON ? Weekly frequency ending on Monday
W-FRI ? Weekly frequency ending on Friday
W-SUN ? Weekly frequency ending on Sunday
Practical Applications
This technique is useful for:
Scheduling recurring events
Analyzing weekly patterns in data
Creating calendar-based reports
Time series analysis with weekly granularity
Conclusion
Pandas date_range() function makes it easy to generate all occurrences of a specific weekday in any given year. This powerful feature is essential for time-series analysis and date-based data manipulation tasks.
