[英]How can I convert time in string into a datetime format so that I can get difference between two columns
I have a dataframe with two columns with different times in string format, I want to find the difference between the two columns so I use the following code 我有一个数据框,其中两列的时间不同,且字符串格式不同,我想查找两列之间的差异,因此我使用以下代码
operational_data_clean['Pick/pack start-time'] = pd.to_datetime(operational_data_clean['Pick/pack start-time'])
operational_data_clean['Flight launched-time'] = pd.to_datetime(operational_data_clean['Flight launched-time'])
operational_data_clean['time_to_launch'] = 0
operational_data_clean['time_to_launch'] = operational_data_clean['Flight launched-time'] - operational_data_clean['Pick/pack start-time']
but this code when I run the first time I get good results but when I run the second time it add todays date on the 'Pick/pack start-time' and 'Flight launched-time' value. 但是,当我第一次运行此代码时,我会得到很好的结果,但是当我第二次运行时,它会将今天的日期添加到“选择/打包开始时间”和“飞行启动时间”值上。
How can I convert this time only to hours without getting the dates that are messing my data. 我如何才能将此时间仅转换为几个小时而又不弄乱数据的日期。
I am assuming you are running your code with jupyter notebook. 我假设您正在使用jupyter notebook运行代码。
When you execute your code, your variable operational_data_clean['Pick/pack start-time']
becomes pd.to_datetime(operational_data_clean['Pick/pack start-time'])
. 当您执行代码时,您的变量
pd.to_datetime(operational_data_clean['Pick/pack start-time'])
operational_data_clean['Pick/pack start-time']
变为pd.to_datetime(operational_data_clean['Pick/pack start-time'])
。
So when you execute the block one more time, jupyter remembers your variables and therefore will perform the same function again, essentially doing this: pd.to_datetime(pd.to_datetime(operational_data_clean['Pick/pack start-time']))
. 因此,当您再次执行该块时,jupyter会记住您的变量,因此将再次执行相同的功能,基本上就是这样做:
pd.to_datetime(pd.to_datetime(operational_data_clean['Pick/pack start-time']))
。
As for pd.to_datetime()
itself, I would advise to look through the pandas docs . 至于
pd.to_datetime()
本身,我建议您仔细阅读pandas docs 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.