Error "time data 'appt_start_date' does not match format"

Hello!
I’m stucked in a challenge proposed in the end of the mission “Working With Dates and Times in Python”, as part of Course 2/2 in the Step1 of Data Scientist In Python Path.

https://app.dataquest.io/m/353/working-with-dates-and-times-in-python/11/next-steps

My Code:

#creating a function to define the lenght of the meeting:
def lenght_meeting(start_time, end_time):
    start_time = dt.datetime.strptime(start_time, "%-m/%-d/%y %H:%M")
    end_time = dt.datetime.strptime(end_time, "%-m/%-d/%y %H:%M")
    lenght = end_time-start_time
    return lenght

visitors = {}

for row in potus:
    
    #finding the visitor name and the lenght of the meeting
    visitor = row[0]
    lenght = lenght_meeting(row[2], row[3])
    
    #creating a table with the visitor and the amount of time he spent in the WH
    if row[0] not in visitors:
        visitors[row[0]] = lenght
    else:
        visitors[row[0]] += lenght

What I expected to happen:
I expected the function lenght_meeting to work to define the lenght of the meeting in every iteration inside the loop.

What actually happened:

ValueError: '-' is a bad directive in format '%-m/%-d/%y %H:%M'

I can’t understand why is Python telling me that, since this is exaclty the format suggested by the link https://strftime.org/ that was given during the mission, considering that the dates are stored in this format: 8/7/15 9:30. Day with one digit, month with one digit, years with two digits and so on.

When I remove the " - " symbols, another error is raised: ValueError: time data ‘appt_start_date’ does not match format ‘%m/%d/%y %H:%M’.

So I dont’t know what to do. I tried several configurations for the format inside the striptime in the lenght_meeting function, but nothing seems to work. Python will always find something to complain, and this is not making sense to me… probably because I’m not being able to find out what is wrong.

I would really appreciate some help here.

Thanks in advance!
Paulo

row[2] is already datetime object you don’t need to convert it again.

And Second

Remove hyphen (-) from %-m/%-d/%y %H:%M
end_time = dt.datetime.strptime(end_time, "%m/%d/%y %H:%M")
This works fine for me even for single digit month and day. Don’t Need to add hyphen (-) here. May be it was required for older version.

The link you provided does not give the required source. But based on the information you provided, it seams the format you are providing does not match the date format.

I tried to solve the exercise outside the dataquest enviroment in Anaconda Jupyter Notebook, this is why I converted it again. I’ve already tried to use your suggestion for the format, but it didn’t work.

That’s weird, I posted the link to the exact screen were the extended challenge was given.
https://app.dataquest.io/m/353/working-with-dates-and-times-in-python/11/next-steps

Also, the format I’m providing matches exactly with the one in the column to be converted (I have this example from the dataset: 8/7/15 9:30) and also with the strftime reference provided during the mission: https://strftime.org/.

I tried to change the formats but none ot them have worked. I’m still stucked.

Anyway, thanks for your help, guys!

I love a good troubleshooting challenge so I decided to try out your problem…and I think I got it to work! I got many of the errors you described above. I took them on, one at a time, commented out lines of code, tried again, more errors, inspected more variables, tried again, more errors, tried a few print(type(some_object)), more errors…eventually I got your code to run without errors and I think it works! Let me know what you think:

def lenght_meeting(start_time, end_time):
    # start_time = dt.datetime.strptime(start_time, "%-m/%-d/%y %H:%M")
    end_time = dt.datetime.strptime(end_time, "%m/%d/%y %H:%M")
    lenght = end_time - start_time
    return lenght

visitors = {}

for row in potus:
    
    #finding the visitor name and the lenght of the meeting
    visitor = row[0]
    lenght = lenght_meeting(row[2], row[3])
    
    #creating a table with the visitor and the amount of time he spent in the WH
    if row[0] not in visitors:
        visitors[row[0]] = lenght
    else:
        visitors[row[0]] += lenght

The big things I ran into were :

  • row[2] contains a datetime object already so I commented out that line of code. I understand that porting over a local Jupyter notebook could cause you to need this code, but just testing it out in DQ, I did not need it so I removed it for troubleshooting purposes
  • row[3] is a str and was throwing errors when trying to do a subtraction between datetime object and a str. In other words, we do need to convert those to a datetime object
  • "%-m/%-d/%y %H:%M" was definitely causing a problem because of the dashes. As you can see in the code above, I removed those : "%m/%d/%y %H:%M"

But now that I’m here…I can see that the objects created from this code aren’t quite like the ones I managed to produce when I did this mission…and after looking at it for a few mins, I can see why!

Inside your for loop, your conditional statement used to build your dictionary is a little off from the mission objective. The task is to build a dictionary that ‘counts’ the number of visits that last a particular length of time…in other words, a frequency table of meeting length times. Your for loop is building a dictionary that is storing the visitors name and the total sum of their meeting times. For example, your dictionary would like this:
{
‘Shiquan ■■■■’ : 16:29:00
‘Darius M. Slade’ : 11:59:00
‘Victor n. Wu’ : 6:59:00
}

Now that your code runs without error, I leave it to you to tweak that last part! :sunglasses:

P.S. - Looks like the forums are using a “nasty language” filter…It’s not Shiquan's fault their last name is a slang term for… I’ll leave that as a bonus question for you @phssaraiva