-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Pandas DataFrame is not JSON serializable #7689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
what are you asking here? pandas provides a read/write API to JSON: http://pandas.pydata.org/pandas-docs/stable/io.html#json, which should be deserializable by foreign JSON parsers. You must use |
Hello, Thanks I know
But maybe I'm wrong and there is no way for Kind regards |
This is the typical way to extend the default json encoder
|
So you think it's better to define our own JSONEncoder. The problem is that I don't think jsonify support cls argument for encoder... but that's an other problem Moreover I noticed
it's like a string inside a string... What I want to encode is in fact
I know I can do
but I feel that's not the right way of doing |
HTH their are lots of options for not really sure what you are doing closing this as its out of scope for pandas. |
Please try this:
it returns |
you need to teach
|
Thanks for this tutorial but there is a difference between the 2 serialized versions
Is there a clean solution with a custom encoder for object with to_json method (like DataFrame) to output correctly JSON (without extra quotes) |
this is maybe a question for SO, I don't use custom uncoders at all. It seems a whole lot simpler for you to simply call |
I understand your reply... but imagine you have several dataframes to output into the same JSON message.... doing things this way is not very clear. I think it's much more clear to have a dict structure which can contains several df (and other data) and after serialize it. About deserialization... yes that's not a problem... I know structure and where are dataframes. |
ok, as I said, you maybe want to do custom encoding/decoding like I showed above. You need to write that. a dataframe can be turned into json via |
It works much better with this custom encoder (with
There is no extra quotes. Thanks |
Problem is that in fact I can't use 'orient' parameter which is very convenient to reduce message size. Maybe a |
@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
|
you can use make_response from flask , e.g. |
This saved my life. Thank you! |
Thanks, Abir0802. Was stuck here for a while. |
Well, there are too many Python-specific data types which JSON cannot serialize/deserialize. Take a look at the following nested data structure and you will immediately realize how many things JSON cannot handle: [1, 3.4, 1.1+2.1j, np.nan, None, True, False, b'ab12', 'abc', int, float,
pd.Series(), pd.DataFrame(), pd.DataFrame, type(pd.DataFrame), ['a', 1],
{
'a':1,
'b':2,
print:max,
pd:np,
type:0,
int:1,
0:pd.DataFrame(np.random.randint(0,256,[4,4]),
columns=['index a1', 'index a2', 'b', 'c'],
index=pd.date_range('2020-01-01', '2020-01-04')).set_index(['index a1', 'index a2'], append=True),
1:pd.Series([1, 2.5, 3+1j, np.nan, 'abc'], index=pd.date_range('2020-01-01', '2020-01-05', tz='Asia/Singapore')),
2:np.array([[1, 2.5, 'a'], [1+.5j, np.nan, 'b']]),
3:np.matrix([[1, 2.5], [1+.5j, np.nan]])
},
{1, 3.4, 1+2j, np.nan, True, False, None, int, 'aa', os, sys, pd.concat}] I have recently developed a comprehensive utility pandas-serializer, which can serialize/deserialize almost everything (exactly everything as shown above). My utility does not depend on JSON at all and it uses native Python eval/repr/str to serialize and deserialize. You are welcome to try and see what cannot be identically deserialized, and report to me. Thanks! -:) |
That is the solution 👍 💯 |
Hello,
I'm trying to build a RESTful api with Flask-RESTful which will return Pandas DataFrame as JSON data.
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
So it seems that Pandas DataFrame are not JSON serializable.
I try this using IPython
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
see flask-restful/flask-restful#269
Kind regards
The text was updated successfully, but these errors were encountered: