Display all Sundays in a given year using Pandas in Python

WBOY
Release: 2023-08-20 11:37:18
forward
890 people have browsed it

Display all Sundays in a given year using Pandas in Python

Pandas is a powerful Python library for data processing and analysis. A key feature of Pandas is its ability to efficiently handle date and time data. In this article, we will show how to use Pandas to display all Sundays in a given year.

In this article, we will explore how to use Pandas, a popular data manipulation library in Python, to display all Sundays in a given year. We'll walk through the process of extracting the Sundays of the year and displaying them in a readable format.

prerequisites

Before you begin, make sure Pandas is installed on your computer. You can install it by running the following command in the terminal -

pip install pandas
Getting Started
Copy after login

Using Pandas in Python

First, we will start by importing the Pandas library and create a Pandas DataFrame to store the day of the year. We will use the date_range function to generate a date range of one year. Below is the code to generate the date range in 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)
Copy after login

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

Extract Sunday

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

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

dates.dt.day_name() method returns the day of the week name for each date in the dates DataFrame. Then, we filter the dates DataFrame to only keep rows for Sunday.

Show Sunday

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

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

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

The Chinese translation of

Final Code

is:

Final Code

This is the complete code to display all Sundays in 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'))
Copy after login

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)>
Copy after login

Use Pandas to display all Sundays in a given year

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

This is the Python code to accomplish this task. it's here. Let’s break down the code step by step −

  • We use the import statement to import the Pandas library.

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

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

  • We store the filtered date range in a variable named 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)
Copy after login

Output

When you run the above code, you should see a list of all Sundays in a 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)
Copy after login

in conclusion

In this article, we explored how to extract and display all Sundays in a given year using Pandas. We used the date_range, dt, and strftime methods of the Pandas library to generate date ranges, extract 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.

The above is the detailed content of Display all Sundays in a given year using Pandas in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!