October 22, 2024
Chicago 12, Melborne City, USA
python

how to convert string datetime representation to datetime field in pandas?


I have a csv , with text data (sample below) .

id   |  name     |   date                     
    
123 |   abc   |  2024-08-05T21:00:26:074Z 
456 |  efg    |  2024-08-05T21:34:30.502z 

I want to filter the data based on date field , for this , i tried to read/convert datetime string to date field but get , following error => this throws an error that time data ‘2024-08-05T21:34:30.502Z" doesn’t match format “%Y-%m-%dT%H:%M:%S%z” , at position …

Question – i really don’t need the time part of this date field . is there a way to ignore this and convert the rest to date field?

import pandas as pd

df = pd.read_csv('test.csv')

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

filtered_df = df[df['formatted_date'] > '2024-08-05']



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video