Typeerror: object of type dataframe is not json serializable

Do you want to know how to solve Typeerror: object of type dataframe is not json serializable?

Read this article for you to understand this error and know how to fix it.

Through this article, you will understand what Typeerror: object of type dataframe is not json serializable means, How and why this error occurs, and know the different steps to solve it.

So first, let us know what this error means.

What is Typeerror: object of type dataframe is not json serializable?

The “Typeerror: object of type dataframe is not json serializable” is an error message indicating that you are trying to serialize a Pandas DataFrame object as JSON, but JSON serialization is not possible with this type of object.

This error message can occur due to various reasons.

But before that, let’s discuss a few of the key terms that can be found in this error message.

What is JSON?

JSON (JavaScript Object Notation) is a widely used serialization format for exchanging data between different systems.

The json module in Python is used to encode and decode JSON data.

Now let’s proceed to the causes of why the “object of type dataframe is not json serializable” error occurs.

How does Typeerror: object of type dataframe is not json serializable occurs?

The Typeerror: object of type dataframe is not json serializable occurs because the Pandas DataFrame object that you are trying to convert to JSON format using the json module contains data that cannot be serialized to JSON.

This error can occur for a number of reasons, such as:

  1. Incorrect usage of json module
  2. Complex data structure
  3. Presence of non-JSON serializable data

1. Incorrect usage of json module:

The reason why “object of type dataframe is not json serializable” error occurs is that the json module may be used incorrectly.

For example, if the json.dumps() function is called with the wrong arguments or if the object being serialized is not a JSON serializable object.

Here is the example code:

import pandas as pd
import json

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

json_data = json.dumps(df)

In this example, the json.dumps() function is called with the DataFrame df as an argument, which is not a JSON serializable object.

When the json module tries to serialize the DataFrame object, it throws the error message stating:

TypeError: Object of type DataFrame is not JSON serializable

2. Complex data structure:

Another reason why “object of type dataframe is not json serializable” error occurs is that the DataFrame may have a complex data structure that is not supported by JSON.

Such as multi-index columns or hierarchical indexes.

Here is an example code that DataFrame has a complex data structure:

import pandas as pd
import json

df = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6]})
df.set_index(['col1', 'col2'], inplace=True)

json_df = json.dumps(df)

In the above example, the DataFrame df has a complex data structure due to a multi-index, which is not supported by JSON.

When we try to convert df to JSON format using json.dumps(), we get the error.

3. Presence of non-JSON serializable data:

Another reason why “object of type dataframe is not json serializable” error occurs is when the DataFrame may contain data types that cannot be serialized to JSON.

Such as NaN or inf values, datetime objects, or custom data types that are not recognized by the default JSON encoder.

Here is the example code snippet:

import pandas as pd
import json

df = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9], 'col4': [float('nan'), float('inf'), -float('inf')]})

json_df = json.dumps(df) # Error: Typeerror: object of type dataframe is not json serializable

In the above example, the DataFrame df contains non-serializable data, i.e., NaN, inf and -inf values, which are not valid JSON values.

Hence, when we try to convert df to JSON format using json.dumps(), we get the error.

Now let’s fix this error.

Typeerror: object of type dataframe is not json serializable – Solutions

To fix the “Typeerror: object of type dataframe is not json serializable”, you can use the following solutions:

Solution 1: Use the to_dict() method:

Use the to_dict() method of the DataFrame to first convert it to a dictionary and then serialize the dictionary to JSON using the json module.

Here is an example code:

import pandas as pd
import json

df = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6]})

dict_df = df.to_dict(orient='records')
json_df = json.dumps(dict_df)

print(json_df)

Solution 2: Drop non-serializable data:

You can drop the rows/columns containing non-serializable data before serializing the DataFrame to JSON.

Alternatively, you can replace the non-serializable values with a valid JSON value, such as null.

For example:

import pandas as pd
import json

df = pd.DataFrame({'col1': [1, 2, None], 'col2': [4, float('inf'), 6]})

# Drop rows containing non-serializable values
df = df.dropna()

json_df = df.to_json()

print(json_df)

Solution 3: Flatten the DataFrame:

If the DataFrame has a complex data structure, such as a multi-index, you can flatten the DataFrame before serializing it to JSON.

This can be done using the reset_index() method of the DataFrame or by using the pandas.json_normalize() function.

Here is the example code:

import pandas as pd
import json

df = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': ['A', 'B', 'C']})
df = df.set_index(['col1', 'col3'])

# Flatten the DataFrame
df = df.reset_index()

json_df = df.to_json(orient='records')

print(json_df)

Solution 4: Use a custom encoder:

You can define a custom encoder class that extends the json.JSONEncoder class and handles the serialization of these objects.

This will be used if the DataFrame contains custom data types or objects that are not serializable by default.

For example:

import pandas as pd
import json

class CustomEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, pd.Timestamp):
            return obj.isoformat()
        return json.JSONEncoder.default(self, obj)

df = pd.DataFrame({'date': [pd.Timestamp('2022-01-01')]})

# Convert the DataFrame to a dictionary
data_dict = df.to_dict(orient='records')

# Serialize the dictionary to a JSON string using the custom encoder class
json_df = json.dumps(data_dict, cls=CustomEncoder)

print(json_df)

Solution 5: Use a third-party library:

There are several third-party libraries available that provide more advanced serialization capabilities for Pandas DataFrames.

Such as pandas.DataFrame.to_json() method or simplejson library.

Just like the example below:

import pandas as pd
import simplejson as json

df = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6]})

json_df = df.to_json(date_format='iso', default_handler=str)

print(json_df)

Those are the alternative solutions that you can use to fix the “Typeerror: object of type dataframe is not json serializable” error.

Hoping that one or more of them helps you troubleshoot and resolve the error.

Here are the other fixed Python errors that you can visit, you might encounter them in the future.

Conclusion

In conclusion, The Typeerror: object of type dataframe is not json serializable is an error message that occurs because the Pandas DataFrame object that you are trying to convert to JSON format using the json module contains data that cannot be serialized to JSON.

To solve this error, you need to convert the DataFrame into a JSON-serializable format or remove any data that is not supported by JSON from the DataFrame before attempting to serialize it.

By following the given solution above, surely you can fix the error quickly and proceed to your coding project again.

I hope this article helps you to solve your problem regarding a Typeerror stating “object of type dataframe is not json serializable”.

We’re happy to help you.

Happy coding! Have a Good day and God bless.