In this article, we will walk you through how to solve the “typeerror object of type series is not json serializable” error message.
We can’t deny the fact that if you are a developer or programmer, you will always encounter such errors.
And one of them is “object of type series is not json serializable.”
But before we jump into the solutions, let’s have a better understanding with regards to this error.
What is “typeerror object of type series is not json serializable”?
The “typeerror: object of type series is not json serializable” is an error message that is raised when you’re trying to convert a Pandas Series object into a JSON format.
However, it is not possible because JSON serialization does not support the Series data type.
JSON serialization is the process of converting a Python object into a JSON string, and not all Python objects can be converted to JSON.
How to fix “typeerror object of type series is not json serializable”?
To fix this “object of type series is not json serializable“ you have to convert the Series object into a format that can be serialized into JSON, such as a Python list or dictionary, before converting it to JSON.
You can do this using the tolist() or to_dict() methods provided by Pandas.
Then you can convert the resulting object into JSON using the json.dumps() method.
1. Convert the series using tolist() method
You have to convert the Pandas Series object into a Python list using the tolist() method provided by Pandas.
Then serialize the resulting list into a JSON string using the json.dumps() method provided by Python’s built-in json module.
import pandas as pd
import json
data = pd.Series([10, 20, 30, 40, 50])
# Convert Series to list
data_list = data.tolist()
# Serialize list to JSON
json_data = json.dumps(data_list)
print(json_data)
Output:
[10, 20, 30, 40, 50]2. Convert the series using to_dict() method
You can also convert the Pandas Series object into a Python dictionary using the to_dict() method provided by Pandas.
Then serialize the resulting dictionary into a JSON string using the json.dumps() method provided by Python’s built-in json module.
import pandas as pd
import json
data = pd.Series([10, 20, 30, 40, 50])
# Convert Series to dictionary
data_dict = data.to_dict()
# Serialize dictionary to JSON
json_data = json.dumps(data_dict)
print(json_data)
Output:
{"0": 10, "1": 20, "2": 30, "3": 40, "4": 50}3. Use the json.JSONEncoder class to create a custom encoder that can handle the Series object
You have to define a custom encoder class that extends the JSONEncoder class provided by Python’s built-in json module.
Override the default() method of the class to handle the Pandas Series data type by converting it to a Python list using the tolist() method provided by Pandas.
import pandas as pd
import json
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, pd.Series):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
data = pd.Series([10, 20, 30, 40, 50])
# Serialize using custom encoder
json_data = json.dumps(data, cls=CustomEncoder)
print(json_data)
Then serialize the resulting object using the json.dumps() method provided by Python’s built-in json module, passing an instance of the custom encoder class using the cls parameter.
Ouput:
[10, 20, 30, 40, 50]Conclusion
That’s the end of our discussion about the “typeerror object of type series is not json serializable” error message.
This article already provides different solutions that you may use to fix this “object of type series is not json serializable” type error.
We are hoping that this article provided you with sufficient solutions to get rid of the error.
You could also check out other “typeerror” articles that may help you in the future if you encounter them.
- Typeerror numpy.ndarray object is not callable
- Typeerror fit missing 1 required positional argument y
- Typeerror object of type is not json serializable
Thank you very much for reading to the end of this article
Python TypeError debugging checklist
- Read the full traceback. The bottom line is the error type + message. The line above shows the exact code that triggered it.
- Print types. Insert
print(type(x), type(y))before the error line to see what Python actually has. - Use isinstance. Guard code with
if isinstance(x, expected_type):. - Type hints + mypy. Adding
x: intlets mypy catch mismatches before you run the code. - Break into a debugger. Insert
breakpoint()before the failing line and inspect variables live.
Common root causes across all TypeError variants
- Silent None returns. A function that should have returned a value returned None instead.
- Mixing types across function boundaries. Legacy code passing str where int is expected (or vice versa).
- Shadowed builtins. Local variable named list, dict, set overriding the built-in.
- Optional[T] not handled. Callers not accounting for the None case.
- Third-party library API drift. New version renamed a kwarg or changed a return type.
Modern tooling to prevent TypeError
- Type hints (PEP 484+). Optional[X], Union[X,Y], List[T] make expected types explicit.
- mypy or Pyright. Runs your codebase through a type checker before you run it.
- Ruff. Fast linter that catches many TypeError-adjacent bugs.
- pydantic v2. Runtime validation with the same syntax as static types.
- pytest fixtures. Test each function with edge-case inputs to catch TypeError paths early.
Official documentation
Frequently Asked Questions
What is Python TypeError and what causes it?
TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.
How do I quickly debug a Python TypeError?
Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.
Should I catch TypeError or let it propagate?
For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.
How do I prevent TypeError in production?
Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.
Where can I find more TypeError fixes?
Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.
