Converting string into DateTime in Python
The goal is to convert a date string like "2021/05/25" into a Python-recognized DateTime object such as 2021-05-25 00:00:00. This enables accurate and consistent date operations like comparisons, calculations and formatting when working with time-related data from sources like files or user input. Let's understand how to do this efficiently.
Using dateutil.parser.parse()
parse() function from the dateutil library automatically detects and converts a wide range of date string formats into a datetime object. This method is ideal when your input dates are inconsistent or come from user input or APIs.
from dateutil.parser import parse
s = '2023-07-25'
res = parse(s)
print(res)
Output
2023-07-25 00:00:00
Explanation: Here, we passed the date string '2023-07-25' to parse(). It automatically recognized the format and returned a full datetime object.
Using datetime.strptime()
datetime.strptime() method, part of Python's datetime module, efficiently converts a date string into a DateTime object when the exact format is known, requiring a format specification like '%Y/%m/%d'.
import datetime
s = '2021/05/25'
format = '%Y/%m/%d'
res = datetime.datetime.strptime(s, format)
print(res)
Output
2021-05-25 00:00:00
Explanation: We define the exact format (%Y/%m/%d) for the input string '2021/05/25' and strptime() converts it into a datetime object.
Using pandas.to_datetime()
For large datasets, especially in CSV or Excel format, pandas.to_datetime() efficiently converts multiple date strings into DateTime objects, handling various formats, missing values, and errors.
import pandas as pd
s = ['2021-05-25', '2020/05/25', '2019/02/15']
res = pd.to_datetime(s, format='mixed')
print(res)
Output
DatetimeIndex(['2021-05-25', '2020-05-25', '2019-02-15'], dtype='datetime64[ns]', freq=None)
Explanation: pandas.to_datetime() automatically detects the correct format for each date in the list using format='mixed'. This avoids format mismatch errors.
Using datetime.date()
For cases where you only need the date (without time), you can use datetime.strptime() followed by date() to convert the string into a DateTime object and extract the date. This method is still quite efficient but slightly more verbose than parse() or to_datetime().
import datetime
s = '2021/05/25'
format = '%Y/%m/%d'
res = datetime.datetime.strptime(s, format).date()
print(res)
Output
2021-05-25
Explanation: After converting the string to a datetime object, we call .date() to extract just the date.
Similar Reads: