Converting datetime format in Pandas column and separating it into two columns

I was practicing a problem along with the pandas tutorial. However, I got stuck at this particular problem:

Does anyone know how to convert the following date-time format in the pic below? I have tried:
df[‘completed_at’] ="%Y-%m-%dT%H:%M:%SZ")
However, it does not work.

Essentially I would like to separate the column into two separate columns: ‘Date’ and ‘Time’:

  1. df[‘Dates’] = pd. to_datetime(df[‘completed_at’])
  2. df[‘Time’] = pd. to_datetime(df[‘completed_at’]).dt.time.
    This would not work without fixing the date-time format.

Any help would be appreciated. Thankyou! :grinning_face_with_smiling_eyes:


I found it easier to do it with regex since we need to extract the date and time from a string. here’s my attempt on that

import pandas as pd
import re

date = {'completed_at': ['2021-12-01T20:53:17+01:00']}
df = pd.DataFrame(data=date)


date_pattern = re.compile(r'(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}\+\d{2}:\d{2})')
matches = date_pattern.finditer(df['completed_at'].loc[0])

dates = []
time = []

for match in matches:

print(dates, time)

it will print ['2021-12-01'] ['20:53:17+01:00']

1 Like

Here is youtube video that i see useful to learn regex

1 Like