创建一个字符串日期,然后将其解析为datetime日期。
from datetime import timedelta
from datetime import date
import datetime as datetime
previous_day=pd.datetime.today().date()-datetime.timedelta(days=1)
start_datetime=datetime.datetime.strptime(str(previous_day)+' 10:00:00','%Y-%m-%d %H:%M:%S')
date_range = pd.date_range(start_datetime, freq='60min', periods=24)
print(date_range)
输出:
DatetimeIndex(['2021-03-25 10:00:00', '2021-03-25 11:00:00',
'2021-03-25 12:00:00', '2021-03-25 13:00:00',
'2021-03-25 14:00:00', '2021-03-25 15:00:00',
'2021-03-25 16:00:00', '2021-03-25 17:00:00',
'2021-03-25 18:00:00', '2021-03-25 19:00:00',
'2021-03-25 20:00:00', '2021-03-25 21:00:00',
'2021-03-25 22:00:00', '2021-03-25 23:00:00',
'2021-03-26 00:00:00', '2021-03-26 01:00:00',
'2021-03-26 02:00:00', '2021-03-26 03:00:00',
'2021-03-26 04:00:00', '2021-03-26 05:00:00',
'2021-03-26 06:00:00', '2021-03-26 07:00:00',
'2021-03-26 08:00:00', '2021-03-26 09:00:00'],
dtype='datetime64[ns]', freq='60T')