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

Python Pandas - Lấy tần suất (frequency) của đối tượng Period

Để lấy đối tượng tần suất (frequency) của Period, sử dụng thuộc tính period.freq.

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 03:55:20")
    • period2 = pd.Period(freq="T", year=2021, month=2, day=14, hour=2, minute=35)
  3. Truy cập period1.freq và period2.freq.

Ví dụ hoàn chỉnh

import pandas as pd

period1 = pd.Period("2020-09-23 03:55:20")
period2 = pd.Period(freq="T", year=2021, month=2, day=14, hour=2, minute=35)

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

res1 = period1.freq
res2 = period2.freq

print("\nDisplay the frequency from the 1st Period object ...\n", res1)  # 
print("\nDisplay the frequency from the 2nd Period object ...\n", res2)   # 

Kết quả

Period1... 2020-09-23 03:55:20
Period2... 2021-02-14 02:35
Display the frequency from the 1st Period object ... 
Display the frequency from the 2nd Period object ... 

Giải thích: freq trả về đối tượng DateOffset (ví dụ: Second, Minute, Hour, Day, MonthEnd, YearEnd) đại diện cho tần suất của Period.