3

I have a dataframe which has a datetime column:

Date
2020-01-01 00:00:00
...
2020-02-29 23:45:00

How can convert this to the format:

Date
2020-01-01 0
...
2020-02-29 23

I tried: df['hour'] = df['Date'].dt.day+ " "+df['Date'].dt.hour but this lets to:

TypeError: unsupported operand type(s) for +: 'datetime.date' and 'str'

PV8
  • 5,799
  • 7
  • 43
  • 87

1 Answers1

5

Use dt.strftime:

pd.to_datetime(df.Date).dt.strftime('%Y-%m-%d %H')

0    2020-01-01 00
1    2020-02-29 23
Name: Date, dtype: object
yatu
  • 86,083
  • 12
  • 84
  • 139