1. Help Center
  2. Python Programming

While importing data from different sources, can the pandas library recognize dates?

Yes, they can, but with some bit of help. We need to add the parse_dates argument while we are reading data from the sources. Consider an example where we read data from a CSV file, we may encounter different date-time formats that are not readable by the pandas library. In this case, pandas provide flexibility to build our custom date parser with the help of lambda functions as shown below:
 
import pandas as pd

from datetime import datetime

dateparser = lambda date_val: datetime.strptime(date_val, '%Y-%m-%d %H:%M:%S')

df = pd.read_csv("some_file.csv", parse_dates=['datetime_column'], date_parser=dateparser)