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."
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.
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.
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