Datetime.strptime issue

I’m working on screen 5, Using Strptime to Parse Strings as Dates, of the Working with Dates and Times in Python mission and getting a result that doesn’t look correct but still passes the autograder.

I input the following code:

import datetime as dt
date_format = "%m/%d/%y %H:%M"

for row in potus:
    start = row[2]
    start = dt.datetime.strptime(start, date_format)
    row[2] = start
print(potus[:2], "\n")

The result is:
[[‘Joshua T. Blanton’, ‘2014-12-18T00:00:00’, datetime.datetime(2015, 1, 6, 9, 30), ‘1/6/15 23:59’, ‘’, ‘potus’, ‘west wing’, ‘JointService Military Honor Guard’], [‘Jack T. Gutting’, ‘2014-12-18T00:00:00’, datetime.datetime(2015, 1, 6, 9, 30), ‘1/6/15 23:59’, ‘’, ‘potus’, ‘west wing’, ‘JointService Military Honor Guard’]]

As you can see, the resulting date is not returning as expected.
Anyone have an idea of what is happening? As I mentioned, the autograder is accepting the results as correct.

1 Like

Hey, Tmoesabi.

Your solution is correct. Try this:

date_format = "%m/%d/%y %H:%M"
for row in potus:
    start_date = row[2]
    start_date = dt.datetime.strptime(start_date, date_format)
    row[2] = start_date
print(potus[:2], "\n")
print(potus[0][2], potus[1][2], sep="\n")

The difference in the format is probably a design choice by Python’s developers.

This is a related post that doesn’t explain the different formats here, but it’s useful information and certainly a step in the right direction to explain this.

Thank you Bruno.
From the code and link you provided, I gather that the issue is simply related to the way Python displays a datetime object within a displayed list (such as print(potus[:2]) ) versus the way it is displayed when we select the individual object from the list ( such as print(potus[0][2]).
This was just an unexpected result, as there was no mention of this “weirdness” in the lesson body. I was expecting to see the formatted date when I looked at the first couple rows of the podus data.
Thanks again!

1 Like