Skip to content Skip to sidebar Skip to footer

Convert List Of Datestrings To Datetime Very Slow With Python Strptime

I have data files containing lists of strings representing ISO formatted dates. Currently, I am reading them in using: mydates = [ datetime.datetime.strptime(timdata[x], '%Y-%m-%dT

Solution 1:

Here is a way to do it about 3x faster.

The original version:

In [23]: %timeit datetime.datetime.strptime("2013-01-01T01:23:45", "%Y-%m-%dT%H:%M:%S")
10000 loops, best of 3: 21.8 us per loop

The faster version:

In [24]: p = re.compile('[-T:]')

In [26]: %timeit datetime.datetime(*map(int, p.split("2013-01-01T01:23:45")))
100000 loops, best of 3: 7.28 us per loop

This is obviously nowhere near as flexible as strptime().

edit: Using a single regex to extract the date components is marginally faster:

In [48]: pp = re.compile(r'(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})')

In [49]: %timeit datetime.datetime(*map(int, pp.match("2013-01-01T01:23:45").groups()))
100000 loops, best of 3: 6.92 us per loop

Solution 2:

Indexing / slicing seems to be faster than the regex used by @NPE:

In [47]: def with_indexing(dstr):                              
   ....:     return datetime.datetime(*map(int, [dstr[:4], dstr[5:7], dstr[8:10],
   ....:                               dstr[11:13], dstr[14:16], dstr[17:]])) 

In [48]: p = re.compile('[-T:]')

In [49]: def with_regex(dt_str):
   ....:     return datetime.datetime(*map(int, p.split(dt_str)))

In [50]: %timeit with_regex(dstr)
100000 loops, best of 3: 3.84 us per loop

In [51]: %timeit with_indexing(dstr)
100000 loops, best of 3: 2.98 us per loop

I think if you would use a file parser like numpy.genfromtxt, the converters argument and a fast string parsing method you can read and parse a whole file in less than a half second.

I used the following function to create an example file with about 25000 rows, ISO date strings as index and 10 data columns:

import numpy as np
import pandas as pd

defcreate_data():
    # create dates
    dates = pd.date_range('2010-01-01T00:30', '2013-01-04T23:30', freq='H')
    # convert to iso
    iso_dates = dates.map(lambda x: x.strftime('%Y-%m-%dT%H:%M:%S'))
    # create data
    data = pd.DataFrame(np.random.random((iso_dates.size, 10)) * 100,
                        index=iso_dates)
    # write to file
    data.to_csv('dates.csv', header=False)

Than I used the following code to parse the file:

In [54]: %timeit a = np.genfromtxt('dates.csv', delimiter=',',
                                   converters={0:with_regex})
1 loops, best of 3: 430 ms per loop

In [55]: %timeit a = np.genfromtxt('dates.csv', delimiter=',',
                                   converters={0:with_indexing})
1 loops, best of 3: 391 ms per loop

pandas (based on numpy) has a C-based file parser which is even faster:

In [56]: %timeit df = pd.read_csv('dates.csv', header=None, index_col=0, 
                                  parse_dates=True, date_parser=with_indexing)
10 loops, best of3: 167 ms per loop

Post a Comment for "Convert List Of Datestrings To Datetime Very Slow With Python Strptime"