Using regex to cut string into ['year', 'month', 'day', 'hour', 'minutes', 'seconds'] then unpack it and fill into datetime class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0) , this is the fastest way I tested so far.
import re import pandas as pd import datetime import timeit def date2timestamp_anyformat(format_date): numbers = ''.join(re.findall(r'\d+', format_date)) if len(numbers) == 8: d = datetime.datetime(int(numbers[:4]), int(numbers[4:6]), int(numbers[6:8])) elif len(numbers) == 14: d = datetime.datetime(int(numbers[:4]), int(numbers[4:6]), int(numbers[6:8]), int(numbers[8:10]), int(numbers[10:12]), int(numbers[12:14])) elif len(numbers) > 14: d = datetime.datetime(int(numbers[:4]), int(numbers[4:6]), int(numbers[6:8]), int(numbers[8:10]), int(numbers[10:12]), int(numbers[12:14]), microsecond=1000*int(numbers[14:])) else: raise AssertionError(f'length not match:{format_date}') return d.timestamp()
and speed test:
print('regex cut:\n',timeit.timeit(lambda: datetime.datetime(*map(int, re.split('-|:|\s', '2022-08-13 12:23:44.234')[:-1])).timestamp(), number=10000)) print('pandas to_datetime:\n', timeit.timeit(lambda: pd.to_datetime('2022-08-13 12:23:44.234').timestamp(), number=10000)) print('datetime with known format:\n',timeit.timeit(lambda: datetime.datetime.strptime('2022-08-13 12:23:44.234', '%Y-%m-%d %H:%M:%S.%f').timestamp(), number=10000)) print('regex get number first:\n',timeit.timeit(lambda: date2timestamp_anyformat('2022-08-13 12:23:44.234'), number=10000)) print('dateutil parse:\n', timeit.timeit(lambda: parser.parse('2022-08-13 12:23:44.234').timestamp(), number=10000))
result:
regex cut: 0.040550945326685905 pandas to_datetime: 0.8012433210387826 datetime with known format: 0.09105705469846725 regex get number first: 0.04557646345347166 dateutil parse: 0.6404162347316742
dateutilwould be an unreliable choice.