Home >Backend Development >Python Tutorial >Super complete! Six ways to get the 'day of the week' for a certain date in Python!
When performing data analysis in Python, grouping and summarizing by date is also needed, for example, to find the periodic pattern of sales.
So before using Python for data statistics, you need to add an extra step: get the day of the week from the specified date. For example, February 22, 2022, happens to be the 22nd Tuesday of the first lunar month, so there are particularly many people registering for marriage on this day. This article takes 2022-02-22 as an example to demonstrate 6 ways to get the "day of the week" for a specified date in Python!
The datetime module is a Python built-in library. There is no need to install pip. In addition to displaying the date and time, it can also Perform date and time operations and formatting.
The method weekday() in the datetime module can be used to retrieve the day of the week. The result returns an integer between 0 and 6, representing "Monday" to "Sunday".
Enter the following command in the interactive environment:
from datetime import datetime datetime.date(2022, 2, 22).weekday()
Output:
1
In the code, the datetime module will first convert the date consisting of year, month and day into datetime date. Then the weekday() method gets "2022-02-22" which day is Tuesday.
But according to our domestic habits, we cannot directly output 1. So we can first customize a list containing Chinese "days of the week", and then return a Chinese day of the week through indexing.
Enter the following command in the interactive environment:
week_list = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"] week_list[datetime.date(2022, 2, 22).weekday()]
Output:
'星期二'
The result is Tuesday directly.
isoweekday() method works similarly to the previous weekday() method. The biggest difference is that it returns an integer between 1-7 to represent "weekday". Monday" to "Sunday".
Enter the following command in the interactive environment:
import datetime datetime.date(2022, 2, 22).isoweekday()
Output:
2
The output result this time is directly "week 2".
If you want to directly output the English week name of the date, you might as well try the strftime() method.
The datetime object can be displayed as a string using the strftime() method. We are actually very familiar with converting date format into string format, such as strftime('%b-%m-%y %H:%M:%S'). Just change the command to %A to get The date is the day of the week.
Enter the following command in the interactive environment:
import datetime datetime.date(2022, 2, 22).strftime("%A")
Output:
'Tuesday'
If you change %A in the code to %a, the day of the week will be output Abbreviation.
datetime.date(2022, 2, 22).strftime("%a")
Output:
'Tue'
The calendar module in Python is also a built-in library, which provides many methods for us to process calendars and is very convenient to use. We can use it to make calendar/weekly calendar and so on. Similarly, it also provides the weekday() and day_name() methods to query the day of the week for a specified date.
Enter the following command in the interactive environment:
import calendar calendar.weekday(2022, 2, 22)
Output:
1
This is exactly the same as the weekday() method of the datetime module in the first method above.
If you want to directly output the English week name of the date, the calendar module also has a method to achieve this.
import datetime import calendar date_week = datetime.date(2022, 2, 22).weekday() calendar.day_name[date_week]
Output:
'Tuesday'
The datetime module also has some limitations, such as handling time zone issues and so on. The pendulum module is a third-party open source library that can solve all problems of the built-in datetime module.
https://www.php.cn/link/ebc03fa648c2cd7da9d63b9ed835664e
Of course, it also has a method to output the day of the week for the specified date.
Enter the following command in the interactive environment:
import pendulum pendulum.parse('2022-02-22').day_of_week
Output:
2
The output result is directly "week 2".
Finally, finally, I want to talk about one of my most commonly used methods. Because Xiaowu usually processes data in Pandas, the method in Pandas will definitely be given priority when generating the "week" column.
Series.dt can be used to access the values of a series in datetimelike form and return several properties. The Series.dt.day_name() function returns the date name with the DateTimeIndex of the specified locale.
Enter the following command in the interactive environment:
import pandas as pd from datetime import datetime df = pd.DataFrame({ "name": ["张三", "李四", "朱五"], "date": [datetime(2022, 2, 21), datetime(2022, 2, 22), datetime(2022, 2, 23)]}) df
The output is as shown below:
##
df["week_num1"] = df["date"].dt.dayofweek df["week_num2"] = df["date"].dt.weekday df["week_name"] = df["date"].dt.day_name() dfThe output is as shown below: In today’s article, we introduced a total of 6 methods to get the day of the week from a specified date. I hope my sharing will be helpful to you. If you see the friends at the end of the article, please give a like in the lower right corner before leaving~
The above is the detailed content of Super complete! Six ways to get the 'day of the week' for a certain date in Python!. For more information, please follow other related articles on the PHP Chinese website!