Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop the Year-Month-Date from a datetime series in python?

I have a datetime series, and I want to only keep the "Hour-Miniute-Second" strings in the series, and drop the Year-Month-Date strings. So what should I do?

original series:

0         2000-12-31 22:12:40
1         2000-12-31 22:35:09
2         2000-12-31 22:32:48
3         2000-12-31 22:04:35
4         2001-01-06 23:38:11
5         2000-12-31 22:37:48
……

target:

0          22:12:40
1          22:35:09
2          22:32:48
3          22:04:35
4          23:38:11
5          22:37:48
……

Preiviously the original series has been already translated from a unix timestamp series by using pandas.to_datetime(). But I'm not able to use this method to reach my target:(

Any suggestions is appreciated!

like image 846
Ricky Avatar asked Apr 23 '17 06:04

Ricky


2 Answers

Use dt.strftime:

#if necessary convert to datetime
#df['date'] = pd.to_datetime(df['date'])

print (df.dtypes)
date    datetime64[ns]
dtype: object

df['time'] = df['date'].dt.strftime('%H:%M:%S')
print (df)
                 date      time
0 2000-12-31 22:12:40  22:12:40
1 2000-12-31 22:35:09  22:35:09
2 2000-12-31 22:32:48  22:32:48
3 2000-12-31 22:04:35  22:04:35
4 2001-01-06 23:38:11  23:38:11
5 2000-12-31 22:37:48  22:37:48

Or cast to string, split and select second value of lists by str[1] :

#if dtype of date column is object (obviously string), omit astype
df['time'] = df['date'].astype(str).str.split().str[1]
print (df)
                 date      time
0 2000-12-31 22:12:40  22:12:40
1 2000-12-31 22:35:09  22:35:09
2 2000-12-31 22:32:48  22:32:48
3 2000-12-31 22:04:35  22:04:35
4 2001-01-06 23:38:11  23:38:11
5 2000-12-31 22:37:48  22:37:48
like image 145
jezrael Avatar answered Sep 27 '22 20:09

jezrael


You've got some nice answer's already. However, they store them as strings, and you might want to store them as actual datetime.time objects:

import pandas as pd

df = pd.DataFrame([
    {'date': '2000-12-31 22:12:40'},
    {'date': '2000-12-31 22:35:09'},
    {'date': '2000-12-31 22:32:48'},
    {'date': '2000-12-31 22:04:35'},
    {'date': '2001-01-06 23:38:11'},
    {'date': '2000-12-31 22:37:48'},
])

df['date'] = pd.to_datetime(df['date'])
df['time'] = df['date'].dt.time

print(df)
print(df['time'].dtype)
print(type(df['time'][0]))

                 date      time
0 2000-12-31 22:12:40  22:12:40
1 2000-12-31 22:35:09  22:35:09
2 2000-12-31 22:32:48  22:32:48
3 2000-12-31 22:04:35  22:04:35
4 2001-01-06 23:38:11  23:38:11
5 2000-12-31 22:37:48  22:37:48
object
<class 'datetime.time'>

Edit:

Noticed your target was without the date column. Add the following to the end:

del df['date']
print(df)

       time
0  22:12:40
1  22:35:09
2  22:32:48
3  22:04:35
4  23:38:11
5  22:37:48
like image 38
André C. Andersen Avatar answered Sep 27 '22 22:09

André C. Andersen