3

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!

Matthew Davis
  • 497
  • 4
  • 12

1 Answers1

7

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!

cs95
  • 379,657
  • 97
  • 704
  • 746