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!
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With