I need just the hour from a column of a df that looks like:
2021-04-30 09:32 +0000
So I split it:
#split the time column into hour and date
df_new['date'] = df_new['time'].astype(str)
# split date into 3 columns
df_new[['date_1','hour','remove']] = df_new['date'].astype(str).str.split(expand=True)
#drop unnecessary columns
df_new = df_new.drop(labels=['time','date','remove'], axis=1)
from datetime import datetime
df_new['hour']= df_new['hour'].astype(str)
#format hour column as date time
df_new['hour'] = pd.to_datetime(df_new['hour'], format='%H:%M').dt.time
The result is:
0 09:32:00
1 01:00:00
2 17:18:00
3 13:08:00
4 11:26:00
But I need just the hours, and I always get the same error that it is not in datetime format because the column dtype
is object
. I tried all solutions but didn't work.