pandas `read_json` behavior with `bytes` object

Viewed 1000

I have multiple projects(with different pandas versions) which make use pandas.read_json for converting JSON into dataframe object. Here is the behavior with version 1.1.4 which is working as expected.

>>> import pandas as pd
>>> pd.__version__
'1.1.4'
>>>
>>> df = pd.read_json(b'{"col": [1, 2, 3]}')
>>> df
   col
0    1
1    2
2    3

But testing the same code in the latest version(1.4.0) resulting TypeError

>>> import pandas as pd
>>> pd.__version__
'1.4.0'
>>>
>>> df = pd.read_json(b'{"col": [1, 2, 3]}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
..... # Skipped traceback for brevity
raise TypeError(
TypeError: Expected file path name or file-like object, got <class 'bytes'> type

I know this can be easily fixed by using in memory bytes stream (BytesIO)

>>> import pandas as pd
>>>
>>> pd.__version__
'1.4.0'
>>>
>>> from io import BytesIO
>>> df = pd.read_json(BytesIO(b'{"col": [1, 2, 3]}'))
>>> df
   col
0    1
1    2
2    3

But I am curios to understand why the latest version of read_json does not work with bytes type.

Any insights, links to Github issue/discussion will be really useful for the future readers.

0 Answers
Related