Để lấy ngày trong tuần (day of week) mà đối tượng Period rơi vào, sử dụng thuộc tính period.dayofweek. Kết quả: Thứ 2=0, Thứ 3=1, ..., Chủ nhật=6.
Các bước thực hiện
- Import thư viện:
import pandas as pd - Tạo hai Period object:
period1 = pd.Period("2021-09-18")period2 = pd.Period(freq='D', year=2021, month=9, day=22, hour=4, minute=55)
- Truy cập
period1.dayofweekvàperiod2.dayofweek.
Ví dụ hoàn chỉnh
import pandas as pd
period1 = pd.Period("2021-09-18")
period2 = pd.Period(freq='D', year=2021, month=9, day=22, hour=4, minute=55)
print("Period1...\n", period1)
print("Period2...\n", period2)
res1 = period1.dayofweek
res2 = period2.dayofweek
print("\nDay of the week from the 1st Period object ...\n", res1)
print("\nDay of the week from the 2nd Period object...\n", res2)
Kết quả
Period1... 2021-09-18
Period2... 2021-09-22
Day of the week from the 1st Period object ... 5
Day of the week from the 2nd Period object... 2
Giải thích: 18/9/2021 là Thứ 7 (Saturday) → 5. 22/9/2021 là Thứ 4 (Wednesday) → 2. (Thứ 2=0).