Question

This error is generated:

ValueError: time data '11/1/13' does not match format '%m/%d/%Y'

An example of my data. (Format- Month,Day,Year-value)

12/27/13,311
12/28/13,283
12/29/13,285
12/30/13,285
12/31/13,252
1/1/14,245
1/2/14,245
1/3/14,245
1/4/14,245
1/5/14,246

The code I have, collected on another SOF post.

import csv
import datetime as dt
import matplotlib.pyplot as plt

arch = 'test.csv'
data = csv.reader(open(arch))

data = [(dt.datetime.strptime(item, "%m/%d/%Y"), float(value)) for item, value in data]
data.sort()
[x, y] = zip(*data)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)

ax.grid(True)
fig.autofmt_xdate()

plt.show()

Why is my date format throwing an error?

Was it helpful?

Solution

You want

dt.datetime.strptime(item, "%m/%d/%y")

From the docs, years with centuries (1999, 2000, etc.) are formatted with '%Y'. For years without centuries (and which are 0 padded for single digit years, like 09, 99, 13), you want to use '%y' (lowercase y).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top