将Pandas Timestamp转换为时间(寻找比.apply更快的时间) [英] Converting Pandas Timestamp to just the time (looking for something faster than .apply)
问题描述
因此,如果我在熊猫中有这样的时间戳记:
So if I have a timestamp in pandas as such:
Timestamp('2014-11-07 00:05:00')
如何创建仅包含时间"组件的新列?
How can I create a new column that just has the 'time' component?
所以我想要
00:05:00
当前,我正在使用.apply
,如下所示,但这很慢(我的数据帧是几百万行),我正在寻找一种更快的方法.
Currently, I'm using .apply
as shown below, but this is slow (my dataframe is a couple million rows), and i'm looking for a faster way.
df['time'] = df['date_time'].apply(lambda x: x.time())
我尝试使用.astype(time)
而不是.apply
,因为我注意到.astype
操作可以比.apply
更快,但是显然不能在时间戳上使用(AttributeError:"Timestamp"对象没有属性'astype')...有什么想法吗?
Instead of .apply
, I tried using .astype(time)
, as I noticed .astype
operations can be faster than .apply
, but that apparently doesn't work on timestamps (AttributeError: 'Timestamp' object has no attribute 'astype')... any ideas?
推荐答案
You want .dt.time
see the docs for some more examples of things under the .dt
accessor.
df['date_time'].dt.time
这篇关于将Pandas Timestamp转换为时间(寻找比.apply更快的时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!