pandas 数据框日期时间到秒 [英] Pandas dataframe datetime to time then to seconds
本文介绍了 pandas 数据框日期时间到秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框.一列包含时间戳.我想删除日期并将时间转换为秒.
I have a dataframe. A column contains timestamps. I would like to remove dates and convert the time to seconds.
首先,我将它们转换为日期时间:
First I converted them to datetime:
In:
df_time = pd.to_datetime(df["Timestamp"])
Out:
0 2017-11-07 13:09:00
1 2017-11-07 13:11:00
2 2017-11-07 13:13:00
3 2017-11-07 13:15:00
dtype: datetime64[ns]
然后我删除了日期:
In:
df_time = pd.Series([val.time() for val in df_time])
Out:
0 13:09:00
1 13:11:00
2 13:13:00
3 13:15:00
4 13:17:00
dtype: object
但是它们变成了对象,我没有设法将它们转换为类似datetime的对象以将它们转换为秒.我知道我也通过了一些类似的线程.
But they became objects and I did not managed to convert them to datetime-like objects to convert them into seconds. I know there are some similar threads I went trhough them.
我先感谢您的帮助.
推荐答案
由于将其转换为日期时间序列,因此只需使用基本数学即可获得秒数,即
Since you are converting it to datetime series, simply use basic maths to get the seconds i.e
df_time = pd.to_datetime(df["Timestamp"])
(df_time.dt.hour*60+df_time.dt.minute)*60 + df_time.dt.second
0 47340
1 47460
2 47580
3 47700
Name: Timestamp, dtype: int64
这篇关于 pandas 数据框日期时间到秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文