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!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨