Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Python Pandas - Lấy phút (minute) trong giờ từ đối tượng Period

Để lấy thành phần phút (minute) trong giờ của đối tượng Period, sử dụng thuộc tính period.minute.

Các bước thực hiện

  1. Import thư viện: import pandas as pd
  2. Tạo hai Period object:
    • period1 = pd.Period("2020-09-23 05:55:30")
    • period2 = pd.Period(freq="T", year=2021, month=7, day=16, hour=2, minute=35) (tần suất phút)
  3. Truy cập period1.minute và period2.minute.

Ví dụ hoàn chỉnh

import pandas as pd

period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="T", year=2021, month=7, day=16, hour=2, minute=35)

print("Period1...\n", period1)
print("Period2...\n", period2)

res1 = period1.minute
res2 = period2.minute

print("\nMinute of the hour from the 1st Period object ...\n", res1)  # 55
print("\nMinute of the hour from the 2nd Period object...\n", res2)   # 35

Kết quả

Period1... 2020-09-23 05:55:30
Period2... 2021-07-16 02:35
Minute of the hour from the 1st Period object ... 55
Minute of the hour from the 2nd Period object... 35