Search code examples
pythondatetimeextracthour

TypeError: 'int' object is not callable for datetime


Trying to apply the hour operation on the dt_obj object. I was expecting to extract the hour from dt_obj which is a string. But I guess that dt_obj is the wrong type to be utilising the operation hour() on.

Excuse my language, I'm quite new, don't really know my types that well. And I think it's because I'm trying to do the hour() operation on the wrong type that I'm getting an int object is not callable error.

So what do I need to do in order to extract the hour from dt_obj?

date_format = '%m/%d/%Y %H:%M' print(date_format) import datetime as dt

`for row in result_list:
    time = row[0]
    print(time)
    dt_obj = dt.datetime.strptime(time, date_format)
    print(type(dt_obj))
    t_hour = dt_obj.hour()
    print(t_hour)



%m/%d/%Y %H:%M
8/16/2016 9:55
<class 'datetime.datetime'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-70-003a1e9f2cc7> in <module>
      8     dt_obj = dt.datetime.strptime(time, date_format)
      9     print(type(dt_obj))
---> 10     t_hour = dt_obj.hour()
     11     print(t_hour)

TypeError: 'int' object is not callable

Should be able to apply hour to the dt_obj to extra the hour but I think it's the wrong type of         object to apply it to.`

Solution

  • All it is saying dt_obj.hour is an integer, not a callable (i.e., function). Remove the parenthesis at the end

    # Replace t_hour = dt_obj.hour()
    t_hour = dt_obj.hour