0

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.

  • You can use `parse_dates` parameter in the `pandas.read_csv()` method if you are creating a `dataframe` object from a `.csv` file and then you can use `dt` accessor to extract the `hour` from each datetime object in the `date` column i.e. `df_new['hour'] = df_new['date'].dt.hour` – strikersps Aug 30 '21 at 14:50

1 Answers1

2

Convert your column to datetime and use .dt.hour:

pd.to_datetime(df['date']).dt.hour
mozway
  • 194,879
  • 13
  • 39
  • 75