Sometimes, we want to automatically read dates from a CSV file with Python Pandas.
In this article, we’ll look at how to automatically read dates from a CSV file with Python Pandas.
How to automatically read dates from a CSV file with Python Pandas?
To automatically read dates from a CSV file with Python Pandas, we can set the date_parser
argument.
For instance, we write
from datetime import datetime
dateparse = lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
df = pd.read_csv(infile, parse_dates=['datetime'], date_parser=dateparse)
to call read_csv
with the file to read.
And we set parse_dates
to 'datetime'
to parse dates with datetime
.
Then we specify the date_parser
argument by setting it to the dateparse
functuon we created to parse the dates with the given format.
Conclusion
To automatically read dates from a CSV file with Python Pandas, we can set the date_parser
argument.