Table of Contents
prerequisites
Using Pandas in Python
Extract Sunday
Show Sunday
Final Code
Output
Use Pandas to display all Sundays in a given year
in conclusion
Home Backend Development Python Tutorial Display all Sundays in a given year using Pandas in Python

Display all Sundays in a given year using Pandas in Python

Aug 20, 2023 am 11:37 AM
python pandas Sunday

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1254
29
C# Tutorial
1228
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles