首页 > 解决方案 > 将字符串转换为不带日期的 pandas 时间戳

问题描述

我应该如何将时间 '14:12:2006' 、 '1200' 和 '1500' 转换为 pandas 日期时间,只有时间部分,以便我可以检查第一次是否在其他两个时间之间。

我正在寻找的是:

12:00  <  14:12:2006  <  15:00

我只需要转换,而不是比较。

标签: pythonpandas

解决方案


如果您只需要比较它们,您可以使用

s=pd.Series(['14:12:2006' , '1200' , '1500']).str.replace(':','').astype(str)
s=s.str.ljust(s.str.len().max(),'0').astype(int)
s.iloc[1]<s.iloc[0]<s.iloc[2]
True

s
0    14122006
1    12000000
2    15000000
dtype: int64

推荐阅读