Home > other >  Pandas combine date column and hour column in %Y%m%d %H:%M:%S format
Pandas combine date column and hour column in %Y%m%d %H:%M:%S format

Time:09-17

I have an xlsx file with hourly precipitation and it has three columns: a date (form 20210727), a time ( form 01:00:00) and a precipitation value. Using pd.to_datetime I change my date column into a date:

prec['date'] = pd.to_datetime(prec['date'], format="%Y%m%d")

When I try to then combine the date and hour columns into a new Datetime column, I get two timestamps, the actual one (time column) and a 00:00:00 timestamp attached to my date.

prec['Datetime'] = prec['date'].apply(str) ' ' prec['hour'].apply(str)

    date        hour        prec    Datetime
0   2021-07-27  00:00:00    0.0     2021-07-27 00:00:00 00:00:00
1   2021-07-27  01:00:00    0.0     2021-07-27 00:00:00 01:00:00
2   2021-07-27  02:00:00    0.0     2021-07-27 00:00:00 02:00:00
3   2021-07-27  03:00:00    0.0     2021-07-27 00:00:00 03:00:00
4   2021-07-27  04:00:00    0.0     2021-07-27 00:00:00 04:00:00 

I would like to get the final Datetime without the 00:00:00 time stamp so as

    date        hour        prec    Datetime
0   2021-07-27  00:00:00    0.0     2021-07-27 00:00:00
1   2021-07-27  01:00:00    0.0     2021-07-27 01:00:00
2   2021-07-27  02:00:00    0.0     2021-07-27 02:00:00
3   2021-07-27  03:00:00    0.0     2021-07-27 03:00:00
4   2021-07-27  04:00:00    0.0     2021-07-27 04:00:00

CodePudding user response:

to get a column of pandas datetime datatype, combine date and time first as string (!), then convert to datetime:

prec['datetime'] = pd.to_datetime(prec['date']   ' '   prec['hour'])

CodePudding user response:

You can use this code:

prec['Datetime'] = prec['date'].dt.strftime("%Y-%d-%m")  ' '   prec['hour'].apply(str)
  • Related