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

Python Pandas - Lấy bí danh chuỗi (string alias) của tần suất thời gian từ Period

Để lấy bí danh chuỗi (string alias) của tần suất chuỗi thời gian (time series frequency) được áp dụng trên đối tượng Period, sử dụng thuộc tính period.freqstr.

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") (tần suất giây mặc định)
    • period2 = pd.Period(freq="Y", year=2021, month=2, day=14, hour=2, minute=35) (tần suất năm)
  3. Truy cập period1.freqstr và period2.freqstr.

Ví dụ hoàn chỉnh

import pandas as pd

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

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

res1 = period1.freqstr
res2 = period2.freqstr

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

Kết quả

Period1... 2020-09-23 03:55:20
Period2... 2021
String alias of the frequency from the 1st Period object ... 
String alias of the frequency from the 2nd Period object ... 

Giải thích: freqstr trả về biểu diễn chuỗi của tần suất (frequency) của Period. nghĩa là tần suất giây, nghĩa là tần suất năm kết thúc tháng 12.