Suppose I have following data with data formats:
| date | time |
|---|---|
| 2022-04-21 | 610 |
| 2022-04-22 | 610 |
| 2022-04-22 | 1751 |
date: object
time: int32
I wish to combine date column and time column to make new column 'datetime' with datetime format like this:
| date | time | datetime |
|---|---|---|
| 2022-04-21 | 610 | 2022-04-21 6:10 |
| 2022-04-22 | 610 | 2022-04-22 7:01 |
| 2022-04-22 | 1751 | 2022-04-22 17:51 |
When I use following codes, I get errors as following:
Converting object to datetime
df['date'] = pd.to_datetime(df['date'], format='%Y-%M-%d')
| date | time |
|---|---|
| 2022-01-21 00:04:00 | 610 |
| 2022-01-22 00:04:00 | 610 |
| 2022-01-22 00:04:00 | 1751 |
Converting int32 to datetime
df_trade['time'] = pd.to_datetime(df_trade['time'], format='%H:%M')
ValueError: time data '610' does not match format '%H:%M' (match)
I guess the last step must be easy with following code, but columns are not in the right format to combine.
df_trade['datetime'] = df_trade['date'].astype(str)+" "+df_trade['time'].astype(str)
How do I convert the object, int32 columns into datetime format without changing the contents?