TypeError: 'datetime.datetime' object is not subscriptable

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: 'datetime.datetime' object is not subscriptable #

The Python "TypeError: 'datetime.datetime' object is not subscriptable" occurs when we use square brackets to access an index or a key in a datetime object. To solve the error, use dot notation to access an attribute or remove the square brackets accessor.

typeerror datetime datetime object is not subscriptable

Here is an example of how the error occurs.

main.py
from datetime import datetime result = datetime.today() print(type(result)) # 👉️ <class 'datetime.datetime'> # ⛔️ TypeError: 'datetime.datetime' object is not subscriptable print(result[0])
We are using square brackets to access an index on a datetime object, but datetime objects are not subscriptable.

You could also be trying to access a specific key.

main.py
from datetime import datetime result = datetime.today() print(type(result)) # ⛔️ TypeError: 'datetime.datetime' object is not subscriptable print(result['some_key'])

Instead, you should access an attribute on the datetime object or remove the square brackets accessor.

The best way to start debugging is to call the dir() function passing it the imported module.

main.py
from datetime import datetime # 👇️ [...'astimezone', 'combine', 'ctime', 'date', 'day', # 'dst', 'fold', 'fromisocalendar', 'fromisoformat', # 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar',... ] print(dir(datetime))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

You can use dot notation to access any of the attributes a datetime object supports.

main.py
from datetime import datetime d = datetime(2022, 11, 24, 9, 30, 0) # 👇️ 24/11/22 print(d.strftime("%d/%m/%y")) print(d.year) # 👉️ 2022 print(d.month) # 👉️ 11

Or you can remove the square brackets if you didn't intend to access an attribute.

The error means that we are using square brackets to either access a key in a specific object or to access a specific index, however the object doesn't support this functionality.

You should only use square brackets to access subscriptable objects.

The subscriptable objects in Python are:

  • list
  • tuple
  • dictionary
  • string

All other objects have to be converted to a subscriptable object by using the list(), tuple(), dict() or str() classes to be able to use bracket notation.

Subscriptable objects implement the __getitem__ method whereas non-subscriptable objects do not.

main.py
a_list = ['bobby', 'hadz', 'com'] # 👇️ <built-in method __getitem__ of list object at 0x7f71f3252640> print(a_list.__getitem__)

Conclusion #

The Python "TypeError: 'datetime.datetime' object is not subscriptable" occurs when we use square brackets to access an index or a key in a datetime object. To solve the error, use dot notation to access an attribute or remove the square brackets accessor.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.