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

Python Pandas - Trích xuất microsecond từ DateTimeIndex


Để lấy phần microsecond (10⁻⁶ giây) từ DateTimeIndex, sử dụng thuộc tính DateTimeIndex.microsecond.

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

1. Import thư viện

import pandas as pd

2. Tạo DateTimeIndex tần suất microsecond

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

3. Hiển thị DateTimeIndex

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

4. Lấy microsecond

print("\nPhần microsecond...\n", datetimeindex.microsecond)

Ví dụ hoàn chỉnh

import pandas as pd

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

print("DateTimeIndex...\n", datetimeindex)
print("Tần suất...\n", datetimeindex.freq)
print("\nPhần microsecond...\n", datetimeindex.microsecond)

Kết quả đầu ra

DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+11:00', '2021-10-20 02:30:50.000001+11:00',
               '2021-10-20 02:30:50.000002+11:00', '2021-10-20 02:30:50.000003+11:00',
               '2021-10-20 02:30:50.000004+11:00', '2021-10-20 02:30:50.000005+11:00'],
              dtype='datetime64[ns, Australia/Sydney]', freq='U')
Tần suất...
<Micro>
Phần microsecond...
Int64Index([0, 1, 2, 3, 4, 5], dtype='int64')

Lưu ý: Tần suất 'us' (hoặc 'U') tạo các timestamp cách nhau 1 microsecond. Thuộc tính microsecond trả về giá trị 0-999999.