如何删除列中的日期信息,只需保留时间 [英] How to remove the date information in a column, just keep time

查看:92
本文介绍了如何删除列中的日期信息,只需保留时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pandas数据框.有一个特定的列具有时间信息.

I am using pandas dataframe. there is a specific column has time information.

原始数据如下:

5:15am
5:28am
6:15am

所以我需要将原始数据转换为日期时间格式:

so I need to convert the raw data into datetime format:

format = '%I:%M%p'
dataset['TimeStamp'] = pd.to_datetime(dataset['TimeStamp'],format)

但是,我得到了:

2014-07-04 05:15:00
2014-07-04 05:28:00
2014-07-04 06:15:00

我不想要年份和日期信息,只想要时间.我该如何删除它.谢谢.

I don't want the year and date information, just want time. How can I remove it. Thanks.

推荐答案

以下内容会将您拥有的内容转换为datetime.time()对象:

The following will convert what you have to datetime.time() objects:

dataset['TimeStamp'] = pd.Series([val.time() for val in dataset['TimeStamp']])

输出

  TimeStamp
0  05:15:00
1  05:28:00
2  06:15:00

这篇关于如何删除列中的日期信息,只需保留时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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