question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

read_json can't import own exported data with orient=table

See original GitHub issue
import pandas
pd = pandas.DataFrame([["Arr","alpha", [1,2,3,4]],["Bee", "Beta", [10,20,30,40]]], index = [["A", "B"], ["Null", "Eins"]], columns = ["Aussprache", "Griechisch", "Args"])

: print(pd)
       Aussprache Griechisch              Args
A Null        Arr      alpha      [1, 2, 3, 4]
B Eins        Bee       Beta  [10, 20, 30, 40]

pd.to_json("test.json", orient = "table")
pandas.read_json("test.json", orient="table")

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-66-a171b349d78d> in <module>()
----> 1 pandas.read_json("test.json", orient="table")

/usr/lib/python3.6/site-packages/pandas/io/json/json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression)
    364         return json_reader
    365
--> 366     return json_reader.read()
    367
    368

/usr/lib/python3.6/site-packages/pandas/io/json/json.py in read(self)
    463             )
    464         else:
--> 465             obj = self._get_object_parser(self.data)
    466         self.close()
    467         return obj

/usr/lib/python3.6/site-packages/pandas/io/json/json.py in _get_object_parser(self, json)
    480         obj = None
    481         if typ == 'frame':
--> 482             obj = FrameParser(json, **kwargs).parse()
    483
    484         if typ == 'series' or obj is None:

/usr/lib/python3.6/site-packages/pandas/io/json/json.py in parse(self)
    572
    573         else:
--> 574             self._parse_no_numpy()
    575
    576         if self.obj is None:

/usr/lib/python3.6/site-packages/pandas/io/json/json.py in _parse_no_numpy(self)
    802         else:
    803             self.obj = DataFrame(
--> 804                 loads(json, precise_float=self.precise_float), dtype=None)
    805
    806     def _process_converter(self, f, filt=None):

/usr/lib/python3.6/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
    328                                  dtype=dtype, copy=copy)
    329         elif isinstance(data, dict):
--> 330             mgr = self._init_dict(data, index, columns, dtype=dtype)
    331         elif isinstance(data, ma.MaskedArray):
    332             import numpy.ma.mrecords as mrecords

/usr/lib/python3.6/site-packages/pandas/core/frame.py in _init_dict(self, data, index, columns, dtype)
    459             arrays = [data[k] for k in keys]
    460
--> 461         return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
    462
    463     def _init_ndarray(self, values, index, columns, dtype=None, copy=False):

/usr/lib/python3.6/site-packages/pandas/core/frame.py in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
   6128     # figure out the index, if necessary
   6129     if index is None:
-> 6130         index = extract_index(arrays)
   6131     else:
   6132         index = _ensure_index(index)

/usr/lib/python3.6/site-packages/pandas/core/frame.py in extract_index(data)
   6179
   6180             if have_dicts:
-> 6181                 raise ValueError('Mixing dicts with non-Series may lead to '
   6182                                  'ambiguous ordering.')
   6183

ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.

Problem description

test.json looks like:

{"schema": {"fields":[{"name":"level_0","type":"string"},{"name":"level_1","type":"string"},{"name":"Aussprache","type":"string"},{"name":"Griechisch","type":"string"},{"name":"Args","type":"string"}],"primaryKey":["level_0","level_1"],"pandas_version":"0.20.0"}, "data": [{"level_0":"A","level_1":"Null","Aussprache":"Arr","Griechisch":"alpha","Args":[1,2,3,4]},{"level_0":"B","level_1":"Eins","Aussprache":"Bee","Griechisch":"Beta","Args":[10,20,30,40]}]}

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.14.7-1-ARCH
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: de_DE.UTF-8

pandas: 0.21.0
pytest: None
pip: 9.0.1
setuptools: 38.2.4
Cython: 0.27.3
numpy: 1.13.3
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.5
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.0
html5lib: None
sqlalchemy: 1.1.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jrebackcommented, Dec 23, 2017

FYI don’t use pd as a variable name, when the common import for pandas is pd

In [26]: df = pd.DataFrame([["Arr","alpha", [1,2,3,4]],["Bee", "Beta", [10,20,30,40]]], index = [["A", "B"], ["Null", "Eins"]], columns = ["Aussprache", "Griechisch", "Args"])

In [27]: df
Out[27]: 
       Aussprache Griechisch              Args
A Null        Arr      alpha      [1, 2, 3, 4]
B Eins        Bee       Beta  [10, 20, 30, 40]

I don’t we have any guarantees when you embed lists inside a field and try to export to json. Further I don’t think orient='table' is actually reversible.

1reaction
jrebackcommented, Dec 23, 2017

records works just fine here.

In [34]: pd.read_json(df.to_json(orient='records'), orient='records')
Out[34]: 
               Args Aussprache Griechisch
0      [1, 2, 3, 4]        Arr      alpha
1  [10, 20, 30, 40]        Bee       Beta
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way I can read the JSON file saved by dataframe ...
read_json ('a.json', orient='table') C:\Anaconda3\lib\site-packages\pandas\io\json\json.
Read more >
pandas.read_json — pandas 1.5.2 documentation
Direct decoding to numpy arrays. Supports numeric data only, but non-numeric column and index labels are supported. Note also that the JSON ordering...
Read more >
How to manage a large JSON file efficiently and quickly - Sease
1) USE THE METHOD PANDAS.READ_JSON PASSING THE CHUNKSIZE PARAMETER ... “Breaking” the data into smaller pieces, through chunks' size selection, hopefully, allows ...
Read more >
Cannot export and import json object in version 0.40.0 but in ...
I'm trying to export a “json object” defined in my main k6 file and import it in another js file located in a...
Read more >
How to convert pandas DataFrame into JSON in Python?
For orient='table', the default is 'iso'. ... Handler to call if object cannot otherwise be converted to a suitable ... import pandas as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found