Skip to content

BUG: reading line-format JSON from file url #27135 #34811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas/io/json/_json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import abc
import functools
from io import StringIO
from io import BytesIO, StringIO
from itertools import islice
import os
from typing import Any, Callable, Optional, Type
Expand Down Expand Up @@ -724,6 +724,9 @@ def _get_data_from_filepath(self, filepath_or_buffer):
self.should_close = True
self.open_stream = data

if isinstance(data, BytesIO):
data = data.getvalue().decode()

return data

def _combine_lines(self, lines) -> str:
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/io/json/data/line_delimited.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

{"a": 1, "b": 2}
{"a": 3, "b": 4}
{"a": 5, "b": 6}
16 changes: 16 additions & 0 deletions pandas/tests/io/json/test_readlines.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from io import StringIO
from pathlib import PureWindowsPath
from platform import system

import pytest

Expand Down Expand Up @@ -219,3 +221,17 @@ def test_readjson_nrows_requires_lines():
msg = "nrows can only be passed if lines=True"
with pytest.raises(ValueError, match=msg):
pd.read_json(jsonl, lines=False, nrows=2)


def test_readjson_lines_chunks_fileurl(datapath):
# GH 27135
# Test reading line-format JSON from file url
os_path = datapath("io", "json", "data", "line_delimited.json")
file_url = "file://localhost" + os_path
if system() == "Windows":
file_url = PureWindowsPath(file_url)
path_reader = pd.read_json(os_path, lines=True, chunksize=1)
df_list = list(path_reader)
url_reader = pd.read_json(file_url, lines=True, chunksize=1)
for index, chuck in enumerate(url_reader):
tm.assert_frame_equal(chuck, df_list[index])