简体   繁体   English

Pandas 到时间增量只有小时分钟和秒

[英]Pandas to time delta with only hours minutes and seconds

In my script i extract a excel that in one column called "Time" is dtype object, in that column there's a hour like so "14:00:00" i want to convert the column to_datetime but when i do this:在我的脚本中,我提取了一个 excel,在一个名为“Time”的列中是 dtype object,在该列中有一个小时,如“14:00:00”我想将列转换为 to_datetime 但是当我这样做时:

df['Time']=pandas.to_datetime(df['Time'],format='%H:%M:%S')

i adds the year month and day to it and i dont want that to happen and i also want to keep that column as a datetime so i can then subtract to another time and get the seconds.我在其中添加了年月日,我不希望这种情况发生,我还想将该列保留为日期时间,这样我就可以减去另一个时间并获得秒数。 How can i pass it to datetime with only the hours minutes and seconds?我怎样才能将它传递给只有小时分钟和秒的日期时间?

Use Series.dt.time :使用Series.dt.time

df['Time']=pandas.to_datetime(df['Time'],format='%H:%M:%S').dt.time

For adding or subtracting time , you can do this:对于添加或减去time ,您可以这样做:

pd.Timedelta(df.Time - t1).seconds / 3600.0

you can use timedelta :你可以使用timedelta

df['Time'] = pd.to_timedelta(df['Time'])

To calculate minutes:要计算分钟:

pd.to_timedelta(df.time).dt.seconds/60

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM