Datetime class problem : shouldn't I pass writing start-date code?

Screen Link:
https://app.dataquest.io/m/403/working-with-dates-and-times-in-python/10/summarizing-appointment-lengths

My Code:


for row in potus:
    end_date = row[3]
    end_date = dt.datetime.strptime(end_date, "%m/%d/%y %H:%M")
    row[3] = end_date

appt_lengths = {} 

# And Here is β˜…

for row in potus :     
    start_date = row[2]
    start_date = dt.datetime.strptime(start_date, "%m/%d/%y %H:%M")
    row[2] = start_data

    
for row in potus : 
    length = end_date - start_date
    if length not in appt_lengths : 
        appt_lengths[length] = 1 
    else : 
        appt_lengths[length] +=1 

min_length = min(appt_lengths)
max_length = max(appt_lengths)


#Answer part 
for row in potus:
    end_date = row[3]
    end_date = dt.datetime.strptime(end_date, "%m/%d/%y %H:%M")
    row[3] = end_date
appt_lengths = {}

for row in potus:
    start_date = row[2]
    end_date = row[3]
    length = end_date - start_date
    if length not in appt_lengths:
        appt_lengths[length] = 1
    else:
        appt_lengths[length] += 1

min_length = min(appt_lengths)
max_length = max(appt_lengths)


What I expected to happen:

#Here isβ˜… , I just want to be accurate with start_date, like we make definition on end_date, so I just write down the same strptime code for it.

What actually happened:

But I got a message like this : 
TypeError: must be str, not datetime.datetime

So what makes the difference for the system.
1 Like

Welcome to our community @diaderym, :hugs:

The error message you are seeing :

TypeError: must be str, not datetime.datetime

And it is telling us that the column you are trying to convert to datetime is already converted !!
this is happening because the system (the dataquest system) has the column already converted for you !!


you can check on that yourself by :

type(potus[0][2])

this checks the type of start_date of the first β€˜row’ of the POTUS list, which will give you:

datetime.datetime

Already converted :smile:


On contrast, checking the type of end_date :

type(potus[0][3])

will give you:

str

so, its not converted yet . :smile:


Hope this helps, if it did don’t forget to select it as your answer :grin:

2 Likes

Thanks :smiling_face_with_three_hearts: :smiling_face_with_three_hearts:

1 Like