Display all the Sundays of a given year using Pandas in Python


Pandas is a powerful Python library for data manipulation and analysis. One of the key features of Pandas is its ability to handle date and time data effectively. In this article, we will show you how to use Pandas to display all the Sundays of a given year.

In this article, we will explore how to use Pandas, a popular data manipulation library in Python, to display all the Sundays of a given year. We will go through the step-by-step process to extract the Sundays of a year and display them in a readable format.

Prerequisites

Before we start, make sure you have Pandas installed on your machine. You can install it by running the following command in your terminal −

pip install pandas
Getting Started

Using Pandas in Python

To begin, we will start by importing the Pandas library and creating a Pandas DataFrame to hold the dates of the year. We will use the date_range function to generate a range of dates for the year. Here's the code to generate a range of dates for the year 2023 −

import pandas as pd
year = 2023
start_date = pd.to_datetime(f'{year}-01-01')
end_date = pd.to_datetime(f'{year}-12-31')
dates = pd.date_range(start_date, end_date)

We have created a start_date and an end_date object using the pd.to_datetime function. The dates variable is created using the pd.date_range function, which generates a range of dates from the start_date to the end_date.

Extracting Sundays

To extract the Sundays from the range of dates, we will use the dt accessor provided by Pandas. The dt accessor provides various methods to manipulate the date and time values of a Pandas DataFrame. We will use the day_name method of the dt accessor to get the name of the day for each date in the dates DataFrame. Here's the code to extract the Sundays −

sundays = dates[dates.dt.day_name() == 'Sunday']

The dates.dt.day_name() method returns the name of the day for each date in the dates DataFrame. We then filter the dates DataFrame to get only the rows where the day name is 'Sunday'.

Displaying Sundays

To display the Sundays in a readable format, we will use the strftime method of the dt accessor. The strftime method is used to format the date and time values of a Pandas DataFrame. Here's the code to display the Sundays −

for sunday in sundays:
   print(sunday.strftime('%Y-%m-%d'))

The strftime('%Y-%m-%d') method formats the date in the YYYY-MM-DD format. We then loop through the sundays DataFrame and print each Sunday in the desired format.

Final Code

Here's the complete code to display all the Sundays of the year 2023 −

import pandas as pd
year = 2023
start_date = pd.to_datetime(f'{year}-01-01')
end_date = pd.to_datetime(f'{year}-12-31')
dates = pd.date_range(start_date, end_date)
sundays = dates[dates.dt.day_name() == 'Sunday']
for sunday in sundays:
   print(sunday.strftime('%Y-%m-%d'))

Output

DatetimeIndex(['2023-01-01', '2023-01-08', '2023-01-15', '2023-01-22',
               '2023-01-29', '2023-02-05', '2023-02-12', '2023-02-19',
               '2023-02-26', '2023-03-05', '2023-03-12', '2023-03-19',
               '2023-03-26', '2023-04-02', '2023-04-09', '2023-04-16',
               '2023-04-23', '2023-04-30', '2023-05-07', '2023-05-14',
               '2023-05-21', '2023-05-28', '2023-06-04', '2023-06-11',
               '2023-06-18', '2023-06-25', '2023-07-02', '2023-07-09',
               '2023-07-16', '2023-07-23', '2023-07-30', '2023-08-06',
               '2023-08-13', '2023-08-20', '2023-08-27', '2023-09-03',
               '2023-09-10', '2023-09-17', '2023-09-24', '2023-10-01',
               '2023-10-08', '2023-10-15', '2023-10-22', '2023-10-29',
               '2023-11-05', '2023-11-12', '2023-11-19', '2023-11-26',
               '2023-12-03', '2023-12-10', '2023-12-17', '2023-12-24',
               '2023-12-31'],
              dtype='datetime64[ns]', freq=None)>

Display all the Sundays of a given year using Pandas

To display all the Sundays of a given year, we first need to create a Pandas DataFrame with a date range that spans the entire year. We can then filter this DataFrame to only include Sundays.

Here's the Python code to accomplish this task. In here. Let's break down the code step-by-step −

  • We import the Pandas library using the import statement.

  • We create a date range that spans the entire year using the pd.date_range() function. We specify the start and end dates using the start and end arguments, respectively. We replace '2022' with the desired year.

  • We filter the date range to only include Sundays by using the .weekday attribute of the date range, which returns the day of the week as an integer (Monday = 0, Tuesday = 1, etc.). Sundays are represented by the integer 6.

  • We store the filtered date range in a variable called sundays.

  • Finally, we print the list of Sundays by calling the print() function on the sundays variable.

import pandas as pd
# Replace '2022' with the desired year
date_range = pd.date_range(start='1/1/2022', end='12/31/2022')
# Filter the date range to only include Sundays
sundays = date_range[date_range.weekday == 6]
# Print the list of Sundays
print(sundays)

Output

When you run the above code, you should see a list of all the Sundays in the given year −

DatetimeIndex(['2022-01-02', '2022-01-09', '2022-01-16', '2022-01-23',
               '2022-01-30', '2022-02-06', '2022-02-13', '2022-02-20',
               '2022-02-27', '2022-03-06', '2022-03-13', '2022-03-20',
               '2022-03-27', '2022-04-03', '2022-04-10', '2022-04-17',
               '2022-04-24', '2022-05-01', '2022-05-08', '2022-05-15',
               '2022-05-22', '2022-05-29', '2022-06-05', '2022-06-12',
               '2022-06-19', '2022-06-26', '2022-07-03', '2022-07-10',
               '2022-07-17', '2022-07-24', '2022-07-31', '2022-08-07',
               '2022-08-14', '2022-08-21', '2022-08-28', '2022-09-04',
               '2022-09-11', '2022-09-18', '2022-09-25', '2022-10-02',
               '2022-10-09', '2022-10-16', '2022-10-23', '2022-10-30',
               '2022-11-06', '2022-11-13', '2022-11-20', '2022-11-27',
               '2022-12-04', '2022-12-11', '2022-12-18', '2022-12-25'],
              dtype='datetime64[ns]', freq=None)

Conclusion

In this article, we have explored how to use Pandas to extract and display all the Sundays of a given year. We used the date_range, dt, and strftime methods of the Pandas library to generate a range of dates, extract the Sundays, and display them in a readable format. Pandas provides a powerful and flexible way to manipulate date and time values in Python, making it a useful tool for data analysis and visualization.

Updated on: 31-Jul-2023

177 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements