Thuộc tính PeriodIndex.day trong Pandas trả về ngày trong tháng (day of month, 1-31) từ từng phần tử trong PeriodIndex object.
Các bước thực hiện
1. Import thư viện
import pandas as pd
2. Tạo PeriodIndex tần suất ngày
periodIndex = pd.PeriodIndex(
['2018-07-25', '2019-10-30', '2020-11-20',
'2021-09-15', '2022-03-12', '2023-06-18'],
freq="D"
)
3. Hiển thị PeriodIndex
print("PeriodIndex...\n", periodIndex)
4. Lấy ngày trong tháng
print("\nNgày trong tháng từ PeriodIndex...\n", periodIndex.day)
Ví dụ hoàn chỉnh
import pandas as pd
periodIndex = pd.PeriodIndex(
['2018-07-25', '2019-10-30', '2020-11-20',
'2021-09-15', '2022-03-12', '2023-06-18'],
freq="D"
)
print("PeriodIndex...\n", periodIndex)
print("\nPeriodIndex frequency...\n", periodIndex.freq)
print("\nNgày trong tháng từ PeriodIndex...\n", periodIndex.day)
Kết quả đầu ra
PeriodIndex...
PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20', '2021-09-15', '2022-03-12', '2023-06-18'], dtype='period[D]')
PeriodIndex frequency...
<Day>
Ngày trong tháng từ PeriodIndex...
Int64Index([25, 30, 20, 15, 12, 18], dtype='int64')
Giải thích: Trả về Int64Index chứa ngày của từng period: 25, 30, 20, 15, 12, 18.
Tóm tắt các thuộc tính ngày:
| Thuộc tính | Ý nghĩa | Phạm vi |
|---|---|---|
day |
Ngày trong tháng | 1-31 |
dayofyear |
Ngày trong năm | 1-366 |
dayofweek / weekday |
Thứ trong tuần | 0-6 (Thứ 2-CN) |