简体   繁体   English

如何将字符串中的时间转换为日期时间格式,以便可以获取两列之间的差异

[英]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.

相关问题 如何将numpy数组的两列中的日期和时间转换为matplotlib可以使用的datetime格式? - How do I get date and time from two columns of a numpy array into a datetime format that can be used by matplotlib? 如何格式化不是日期时间格式的日期时间字符串,以便可以将它与 pd.to_datetime() 一起使用? - How can I format date time string which is not date time format so that I can use it with pd.to_datetime()? 如何获得两个 datetime.time Pandas 列之间的绝对差异? - How do I get the absolute difference between two datetime.time Pandas columns? 如何将该日期和时间格式转换为datetime? - How can I convert this date and time format to datetime? 如何获得两个日期时间列熊猫之间的时间差? - How to get difference of time between two datetime columns pandas? Python:如何在不知道格式的情况下将字符串转换为日期时间? - Python: How can I convert string to datetime without knowing the format? 如何在 python 中使用 365 天格式计算两个日期/时间列表之间的时差? - How can I calculate the time difference between two lists of dates/times using 365 day format in python? 如何使用DateTime将时间转换为这种格式? - How can I use DateTime to get the time to this format? 如何找到两列时间之间的差异? - How can I find the difference between two columns with times? 如何将字符串转换为日期时间? - How can I convert string to datetime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM