Skip to main content
 首页 » 编程设计

python之将字符串转换为没有日期的 Pandas 时间戳

2024年11月24日23zhwl

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

我正在寻找的是:

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

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

请您参考如下方法:

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

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