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

Python Pandas - Trích xuất đối tượng tần suất từ DateTimeIndex


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

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

1. Import thư viện

import pandas as pd

2. Tạo DateTimeIndex

datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D')

3. Hiển thị DateTimeIndex

print("DateTimeIndex...\n", datetimeindex)

4. Lấy đối tượng tần suất

print("Đối tượng tần suất...\n", datetimeindex.freq)

Ví dụ hoàn chỉnh

import pandas as pd

datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D')

print("DateTimeIndex...\n", datetimeindex)
print("Đối tượng tần suất...\n", datetimeindex.freq)

Kết quả đầu ra

DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+10:30', '2021-10-21 02:30:50+10:30',
               '2021-10-22 02:30:50+10:30', '2021-10-23 02:30:50+10:30',
               '2021-10-24 02:30:50+10:30', '2021-10-25 02:30:50+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='D')
Đối tượng tần suất...
<Day>

Lưu ý: Thuộc tính freq trả về đối tượng DateOffset (như Day, Hour, MonthEnd), cho phép kiểm tra và thao tác nâng cao hơn so với chuỗi freqstr.