Python - Get the weekday from Timestamp object in Pandas

PythonServer Side ProgrammingProgramming

To get the weekday from Timestamp object, use the timestamp.weekday() method. At first, import the required libraries −

import pandas as pd
import datetime

Set the timestamp in Pandas. Create a Timestamp object

timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12))

Get the weekday of the year. Weekday is represented by number Monday == 0, Tuesday == 1 … Sunday == 6

timestamp.weekday()

Example

Following is the code

import pandas as pd
import datetime

# set the timestamp in Pandas
# create a Timestamp object
timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12))

# display the Timestamp
print("Timestamp...\n", timestamp)

# getting the weekday of the year
res = timestamp.weekday()

# display only the weekday
# weekday represented by number Monday == 0, Tuesday == 1 … Sunday == 6
print("\nGet the weekday from Timestamp...\n", res)

Output

This will produce the following code

Timestamp...
 2021-05-12 00:00:00

Get the weekday from Timestamp...
 2
raja
Published on 13-Oct-2021 07:26:25
Advertisements