Để trích xuất năm từ DateTimeIndex với tần suất chuỗi thời gian cụ thể, hãy sử dụng DateTimeIndex.day 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
DatetimeIndex với chu kỳ 6 và tần suất là D tức là ngày. Múi giờ là Úc / Sydney -
datetimeindex = pd.date_range('2021-10-20 02:35:55', periods=6, tz='Australia/Sydney',freq='D') Hiển thị DateTimeIndex -
print("DateTimeIndex...\n", datetimeindex) Nhận ngày -
print("\nGetting the day..\n",datetimeindex.day)
Ví dụ
Sau đây là mã -
import pandas as pd
# DatetimeIndex with period 6 and frequency as D i.e. day
# timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:35:55', periods=6, tz='Australia/Sydney',freq='D')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)
# get the day
print("\nGetting the day..\n",datetimeindex.day) Đầu ra
Điều này sẽ tạo ra kết quả sau -
DateTimeIndex...
DatetimeIndex(['2021-10-20 02:35:55+11:00', '2021-10-21 02:35:55+11:00',
'2021-10-22 02:35:55+11:00', '2021-10-23 02:35:55+11:00',
'2021-10-24 02:35:55+11:00', '2021-10-25 02:35:55+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='D')
DateTimeIndex frequency...
<Day>
Getting the day..
Int64Index([20, 21, 22, 23, 24, 25], dtype='int64')