2nd Guided Project: Hacker News Analysis - datetime issue

Screen Link:

My Code:

import datetime as dt

# need to find the date the post was created and the amt of post comments attached to post, then append these two values to the result_list. 
result_list = []
for row in ask_posts:

# now that the result_list is created, we can find out how many posts are created at each hour and how many comments per hour.
counts_by_hour = {}
comments_by_hour = {}
date_format = "%m/%d/%y %H:%M"
for row in result_list:
    date_string = row[0]
    comments_count = row[1]
    date_created = dt.datetime.strptime(date_string, date_format)
    hour_created = date_created.hour
    if hour_created in counts_by_hour:
        counts_by_hour[hour_created] += 1
        comments_by_hour[hour_created] += comments_count 
        counts_by_hour[hour_created] = 1
        comments_by_hour[hour_created] = comments_count

What I expected to happen:
Expected to populate the counts_by_hour and comments_by_hour empty dictionaries.

What actually happened:

ValueError                                Traceback (most recent call last)
<ipython-input-12-ad2f121ca4f3> in <module>
     14     date_string = row[0]
     15     comments_count = row[1]
---> 16     date_created = dt.datetime.strptime(date_string, date_format)
     17     hour_created = date_created.hour
     18     if hour_created in counts_by_hour:

~\anaconda3\lib\_strptime.py in _strptime_datetime(cls, data_string, format)
    566     """Return a class cls instance based on the input string and the
    567     format string."""
--> 568     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    569     tzname, gmtoff = tt[-2:]
    570     args = tt[:6] + (fraction,)

~\anaconda3\lib\_strptime.py in _strptime(data_string, format)
    347     found = format_regex.match(data_string)
    348     if not found:
--> 349         raise ValueError("time data %r does not match format %r" %
    350                          (data_string, format))
    351     if len(data_string) != found.end():

ValueError: time data '9' does not match format '%m/%d/%y %H:%M'

Unsure why my code isn’t working. The error prompt suggests that it has to do with the date format but I can’t find the issue.

Any help would be greatly appreciated! Thanks

1 Like

It looks like the error is being generated because of some faulty datetime data stored in results_list that doesn’t match the strftime syntax "%m/%d/%y %H:%M". I believe you will need to find and remove this row from results_list.

Can you please provide a link to the guided project to make it easier to experiment with your code?

Hi @jerome.landicho92

I think the problems is in your result_list, let’s start from the point where you have done the iteration and appended the values in row[6] and row[4] contained in the ask_posts.

result_list = []
for row in ask_posts:

if you ran the code lines above, the first iteration takes in the value stored in the first row in the 6th index(row[6]) of ask_posts and append it to result_listand at the same time it append the value in first row the 4th index(row[4]) in sequential order and the process continues until all the iteration is achieved. So you will end up with a list of values, that is values in ‘created_at’ and ‘num_comments’ columns and NOT List of lists because this line of code;

for row in result_list:

suggest that that result_list is a list of list, which is not the case, remember you only appended the values in index 6 and index 4 to the list and you will therefore end up with a list of values. Below is the display contained in your list, that is after doing some slicing result_list[0:100]

To solve the problem, you will need to append the values in index 6 and index 4 of ask_post as one list for every iteration. please make the changes to match the code below.

result_list = []

for row in ask_posts:
    created_at = row[6]
    num_comments = int(row[4])
    result_list.append([created_at, num_comments])

let me know if it worked for you :heart_eyes:.

Happy codding!

Hi @brayanopiyo18,

Thank you for the response! I have tried to use your solution, however when I input the code, it still comes up with an error prompt as per below. I can’t seem to find the issue because the date format is correct. Any further help would be greatly appreciated!

1 Like

Hi @jerome.landicho92
I think the error arise because of the data format you have used, The correct format is "%m/%d/%Y %H:%M"( the year should be in uppercase and not lower case). Actually I did not concentrate that much on the date format on my previous submission and with this "%m/%d/%y %H:%M" , you will never much the year category, can you please fix the changes and let me know if it worked.