"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Parse Custom Date Formats in CSV Files Using Pandas?

How to Parse Custom Date Formats in CSV Files Using Pandas?

Published on 2024-11-03
Browse:630

How to Parse Custom Date Formats in CSV Files Using Pandas?

Pandas Automates Date Parsing in CSV Files

Pandas simplifies data retrieval from CSV files with its capability to automatically infer data types, including dates. However, it occasionally fails to recognize specific date formats, such as those presented as "2013-6-4."

Solution: Specify 'parse_dates' Argument

To overcome this challenge, utilize the 'parse_dates' argument. For instance, to designate a column with dates in the "YYYY-MM-DD" format as 'datetime' objects, execute the following:

df = pandas.read_csv('test.dat', parse_dates=['datetime'], delimiter=r"\s ", names=['col1','col2','col3'])

This will convert the relevant column into 'datetime' objects.

Advanced Customization: Date Parser Functions

For more intricate date formats, employ date parser functions. These grant greater flexibility in specifying custom parsing logic. For instance, consider a 'datetime' column with the format "YYYY-MM-DD HH:MM:SS":

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)

You can even merge multiple date-related columns into a single 'datetime' column:

dateparse = lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
df = pd.read_csv(infile, parse_dates={'datetime': ['date', 'time']}, date_parser=dateparse)

Browse the documentation for 'strptime' for directives representing various datetime formats.

Release Statement This article is reprinted at: 1729206855 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3