1
votes

I have a .txt file with a list of dictionaries, where one entry in each dictionary is a time difference in the format "datetime.timedelta(days, seconds). I am trying to find the average of all the time differences. However, when I try to do anything with the timedelta in this format, I get the error

AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'

For example, the following gives me the above attribute error:

from datetime import datetime, timedelta
print(datetime.timedelta(0, 9780))

It seems like I can solve the problem by instead having the format:

from datetime import datetime, timedelta
print(timedelta(0, 9780))

And I will get the result:

2:43:00

My question then is, is there a simple way to get python to be able to translate the datetime.timedelta(days, seconds) into the form timedelta(days, seconds)?

1
They are the same thing. from datetime import datetime just means that the second datetime masks the module, hence the error with timedelta. Just do something like import datetime as dt and then do dt.datetime.strftime() (or whatever it is you want datetime for) and dt.timedelta() for example. - roganjosh

1 Answers

1
votes

Ultimately, datetime.timedelta(days, seconds) and timedelta(days, seconds) are the same thing.

Firstly, there is a module called datetime that contains several objects. If you do dir(datetime) you will see (at least in Python 2.7):

['MAXYEAR',
 'MINYEAR',
 '__doc__',
 '__name__',
 '__package__',
 'date',
 'datetime',
 'datetime_CAPI',
 'time',
 'timedelta',
 'tzinfo']

You will note, then, that the datetime module contains two objects of interest; one also called datetime and one called timedelta. If you then did dir(datetime.datetime), you would see:

['__add__',
 '__class__',
 '__delattr__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__ne__',
 '__new__',
 '__radd__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rsub__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 'astimezone',
 'combine',
 'ctime',
 'date',
 'day',
 'dst',
 'fromordinal',
 'fromtimestamp',
 'hour',
 'isocalendar',
 'isoformat',
 'isoweekday',
 'max',
 'microsecond',
 'min',
 'minute',
 'month',
 'now',
 'replace',
 'resolution',
 'second',
 'strftime',
 'strptime',
 'time',
 'timetuple',
 'timetz',
 'today',
 'toordinal',
 'tzinfo',
 'tzname',
 'utcfromtimestamp',
 'utcnow',
 'utcoffset',
 'utctimetuple',
 'weekday',
 'year']

There is no timedelta in that list.

When you did from datetime import datetime, you did not import the datetime module to use but rather imported the datetime object. All references to datetime therefore refer to the object, which does not have timedelta as an attribute/method, hence the error.

Remembering the first list, there is no difference between the following:

from datetime import timedelta
my_time = timedelta(0, 9780)

and

import datetime
my_time = datetime.timedelta(0, 9780)