Để lấy ngày trong tuần từ đối tượng Dấu thời gian, hãy sử dụng timestamp.weekday () phương pháp. Đầu tiên, hãy nhập các thư viện được yêu cầu -
import pandas as pd import datetime
Đặt dấu thời gian trong Pandas. Tạo đối tượng Dấu thời gian
timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12))
Nhận các ngày trong tuần trong năm. Ngày trong tuần được biểu thị bằng số Thứ Hai ==0, Thứ Ba ==1… Chủ Nhật ==6
timestamp.weekday()
Ví dụ
Sau đây là mã
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) Đầu ra
Điều này sẽ tạo ra mã sau
Timestamp... 2021-05-12 00:00:00 Get the weekday from Timestamp... 2