Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the date information in a column, just keep time

Tags:

python

pandas

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

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.

like image 766
lserlohn Avatar asked Jul 04 '14 05:07

lserlohn


People also ask

How do I remove the date from a column?

Remove date format from cells by Clear Formats feature The Clear Formats feature is the most common way to remove format from cells in Excel. Of course, it supports to remove all kinds of date formats. Select the date cells that you will remove date formats from, and click Home > Clear > Clear Formats.

How do you remove the date from the date and time in Excel?

Step 1: Highlight the cells to remove time from date. Step 2: Then right-click on it and choose Format Cells… Step 3:-Then in the Format Cells box select Date in Category and select *14-03-2012 in Type, and then click Ok. After that time is removed from the date.


4 Answers

your_date_df.dt.time

Lets say that your column with the date ans time is 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:

df['arrived_time']=df['arrived_date'].dt.time 

The new column df['arrived_time'] will look like this:

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

Observe that the new column, df['arrived_time'], is no longer a datetime64 type, the type of the column is just a pandas object

like image 120
pink.slash Avatar answered Oct 01 '22 23:10

pink.slash


Since version 0.17.0 you can just do

dataset['TimeStamp'].dt.time

For versions older than 0.17.0:

You can just call apply and access the time function on the datetime object create the column initially like this without the need for post processing:

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
like image 12
EdChum Avatar answered Oct 17 '22 06:10

EdChum


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

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
like image 11
Woody Pride Avatar answered Oct 17 '22 06:10

Woody Pride


Just use the datetime.time() function

datetime.time()
Return time object with same hour, minute, second and microsecond. tzinfo is None. See also method timetz().

This will return a datetime.time object and you can access the data with the time.hour time.minute and time.second attributes.

like image 4
Dan Oberlam Avatar answered Oct 17 '22 07:10

Dan Oberlam