How to Solve Python AttributeError: ‘datetime.datetime’ object has no attribute ‘timestamp’

by | Programming, Python, Tips

The timestamp method was added in Python 3.3. If you try to call the timestamp method with Python version 3.2 or earlier, you will raise the AttributeError: ‘datetime.datetime’ object has no attribute ‘timestamp’. You can solve this error by upgrading to the latest Python version. Alternatively, you can use time.mktime(), for example:

from datetime import datetime
import time

dt = datetime.now()
timestamp = time.mktime(dt.timetuple()) + dt.microsecond/1e6

This tutorial will go through the error and how to solve it with code examples.


AttributeError: type object ‘datetime’ has no attribute ‘fromisoformat’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The timestamp method is new in Python version 3.3 and returns a POSIX timestamp corresponding to the datetime instance. Python versions older than 3.3 do not have timestamp as an attribute of the datetime class.

Example

Let’s look at an example of converting a datetime object into a POSIX timestamp using the timestamp method. We will use the now() method to get the current date and time and use it as the parameter for the timestamp method.

from datetime import datetime

# current date and time

now = datetime.now()

timestamp = datetime.timestamp(now)

print "Timestamp = ", timestamp

Let’s run the code to see the result:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-ce8c27166c73> in <module>()
      5 now = datetime.now()
      6 
----> 7 timestamp = datetime.timestamp(now)
      8 
      9 print "Timestamp = ", timestamp

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

The error occurs because we are using a Python version older than 3.3. We can check the version of Python we are using by importing sys and printing sys.version.

import sys
print(sys.version)
2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 16:55:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]

Solution: Upgrade to Python 3.3+

The first way we can solve this error is by upgrading to the latest version of Python. Suppose we are in a conda environment with Python 2.7 installed. We can upgrade to the newest version of Python with the following command:

conda update python

We can then check we are using the newest version of Python with the sys module.

import sys
print(sys.version)
3.8.8 (default, Apr 13 2021, 12:59:45) 
[Clang 10.0.0 ]

With this version of Python, we can use the datetime.timestamp method as follows:

from datetime import datetime

# current date and time

now = datetime.now()

timestamp = datetime.timestamp(now)

print(f'Timestamp = {timestamp}')
Timestamp = 1653601180.404315

We successfully converted the current date and time to a POSIX timestamp.

Solution #2: Use time.mktime

The alternative way to solve this error is to use the time.mktime() method. mktime is a C native function that converts a broken-down time, expressed as local time, into a time since the Unix epoch.

The mktime() method accepts a struct_time or full 9-tuple as its argument. We can convert the current datetime to a struct_time using the timetuple() method.

from datetime import datetime

import time

now = datetime.now()

timestamp = time.mktime(now.timetuple()) + now.microsecond/1e6

print "Timestamp = ", timestamp

Using the microseconds() method, we add the datetime microseconds to the end of the timestamp. Let’s run the code to see the result:

Timestamp =  1653601433.77

We successfully converted the current date and time to a POSIX timestamp.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on AttributeErrors involving datetime, go to the article:

How to Solve Python AttributeError: type object ‘datetime.datetime’ has no attribute ‘fromisoformat’

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!

Follow the Research Scientist Pod on Social media!