Python Pandas - 如何以每小时的频率对 DateTimeIndex 执行 ceil 操作
要以每小时的频率对DateTimeIndex执行ceil操作,请使用方法。对于每小时频率,使用值为“H”的freq参数。DateTimeIndex.ceil()
首先,导入所需的库-
import pandas as pd
创建一个日期时间索引,周期为5,频率为分钟,即分钟-
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='20min')
以每小时的频率对DateTimeIndex日期执行Ceil操作。对于每小时频率,我们使用了“H”-
print("\nPerforming ceil operation with hourly frequency...\n", datetimeindex.ceil(freq='H'))
示例
以下是代码-
import pandas as pd #DatetimeIndexwithperiod5andfrequencyasmini.e.minutes #timezoneisAustralia/Adelaide datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='20min') #displayDateTimeIndex print("DateTimeIndex...\n", datetimeindex) #displayDateTimeIndex frequency print("DateTimeIndex frequency...\n", datetimeindex.freq) #CeiloperationonDateTimeIndexdatewithhourlyfrequency # For hourly frequency, we have used 'H' print("\nPerforming ceil operation with hourly frequency...\n", datetimeindex.ceil(freq='H'))输出结果
这将产生以下代码-
DateTimeIndex... DatetimeIndex(['2021-09-29 07:20:32.261811624+09:30', '2021-09-29 07:40:32.261811624+09:30', '2021-09-29 08:00:32.261811624+09:30', '2021-09-29 08:20:32.261811624+09:30', '2021-09-29 08:40:32.261811624+09:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='20T') DateTimeIndex frequency... <20 * Minutes> Performing ceil operation with hourly frequency... DatetimeIndex(['2021-09-29 08:00:00+09:30', '2021-09-29 08:00:00+09:30', '2021-09-29 09:00:00+09:30', '2021-09-29 09:00:00+09:30', '2021-09-29 09:00:00+09:30'], dtype='datetime64[ns, Australia/Adelaide]', freq=None)