pandas 如何去掉一列中的日期信息,只保留时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24567078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove the date information in a column, just keep time
提问by lserlohn
I am using pandas dataframe. there is a specific column has time information.
我正在使用Pandas数据框。有一个特定的列有时间信息。
the raw data likes this:
原始数据是这样的:
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)
However, I got:
但是,我得到了:
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.
我不想要年份和日期信息,只想要时间。我怎样才能删除它。谢谢。
回答by Woody Pride
The following will convert what you have to datetime.time() objects:
以下内容会将您拥有的内容转换为 datetime.time() 对象:
dataset['TimeStamp'] = pd.Series([val.time() for val in dataset['TimeStamp']])
Output
输出
TimeStamp
0 05:15:00
1 05:28:00
2 06:15:00
回答by EdChum
Since version 0.17.0you can just do
因为版本0.17.0你可以做
dataset['TimeStamp'].dt.time
For versions older than 0.17.0:
对于早于 的版本0.17.0:
You can just call applyand access the timefunction on the datetime object create the column initially like this without the need for post processing:
您可以调用apply并访问timedatetime 对象上的函数,最初像这样创建列,而无需进行后期处理:
In [143]:
dataset['TimeStamp'] = pd.to_datetime(dataset['TimeStamp'],format).apply(lambda x: x.time())
dataset
Out[143]:
TimeStamp
0 05:15:00
1 05:28:00
2 06:15:00
回答by Dannnno
Just use the datetime.time()function
只需使用该datetime.time()功能
datetime.time()
Return time object with same hour, minute, second and microsecond. tzinfo is None. See also method timetz().
datetime.time()
返回具有相同小时、分钟、秒和微秒的时间对象。tzinfo 是无。另见方法 timetz()。
This will return a datetime.timeobject and you can access the data with the time.hourtime.minuteand time.secondattributes.
这将返回一个datetime.time对象,您可以使用time.hourtime.minute和time.second属性访问数据。
回答by john doe
There's a simpler way to do it using pandas, although most, if not all solutions are correct
使用 Pandas 有一种更简单的方法,尽管大多数(如果不是所有)解决方案都是正确的
df.TimeStamp = pd.to_datetime(df.TimeStamp).dt.strftime('%H:%M')
回答by pink.slash
your_date_df.dt.time
your_date_df.dt.time
Lets say that your column with the date ans time is df['arrived_date']:
假设您的日期和时间列是df['arrived_date']:
0 2015-01-06 00:43:00
1 2015-01-06 07:56:00
2 2015-01-06 11:02:00
3 2015-01-06 11:22:00
4 2015-01-06 15:27:00
Name: arrived_date, dtype: datetime64[ns]
Whith pandas, you just need to do:
对于Pandas,您只需要执行以下操作:
df['arrived_time']=df['arrived_date'].dt.time
The new column df['arrived_time']will look like this:
新列df['arrived_time']将如下所示:
0 00:43:00
1 07:56:00
2 11:02:00
3 11:22:00
4 15:27:00
Name: arrived_time, dtype: object
Observethat the new column, df['arrived_time'], is no longer a datetime64type, the type of the column is just a pandas object
观察到新的列, df['arrived_time'], 不再是一个datetime64类型,该列的类型只是一个pandasobject
回答by K.Hakan
dataset['TimeStamp']=dataset['TimeStamp'].str.slice(11,18)
数据集['TimeStamp']=dataset['TimeStamp'].str.slice(11,18)

