How to Solve Python TypeError: Object of type Timestamp is not JSON serializable

by | Programming, Python, Tips

This error occurs when you try to serialize a pandas Timestamp object to a JSON string using the json.dumps() method. You can solve this error by converting the Timestamp to a string using the build in str() method. For example,

import json
import pandas as pd

timestamp = pd.Timestamp('2011-05-19T13')

json_str = json.dumps(str(timestamp))

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


TypeError: Object of type Timestamp is not JSON serializable

Let’s break up the error message to understand what the error means. TypeError occurs whenever you attempt to use an illegal operation for a specific data type. The part “Object of type Timestamp” tells us the error is due to an illegal operation with a map object.

Serialization in Python refers to converting a Python object into a transmittable format that we can recreate when needed using deserialization. JSON serialization returns a human-readable string form called a JSON string. The JSON encoder json.dump() and json.dumps() can only serialize certain object types like dictionaries, lists, or strings.

is not JSON serializable” informs us that the JSON serialization is an illegal operation for the Timestamp type.

Example

Let’s look at an example of serializing of a Timestamp object. The Timestamp is the pandas equivalent of Python’s built-in Datetime. First, we will create a Pandas DataFrame with two columns, one for a date and one for a random number. The dates will occupy a range of five days.

import pandas as pd
import numpy as np

np.random.seed(0)
rng = pd.date_range('2021-01-13', periods=5, freq='D')
df = pd.DataFrame({ 'Date': rng, 'Val': np.random.randn(len(rng)) }) 
print(df)
        Date       Val
0 2021-01-13  1.764052
1 2021-01-14  0.400157
2 2021-01-15  0.978738
3 2021-01-16  2.240893
4 2021-01-17  1.867558

Next, we will create variable containing the Date column, then select the first date and attempt to serialize it to a JSON string using the json.dumps() method.

import json

dates = df.Date

first_date = dates[0]

json_str = json.dumps(first_date)

print(json_str)

Let’s run the code to see what happens:

TypeError: Object of type Timestamp is not JSON serializable

The error occurs because first_date is a Timestamp object and the json.dumps() method does not handle Timestamp objects by default.

Solution #1: Convert Timestamp to string using str()

The simplest way to solve this error is to convert the Timestamp object to a string using the built-in str() method. The json.dumps() method handles string objects. Let’s look at the revised code:

import json

dates = df.Date

first_date = str(dates[0])

json_str = json.dumps(first_date)

print(json_str)

Let’s run the code to get the JSON string:

"2021-01-13 00:00:00"

Solution #2: Set default argument to str

We can also solve this error by setting the default argument of json.dumps() to str. The function we set default to gets called for objects that are not JSON serializable. In this case we can pass the Timestamp object directly to the json.dumps() method.

Let’s look at the revised code:

import json

dates = df.Date

first_date = dates[0]

json_str = json.dumps(first_date, default=str)

print(json_str)

Let’s run the code to see the JSON string:

"2021-01-13 00:00:00"

Solution #3: Define a JSONEncoder subclass for the cls kwarg

We can also solve this error by building a custom JSONEncoder subclass. This subclass will override the default method to serialize additional types.

import json
import pandas as pd

class timestamp_encoder(json.JSONEncoder):

    def default(self, obj):

        if isinstance(obj, pd.Timestamp):

            return str(obj)

        return json.JSONEncoder.default(self, obj)

Similar to the custom function, the default method of the timestamp_encoder class checks if the object is of type Timestamp, converts it to a string and returns it.

We have to specify the custom JSONEncoder subclass with the cls keyword argument. Otherwise, JSONEncoder is used. Let’s look at the updated code:

import json

dates = df.Date

first_date = dates[0]

json_str = json.dumps(first_date, cls=timestamp_encoder)

print(json_str)

Let’s run the code to get the JSON string.

"2021-01-13 00:00:00"

Below is the collection of objects that the JSONEncoder class supports by default and their JSON equivalent.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int- & float- derived Enumsnumber
Truetrue
Falsefalse
Nonenull
JSONEncoder Supported objects and types by default

Summary

Congratulations on reading to the end of this tutorial.

For further reading on errors involving JSON serialization, go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

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 ✨