10

I have some problems converting a simple Pandas Series into a json string and back. Here's my attempt

import pandas as pd
f = pd.Series(data=[1.0,2.0,3.0],index=[10,20,30])
x = f.to_json()
a = pd.read_json(x)

This results in ValueError: If using all scalar values, you must pass an Index.

The json String x looks like {"10":1.0,"20":2.0,"30":3.0}

What's missing here. Please help

1 Answer 1

20

You need to specify the type of object (default is DataFrame) and the format of the JSON string. More info here.

This should work:

a = pd.read_json(x, typ='series', orient='records')
1
  • ah, me being stupid. I oversaw the typ argument... Easy now. Works. Thanks so much
    – tschm
    Apr 9, 2014 at 15:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.