目前,我正在使用以下代码生成每小时的时间
import pandas as pd
times=[pd.to_datetime(i) for i in '09:14:00','10:15:00','11:15:00','12:15:00','13:15:00','14:15:00','15:15:00', '15:30:00']
我需要像'09:14:00','09:15:00'
有没有一种方法可以像上面那样对分钟进行计时而无需进行硬编码hh:mm:ss
。
我尝试了pd.date_range("09:14:00", "15:31:00", freq="1min").time
但是输出的类型与我期望的不同
In [100]: pd.date_range("09:14:00", "09:15:00", freq="1min").time
Out[100]: array([datetime.time(9, 14), datetime.time(9, 15)], dtype=object)
#expected type
In [99]: pd.to_datetime("09:14:00")
Out[99]: Timestamp('2019-02-20 09:14:00')
答案 0 :(得分:0)
我认为您需要的是timedelta
,因为Timestamp
没有日期,只有时间,不存在:
r1 = pd.timedelta_range("09:14:00", "09:20:00", freq="1min")
print (r1)
TimedeltaIndex(['09:14:00', '09:15:00', '09:16:00', '09:17:00', '09:18:00',
'09:19:00', '09:20:00'],
dtype='timedelta64[ns]', freq='T')
只能在time
中生成strings
:
r2 = pd.date_range("09:14:00", "09:20:00", freq="1min").strftime('%H:%M:%S').tolist()
print (r2)
['09:14:00', '09:15:00', '09:16:00', '09:17:00', '09:18:00', '09:19:00', '09:20:00']