Pandas DataFrame is not JSON serializable
See original GitHub issueHello,
I’m trying to build a RESTful api with Flask-RESTful which will return Pandas DataFrame as JSON data.
from flask import Flask
from flask.ext import restful
from flask.ext.restful import Resource, Api
import pandas as pd
import click
import logging
app = Flask(__name__)
api = restful.Api(app)
class DataWebservice(Resource):
def get(self, size):
logging.info("get %d" % size)
# get DB conn
# df = pd.read_sql(...) # USE LIMIT
df = pd.DataFrame({"col1": [1]*size, "col2": [2]*size})
#return(df.to_json())
return(df)
api.add_resource(DataWebservice, '/api/v1/solar/df/get/<int:size>')
@click.command()
@click.option('--host', default='127.0.0.1', \
help="host (127.0.0.1 or 0.0.0.0 to accept all ip)")
@click.option('--debug/--no-debug', default=False, help="debug mode")
def main(debug, host):
app.run(host=host, debug=debug)
if __name__ == '__main__':
main()
I run server using $ python server.py --debug
I run client using $ curl http://127.0.0.1:5000/api/v1/solar/df/get/10
but I get the following error
TypeError: col1 col2
0 1 2
1 1 2
...
8 1 2
9 1 2
[10 rows x 2 columns] is not JSON serializable
So it seems that Pandas DataFrame are not JSON serializable.
I try this using IPython
size = 10
df = pd.DataFrame({"col1": [1]*size, "col2": [2]*size})
json.dumps(df)
It raises same error. I’m aware that DataFrame have method named to_json() but it doesn’t help me much as my server will return escaped strings such as
"{\"col1\":{\"0\":1,\"1\":1,\"2\":1,\"3\":1,\"4\":1,\"5\":1,\"6\":1,\"7\":1,\"8\":1,\"9\":1},\"col2\":{\"0\":2,\"1\":2,\"2\":2,\"3\":2,\"4\":2,\"5\":2,\"6\":2,\"7\":2,\"8\":2,\"9\":2}}"
see https://github.com/twilio/flask-restful/issues/269
Kind regards
Issue Analytics
- State:
- Created 9 years ago
- Comments:20 (14 by maintainers)
Top Results From Across the Web
TypeError: Object of type DataFrame is not JSON serializable
The Python "TypeError: Object of type DataFrame is not JSON serializable" occurs when we try to serialize a DataFrame object to JSON using...
Read more >TypeError: Object of type 'DataFrame' is not JSON serializable
Your df is still a data frame because you haven't assigned it as json. df = df.to_json(). This should work. Let me know...
Read more >TypeError: Object of type 'DataFrame' is not JSON serializable
Pandas : TypeError: Object of type ' DataFrame' is not JSON serializable [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] ...
Read more >Object of type is not JSON serializable in Python - LinuxPip
“Object of type is not JSON serializable” is a common error message in Python. It basically says that the built-in json module cannot...
Read more >Object of type Series is not JSON serializable - Python Help
If I were to hazard a guess, though, you're trying to pass a pandas.Series , i.e. a pandas Series object (a single-column dataframe,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
you can use make_response from flask , e.g. resp = make_response(df.to_json(orient = “records”)) and then simply return it.
@scls19fr this is obviously an old issue, but seeing as I stumbled upon it. The easiest way to nest a dataframe in a larger JSON blob is to use