Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Series writing and reading json data gives ValueError with to_json and read_json [duplicate]

When writing data to json from a pandas.Series object using to_json, then reading that data in using pandas.read_json, I receive the following ValueError.

import pandas as pd
js = pd.Series([1, 2, 3], index=list('abc')).to_json()
js
# out: '{"a":1,"b":2,"c":3}'

pd.read_json(js)
# Traceback ... 
# ValueError: If using all scalar values, you must pass an index

Apparently because the json data contains just index:value pairs, the read_json function doesn't know to interpret the keys as indexes.

I also tried the following orient option based on the docs, which resulted in a different ValueError.

js = pd.Series([1, 2, 3], index=list('abc'), name='mydata').to_json(orient='split')
js
# out: '{"name":"mydata","index":["a","b","c"],"data":[1,2,3]}'
pd.read_json(js, orient='split')
# Traceback ... 
# ValueError: JSON data had unexpected key(s): name

My question is: how do I configure Series.to_json to be compatible with pd.read_json? Is this a bug/opportunity to improve the Series default json write/read behavior?

Thanks for the help!

like image 635
Matthew Davis Avatar asked Dec 01 '25 00:12

Matthew Davis


1 Answers

Simple fix, use the typ argument:

pd.read_json(js, typ='series')

a    1
b    2
c    3
dtype: int64

It's "typ" and not "type" so as to not confuse it with the type builtin!

like image 121
cs95 Avatar answered Dec 03 '25 13:12

cs95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!