3

I want to export some rows from a Pandas DataFrame to JSON. However, when exporting a columns I got an error:

TypeError: False is not JSON serializable

or

TypeError: 0 is not JSON serializable

I looked up in my data and the problem occurs for numpy.int64 and numpy.bool_ (numpy.float64 works fine).

For example, the problem appears for the following:

import pandas as pd
import simplejson as json

df = pd.DataFrame([[False,0],[True,1]], columns=['a','b'])
json.dumps(df.ix[0].to_dict())

(The same thing happens for dict(df.ix[0])).

Is there a simple workaround to export Pandas Series to JSON?

Or at least, a function that coerce any numpy type to the closest type compatible with JSON?

1 Answer 1

7

DataFrame has a method to export itself to json string:

>>> df.to_json()
'{"a":{"0":false,"1":true},"b":{"0":0,"1":1}}'

You can also export it directly to a file:

>>> df.to_json(filename)
Sign up to request clarification or add additional context in comments.

5 Comments

It works with DataFrames, thanks. Does it work also with Series? I am getting OverflowError: Maximum recursion level reached.
@PiotrMigdal It does work with Series. Can you post a Series object that reproduces the stack overflow?
@PhillipCloud For example the one in the question, i.e. df.ix[0].to_json() for df = pd.DataFrame([[False,0],[True,1]], columns=['a','b']).
I get '{"a":false,"b":0}', as expected. That's on pandas git master, which may have this fix in it (and not in the 0.12 release). Let me see.
@PhillipCloud Yup, it's a bug in 0.12, but it's fixed on master.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.