Để làm tròn lên (ceil) DateTimeIndex đến tần suất giây, dùng DateTimeIndex.ceil(freq='S').
Các bước thực hiện
1. Import thư viện
import pandas as pd
2. Tạo DateTimeIndex
datetimeindex = pd.date_range(
'2021-10-18 07:20:32.261811624',
periods=5,
tz='Australia/Adelaide',
freq='40S'
)
3. Lấy thành phần giây (tham khảo)
print("Giây từ DateTimeIndex...\n", datetimeindex.second)
4. Ceil giây
print("\nCeil giây...\n",
datetimeindex.ceil(freq='S'))
Ví dụ hoàn chỉnh
import pandas as pd
datetimeindex = pd.date_range(
'2021-10-18 07:20:32.261811624',
periods=5,
tz='Australia/Adelaide',
freq='40S'
)
print("DateTimeIndex...\n", datetimeindex)
print("DateTimeIndex frequency...\n", datetimeindex.freq)
print("\nGiây từ DateTimeIndex...\n", datetimeindex.second)
print("\nCeil giây...\n",
datetimeindex.ceil(freq='S'))
Kết quả đầu ra
DateTimeIndex...
DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30', ...],
dtype='datetime64[ns, Australia/Adelaide]', freq='40S')
Giây từ DateTimeIndex...
Int64Index([32, 12, 52, 32, 12], dtype='int64')
Ceil giây...
DatetimeIndex(['2021-10-18 07:20:33+10:30', '2021-10-18 07:21:13+10:30',
'2021-10-18 07:21:53+10:30', '2021-10-18 07:22:33+10:30',
'2021-10-18 07:23:13+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
Giải thích
- Gốc có phần lẻ giây (nano/micro/mili) → ceil giây = giây tiếp theo
- Ví dụ:
32.261811624→33giây - Kết quả không còn phần lẻ giây, tần suất (freq) bị mất
Tóm tắt ceil các tần suất
| freq | Ví dụ gốc | Kết quả ceil |
|---|---|---|
| 'us' | .261811624 | .261812 |
| 'ms' | .261811624 | .262000 |
| 'S' | 32.261811624 | 33 |
| 'T' | 20:32.26 | 21:00 |
| 'H' | 07:20:32 | 08:00:00 |