Type error has me confused

Screen Link:

My Code:

import datetime as dt
result_list = []

for row in ask_posts:
    post = []
    created_at = row[6]
    num_comments = row[4]

counts_by_hour = {}
comments_by_hour = {}

for row in result_list:
    created_hour_dt = dt.datetime.strptime(row[0], '%m/%d/%Y %H:%M')
    row[0] = created_hour_dt
    comment_hour = row[0]
    comment_hour = dt.datetime.strftime("%H")
    if comment_hour not in counts_by_hour:
        counts_by_hour[comment_hour] = 1
        comments_by_hour[comment_hour] = row[1]
        counts_by_hour[comment_hour] += 1
        comments_by_hour[comment_hour] += row[1]

What I expected to happen:
Exract just the hour from a datetime object, then create dictionaries based on the value of that hour plus another element in the data set (comment count)

What actually happened:

<class 'datetime.datetime'>

TypeErrorTraceback (most recent call last)
<ipython-input-5-d5f085f9c36f> in <module>()
     20     print(type(row[0]))
     21     comment_hour = row[0]
---> 22     comment_hour = dt.datetime.strftime("%H")
     23     if comment_hour not in counts_by_hour:
     24         counts_by_hour[comment_hour] = 1

TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'

Other details:

I added the print(type(row[0])) because I thought I was going mad. I wanted to assure the row/record I was working with was in fact a datetime object. Which it is, however it’s ‘datetime.datetime,’ and not ‘datetime.date’
Still, it’s definitely not type ‘str’ so the error confuses me.

Any help is appreciated!

1 Like

Hi @neelblair,

First, you have to convert the number of comments to an integer, as it was told in the task:
num_comments = int(post[4])

Now about your error. You should use ‘strftime’ directly on your ‘created_hour_dt object’:
comment_hour = created_hour_dt.strftime("%H")

You can even combine ‘strptime’ and ‘strftime’ in one-line code:
comment_hour = dt.datetime.strptime(row[0], "%m/%d/%Y %H:%M").strftime("%H")


I saw that combined strptime and strftime syntax in another post, but that wasn’t the way I’d learned it earlier in the lesson. I’ll go back and look at how exactly that works.

So despite the fact that the row shows (via the print(type()) function) datetime.datetime, the issue is that the data stored in that datetime.datetime object is str data and not int data?

Do I have it right? I assumed the “type()” function would confirm, but all it did was confirm that the OBJECT was datetime.datetime, not necessarily the data IN the object.

The dt.datetime.strptime() is a constructor that returns a datetime object (i.e., the object of the class ‘datetime.datetime’). The objects of this class have a method datetime.strftime(), which turns the datetime object into a string.

Try this code:
for row in result_list:
created_hour_dt = dt.datetime.strptime(row[0], "%m/%d/%Y %H:%M")
comment_hour = created_hour_dt.strftime("%H")

Unfortunately, I don’t know how to do here an indentation in the for loop :slightly_smiling_face:

1 Like

I got it to work, thanks. I didn’t do it exactly like the above. I am still cementing the class-object structure in my mind. I think I had a misconception about where strftime works in the hierarchy. I’m straight on that now.


Hello @Elena_Kosourova,

You can refer this guide for markdown

5. Use Markdown to style your question.

Markdown is a very handy syntax to learn. It’s catching up a lot of traction and more and more forums are implementing this. Therefore, it would help you not only in this community but also with other forums on the internet. If you are unfamiliar with Markdown, I would recommend you this cool interactive markdown tutorial . Or if you just need a refresher, check out this article.

Full Guideline - here

1 Like

Hi @DishinGoyani,

Thanks a lot, it helped!
I found the answer, as well as some other useful lifehacks.

1 Like