How to display the days of the week for a particular year using Pandas?


Pandas is an open-source Python library designed for data manipulation and analysis.

It provides powerful data structures like Series (1-dimensional labeled array) and DataFrame (2-dimensional labeled array) that can handle different types of data and operations, such as reading and writing data from/to various file formats, merging, filtering, aggregating, and pivoting data, as well as handling missing or duplicate data.

Pandas also supports time-series data and provides extensive data visualization capabilities. Its ease of use, versatility, and performance make it a popular choice among data scientists and analysts for exploratory data analysis, data cleaning, and feature engineering tasks.

As we know, there are 52 weeks in a year. Here in our case, we want to know what were the dates of a certain day of a week in a particular year. Pandas can be very much helpful for our problem, and they can provide us with all the dates with just a few lines of code.

Syntax

To display the days of the week for a particular year in Pandas, you need to follow the following syntax −

range_of_dates = pandas.date_range(start, periods, freq)
result = pandas.Series(range_of_dates)
print(result)

The syntax creates a range of dates using the pandas.date_range() function and creates a Pandas series using the resulting range of dates with the pandas.Series() function.

The pandas.date_range() function takes three arguments −

  • start  The start date of the range.

  • periods  The number of periods in the range.

  • freq  The frequency of the range.

The resulting range of dates is stored in the range_of_dates variable. The pandas.Series() function is then used to create a Pandas series called result using the range_of_dates variable.

Example

This code imports the Pandas library with an alias pd. The variables day and yy are defined as strings. The code then uses these variables to create a range of dates for 52 weeks, starting from January 1st of the year specified in the yy variable, with a frequency of every week on a specific day of the week specified in the day variable. Finally, we get all the dates when Monday occurs in the year 2023.

Specifically, the pd.date_range() function is used to create a range of dates with the start date set to yy+'-01-01', representing January 1st of the year specified in the yy variable. The periods argument is set to 52 to create a range of dates for 52 weeks. The freq argument is set to 'W-'+(day[:3]).lower(), where (day[:3]).lower() extracts the first three characters of the string in day and converts them to lowercase, and 'W-' represents a week ending on a specific day of the week, resulting in a weekly frequency on the specified day of the week.

The resulting range of dates is stored in the range_of_dates variable, which is then passed as an argument to the pd.Series() function to create a Pandas series called result. The result variable is then displayed in the output using the result variable name.

import pandas as pd
day = "monday"
yy = "2023"
range_of_dates = pd.date_range(yy+'-01-01', periods=52, freq=('W-'+(day[:3]).lower()))
result = pd.Series(range_of_dates)
print(result)

Output

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

This code is similar to the previous code, with some changes made to the variables day and yr. Specifically, the day is now set to "FRIDAY" in all uppercase, and the year is set to "2023".

The code also creates a range of dates starting from January 1st of the year specified in the yr variable, for a total of 52 periods, with a frequency of every week on a specific day of the week (in this case, Friday) using the pd.date_range() function. The frequency is specified using the string format 'W-'+(day[:3]).lower(), which extracts the first three characters of the string in a day, converts them to lowercase using the lower() method, and appends them to 'W-', indicating a weekly frequency on a specific day of the week.

The resulting range of dates is stored in the range_of_dates variable, which is then passed as an argument to the pd.Series() function to create a Pandas series called result. Finally, the result variable is displayed in the output.

import pandas as pd
day = "FRIDAY"
yr = "2023"
range_of_dates = pd.date_range(yr+'-01-01', periods=52, freq=('W-'+(day[:3]).lower()))
result = pd.Series(range_of_dates)
print(result)

Output

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]

Conclusion

We learned how to display the days of the week for a particular year using Pandas. Displaying the days of the week for a particular year using Pandas is a useful exercise that demonstrates how to generate a range of dates with a specific frequency and provides insight into patterns and trends over time. With its intuitive and flexible syntax, Pandas is an essential tool for any data analyst or scientist working with time-series data.

Updated on: 12-May-2023

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements