Để trích xuất giây từ DateTimeIndex với tần suất chuỗi thời gian cụ thể, hãy sử dụng DateTimeIndex.second 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 là S tức là giây. Múi giờ là Úc / Sydney -
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='S')
Hiển thị DateTimeIndex -
print("DateTimeIndex...\n", datetimeindex) Nhận giây -
print("\nGetting the seconds..\n",datetimeindex.second)
Ví dụ
Sau đây là mã -
import pandas as pd
# DatetimeIndex with period 6 and frequency as S i.e. seconds
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='S')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)
# get the seconds
print("\nGetting the seconds..\n",datetimeindex.second) Đầ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:51+11:00', '2021-10-20 02:30:52+11:00', '2021-10-20 02:30:53+11:00', '2021-10-20 02:30:54+11:00', '2021-10-20 02:30:55+11:00'], dtype='datetime64[ns, Australia/Sydney]', freq='S') DateTimeIndex frequency... <Second> Getting the seconds.. Int64Index([50, 51, 52, 53, 54, 55], dtype='int64')