Thuộc tính PeriodIndex.quarter trong Pandas trả về quý (quarter) của ngày từ từng phần tử trong PeriodIndex object. Quý 1: 1-3, Quý 2: 4-6, Quý 3: 7-9, Quý 4: 10-12.
Các bước thực hiện
1. Import thư viện
import pandas as pd
2. Tạo PeriodIndex
periodIndex = pd.PeriodIndex(
['2021-09-25 07:30:35', '2019-10-30 04:15:45',
'2021-07-15 02:55:15', '2022-06-25 09:40:55'],
freq="T"
)
3. Hiển thị PeriodIndex
print("PeriodIndex...\n", periodIndex)
4. Lấy quý
print("\nQuý của ngày từ PeriodIndex object...\n", periodIndex.quarter)
Ví dụ hoàn chỉnh
import pandas as pd
periodIndex = pd.PeriodIndex(
['2021-09-25 07:30:35', '2019-10-30 04:15:45',
'2021-07-15 02:55:15', '2022-06-25 09:40:55'],
freq="T"
)
print("PeriodIndex...\n", periodIndex)
print("\nPeriodIndex frequency object...\n", periodIndex.freq)
print("\nPeriodIndex frequency as string...\n", periodIndex.freqstr)
print("\nQuý của ngày từ PeriodIndex object...\n", periodIndex.quarter)
Kết quả đầu ra
PeriodIndex...
PeriodIndex(['2021-09-25 07:30', '2019-10-30 04:15', '2021-07-15 02:55', '2022-06-25 09:40'], dtype='period[T]')
PeriodIndex frequency object...
<Minute>
PeriodIndex frequency object as a string...
T
Quý của ngày từ PeriodIndex object...
Int64Index([3, 4, 3, 2], dtype='int64')
Giải thích:
- Tháng 9 → Quý 3
- Tháng 10 → Quý 4
- Tháng 7 → Quý 3
- Tháng 6 → Quý 2