Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python - Get the weekday from Timestamp object in Pandas
To get the weekday from a Timestamp object in Pandas, use the timestamp.weekday() method. This returns an integer where Monday = 0, Tuesday = 1, ..., Sunday = 6.
Syntax
timestamp.weekday()
Basic Example
Let's create a Timestamp and get its weekday ?
import pandas as pd
import datetime
# Create a Timestamp object
timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12))
# Display the Timestamp
print("Timestamp:", timestamp)
# Get the weekday (0=Monday, 1=Tuesday, ..., 6=Sunday)
weekday = timestamp.weekday()
print("Weekday number:", weekday)
print("This is a Wednesday (2)")
Timestamp: 2021-05-12 00:00:00 Weekday number: 2 This is a Wednesday (2)
Multiple Timestamps
You can also get weekdays for multiple timestamps ?
import pandas as pd
# Create multiple timestamps
dates = ['2021-05-10', '2021-05-11', '2021-05-12', '2021-05-13']
timestamps = [pd.Timestamp(date) for date in dates]
for ts in timestamps:
weekday = ts.weekday()
day_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
print(f"{ts.date()}: {weekday} ({day_names[weekday]})")
2021-05-10: 0 (Monday) 2021-05-11: 1 (Tuesday) 2021-05-12: 2 (Wednesday) 2021-05-13: 3 (Thursday)
Alternative: day_name() Method
For readable weekday names, use day_name() instead ?
import pandas as pd
timestamp = pd.Timestamp('2021-05-12')
# Get weekday number
weekday_num = timestamp.weekday()
print("Weekday number:", weekday_num)
# Get weekday name
weekday_name = timestamp.day_name()
print("Weekday name:", weekday_name)
Weekday number: 2 Weekday name: Wednesday
Key Points
-
weekday()returns integers: Monday=0, Tuesday=1, ..., Sunday=6 -
day_name()returns readable day names like "Monday", "Tuesday" - Both methods work with individual Timestamp objects
Conclusion
Use timestamp.weekday() to get numeric weekday values (0-6). For readable names, use timestamp.day_name() instead.
Advertisements
