Search code examples
datetimedate-formatting

Python ValueError: time data does not match format ' %H:%M %d/%m/%Y'


I am continually getting this string formatting error, but I cannot find what doesn't match properly. I've tried removing the leading space from the format string, but that does not make a difference. I'm using Python 3.5 on OSX 10.11.5

ValueError: time data ' 00:59 03/20/1978' does not match format ' %H:%M %d/%m/%Y'

The function call, in case there's a culprit farther back:

def bytedate2num(fmt):
    def converter(b):
        return mdates.strpdate2num(fmt)(b.decode('ascii'))
    return converter

date_converter = bytedate2num(' %H:%M %d/%m/%Y')

def get_events(fname):
    events = []
    events.append(np.loadtxt(fname, comments = '#', delimiter='\t', converters = {0 : date_converter}))
    return events     

Any suggestions are much appreciated, since this function is responsible for gathering all the data to analyze.


Solution

  • According to the error:

    ValueError: time data ' 00:59 03/20/1978' does not match format ' %H:%M %d/%m/%Y'
    

    You are trying to use the wrong date format. Specifically, according to this, it is trying to parse the 20 as a month (%m). I think the date format you want is ' %H:%M %m/%d/%Y' (notice that the %m and %d have been switched).