Python Pandas - 從具有特定時間序列頻率的 DateTimeIndex 中提取日期的季度
要從具有特定時間序列頻率的 DateTimeIndex 中提取日期的季度,請使用 **DateTimeIndex.quarter**。
首先,匯入所需的庫 -
import pandas as pd
建立一個週期為 6 且頻率為 M(即月份)的 DatetimeIndex。時區為澳大利亞/悉尼 -
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='2M')
顯示 DateTimeIndex 頻率 -
print("DateTimeIndex frequency...\n", datetimeindex.freq)獲取日期的季度 -
print("\nGet the quarter of the date..\n",datetimeindex.quarter)
結果基於以下年份的季度 -
Quarter 1 = 1st January to 31st March Quarter 2 = 1st April to 30th June Quarter 3 = 1st July to 30th September Quarter 4 = 1st October to 31st December
示例
以下是程式碼 -
import pandas as pd
# DatetimeIndex with period 6 and frequency as M i.e. Month
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='2M')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)
# Get the quarter of the date
# Result is based on the following quarters of an year:
# Quarter 1 = 1st January to 31st March
# Quarter 2 = 1st April to 30th June
# Quarter 3 = 1st July to 30th September
# Quarter 4 = 1st October to 31st December
print("\nGet the quarter of the date..\n",datetimeindex.quarter)輸出
這將生成以下程式碼 -
DateTimeIndex... DatetimeIndex(['2021-10-31 02:30:50+11:00', '2021-12-31 02:30:50+11:00', '2022-02-28 02:30:50+11:00', '2022-04-30 02:30:50+10:00', '2022-06-30 02:30:50+10:00', '2022-08-31 02:30:50+10:00'], dtype='datetime64[ns, Australia/Sydney]', freq='2M') DateTimeIndex frequency... <2 * MonthEnds> Get the quarter of the date.. Int64Index([4, 4, 1, 2, 2, 3], dtype='int64')
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP