Home > Mobile >  Extract utc format for datetime object in a new Python column
Extract utc format for datetime object in a new Python column

Time:04-18

Be the following pandas DataFrame:

|      ID      |     date                              |
|--------------|---------------------------------------|
|     0        |        2022-03-02 18:00:20 01:00      |
|     0        |        2022-03-12 17:08:30 01:00      |
|     1        |        2022-04-23 12:11:50 01:00      |
|     1        |        2022-04-04 10:15:11 01:00      |
|     2        |        2022-04-07 08:24:19 02:00      |
|     3        |        2022-04-11 02:33:22 02:00      |

I want to separate the date column into two columns, one for the date in the format "yyyy-mm-dd" and one for the time in the format "hh:mm:ss tmz".

That is, I want to get the following resulting DataFrame:

|      ID      |     date_only           | time_only      |
|--------------|-------------------------|----------------|
|     0        |        2022-03-02       | 18:00:20 01:00 |
|     0        |        2022-03-12       | 17:08:30 01:00 |
|     1        |        2022-04-23       | 12:11:50 01:00 |
|     1        |        2022-04-04       | 10:15:11 01:00 |
|     2        |        2022-04-07       | 08:24:19 02:00 |
|     3        |        2022-04-11       | 02:33:22 02:00 |

Right now I am using the following code, but it does not return the time with utc hh:mm.

df['date_only'] = df['date'].apply(lambda a: a.date())
df['time_only'] = df['date'].apply(lambda a: a.time())
|      ID      |     date_only           |time_only |
|--------------|-------------------------|----------|
|     0        |        2022-03-02       | 18:00:20 |
|     0        |        2022-03-12       | 17:08:30 |
|    ...       |        ...              |  ...     |
|     3        |        2022-04-11       | 02:33:22 |

I hope you can help me, thank you in advance.

CodePudding user response:

Convert column to datetimes and then extract Series.dt.date and times with timezones by Series.dt.strftime:

df['date'] = pd.to_datetime(df['date'])

df['date_only'] = df['date'].dt.date
df['time_only'] = df['date'].dt.strftime('%H:%M:%S%z')

Or split converted values to strings by space and select second lists:

df['date'] = pd.to_datetime(df['date'])

df['date_only'] = df['date'].dt.date
df['time_only'] = df['date'].astype(str).str.split().str[1]
  • Related