Để lấy thành phần tháng (month) trong năm của đối tượng Period, sử dụng thuộc tính period.month.
Các bước thực hiện
- Import thư viện:
import pandas as pd - Tạo hai Period object:
period1 = pd.Period("2020-09-23 05:55:30")period2 = pd.Period(freq="M", year=2021, month=8, day=16, hour=2, minute=35)
- Truy cập
period1.monthvàperiod2.month.
Ví dụ hoàn chỉnh
import pandas as pd
period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="M", year=2021, month=8, day=16, hour=2, minute=35)
print("Period1...\n", period1)
print("Period2...\n", period2)
res1 = period1.month
res2 = period2.month
print("\nMonth from the 1st Period object ...\n", res1) # 9
print("\nMonth from the 2nd Period object...\n", res2) # 8
Kết quả
Period1... 2020-09-23 05:55:30
Period2... 2021-08
Month from the 1st Period object ... 9
Month from the 2nd Period object... 8