将Pandas Timestamp转换为时间(寻找比.apply更快的时间) [英] Converting Pandas Timestamp to just the time (looking for something faster than .apply)

查看:127
本文介绍了将Pandas Timestamp转换为时间(寻找比.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?

推荐答案

您要.dt.time参见

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆