我认为如果需要输出你需要string
s zfill http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.zfill.html and str[]
按职位选择:
t1 = quote['BuyTime'].astype(str).str.zfill(6)
t2 = quote['SellTime'].astype(str).str.zfill(6)
quote['BuyTime'] = t1.str[0:2] + ':' + t1.str[2:4] + ':' + t1.str[4:6]
quote['SellTime'] = t2.str[0:2] + ':' + t2.str[2:4] + ':' + t2.str[4:6]
print (quote)
BuyTime ID SellTime
0 09:46:50 1 09:46:51
1 09:47:17 1 09:48:17
2 12:04:58 2 11:41:19
或者如果需要的话python times
add 0
by zfill http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.zfill.html, 转换成datetime
并提取time http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.time.htmls:
t1 = quote['BuyTime'].astype(str).str.zfill(6)
t2 = quote['SellTime'].astype(str).str.zfill(6)
quote['BuyTime'] = pd.to_datetime(t1, format='%H%M%S').dt.time
quote['SellTime'] = pd.to_datetime(t2, format='%H%M%S').dt.time
print (quote)
BuyTime ID SellTime
0 09:46:50 1 09:46:51
1 09:47:17 1 09:48:17
2 12:04:58 2 11:41:19
替代方案string
s 的输出是strftime http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.strftime.html:
quote['BuyTime'] = pd.to_datetime(t1, format='%H%M%S').dt.strftime('%H:%M:%S')
quote['SellTime'] = pd.to_datetime(t2, format='%H%M%S').dt.strftime('%H:%M:%S')
print (quote)
BuyTime ID SellTime
0 09:46:50 1 09:46:51
1 09:47:17 1 09:48:17
2 12:04:58 2 11:41:19