Search code examples
pythonnumpystrptime

Cannot parse datetime using strptime


I am trying to load data from csv and display it in a matplotlib scatter graph, where on X axis I have formatted datetime. This is the data:

0,03/12/2017 21:00:00.000 +0000,4745
0,03/12/2017 22:00:00.000 +0000,3046
0,03/12/2017 23:00:00.000 +0000,2052
0,03/13/2017 00:00:00.000 +0000,1455
2,03/13/2017 00:00:00.000 +0000,2
1,03/13/2017 00:00:00.000 +0000,2

And the Python3.4 code I use:

import numpy as np
import matplotlib.pyplot as plt
import datetime as dt

retries, count = np.loadtxt(open('search-results.csv', 'r'),
                  delimiter=",",
                  skiprows=1,
                  unpack=True,
                  usecols=[0, 2])

time = np.loadtxt(open('search-results.csv', 'r'),
                  delimiter=",",
                  skiprows=1,
                  unpack=True,
                  usecols=[1],
                  dtype=str)

dates = [dt.datetime.strptime(str(d), "b'%d/%m/%Y %H:%M:%S.000 +0000'") for d in time]

plt.scatter(dates, retries, s=count)
plt.grid(which='both', color='#aaaaaa')
plt.savefig('out.png', format='png')

When I run the above code it looks like it is parsing the data until it reaches day 13:

ValueError: time data "b'03/13/2017 00:00:00.000 +0000'" does not match format "b'%d/%m/%Y %H:%M:%S.000 +0000'"

Solution

  • TL;DR:

    You should change the following line from this:

    dates = [dt.datetime.strptime(str(d), "b'%d/%m/%Y %H:%M:%S.000 +0000'") for d in time]
    

    to this:

    dates = [dt.datetime.strptime(str(d), "b'%m/%d/%Y %H:%M:%S.000 +0000'") for d in time]
    

    Well the error you are getting is correct, because you are telling your code to expect a date, formated as: "b'%d/%m/%Y %H:%M:%S.000 +0000'" but you are passing it a date formated like this:

    "b'%m/%d/%Y %H:%M:%S.000 +0000'" (interchanged month and date).
    

    Your code works for the first 3 lines because 12 is in the Month range of a Year, but when it gets 13 on the 4th line it breaks!

    Good luck :)