In Python, the pandas library includes built-in functionalities that allow you to perform different tasks with only a few lines of code. One of these functionalities is the conversion of a pandas series of date-strings to a timeseries.
mapIn this method, we first initialize a pandas series. Then, we use the dateutil library to parse the input string format to represent a datetime format. Then, we map this converted format to the input pandas series.
#importing pandas and dateutil librariesimport pandas as pdfrom dateutil.parser import parse as pr#initializing date-string pandas seriesseries = pd.Series(['12 July 2021', '04-01-2001', '20020101', '2000/09/09', '2020-05-18', '2021-08-08T14:35'])#parsing and mapping the date-strings to timeseriesresult = series.map(lambda iterator: pr(iterator))print(result)
to_datetimeIn this method, we first initialize a pandas series. Then we use the to_datetime() function of pandas to convert the given date-strings into the respective timeseries.
#importing pandas libraryimport pandas as pd#initializing date-string pandas seriesseries = pd.Series(['12 July 2021', '04-01-2001', '20020101', '2000/09/09', '2020-05-18', '2021-08-08T14:35'])#converting date-strings to timeseriesresult = pd.to_datetime(series)print(result)