I worked with pandas DataFrame objects on my server, converting them to CSV for transfer to a browser where table values ββare displayed using d3. Although CSV is a file as much as possible, I really need more than just a 2D data table. If nothing else, I would like to return some metadata about the data.
So, I started talking to JSON, thinking that I could build a dictionary with some meta-information and my DataFrame. For example, as an absurdly simple example:
>>> z = numpy.zeros(10) >>> df = pandas.DataFrame(z) >>> df 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 >>> result = { ... "name": "Simple Example", ... "data": df, ... }
Not surprisingly, this cannot be directly serialized using the json module. I found the jsonext module and tried it. It "works", but gives incomplete results:
>>> jsonext.dumps(result) '{"data": ["0"], "name": "Simple Example"}'
Looking at the methods that the DataFrame itself provides for this kind of thing, I found to_dict () and to_json (). The first produces dictionaries of dictionaries:
>>> df.to_dict() {0: {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0}}
but, as you can see, they cannot be serialized in JSON, since keys are not strings.
df.to_json () looked like it might work, although I would run a JSON string embedded in another JSON string. Something like that:
json.dumps ({"name": "Simple Example", "data": df.to_json ()}) '{"data": "{\" 0 \ ": {\" 0 \ ": 0.0, \" 1 \ ": 0.0, \" 2 \ ": 0.0, \" 3 \ ": 0.0, \" 4 \ ": 0.0, \" 5 \ ": 0.0, \" 6 \ ": 0.0, \" 7 \ ": 0.0, \" 8 \ ": 0.0, \" 9 \ ": 0.0}}", "name": "A simple example"} '
In other words, a bit of a mess.
Any suggestions on how to handle such a nested structure where some of the elements cannot be directly serialized? I think I could get jsonext to work, but its Dict mixin expects to find the correct (in my mind) to_dict () method. DataFrame.to_dict () doesn't seem to return the right thing. (Although I will continue to ride with him.)
I decided that this should be a cat who was already a scrub. I just didnβt find it. I would be pleased that I had nothing more hierarchical than something like my example (albeit with a lot of key / value pairs), although I will not turn my nose in a more general solution.