Để trích xuất micro giây từ DateTimeIndex với tần suất chuỗi thời gian cụ thể, hãy sử dụng DateTimeIndex.microsecond tài sản.
Đầu tiên, hãy nhập các thư viện được yêu cầu -
import pandas as pd
Tạo một DatetimeIndex với chu kỳ 6 và tần suất như chúng tôi, tức là micro giây. Múi giờ là Úc / Sydney -
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='us')
Hiển thị DateTimeIndex -
print("DateTimeIndex...\n", datetimeindex) Nhận micro giây -
print("\nGetting the microseconds..\n",datetimeindex.microsecond)
Ví dụ
Sau đây là mã -
import pandas as pd
# DatetimeIndex with period 6 and frequency as us i.e. microseconds
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='us')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)
# get the microsecond
print("\nGetting the microseconds..\n",datetimeindex.microsecond) Đầu ra
Điều này sẽ tạo ra mã sau -
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') DateTimeIndex frequency... <Micro> Getting the microseconds.. Int64Index([0, 1, 2, 3, 4, 5], dtype='int64')