Skip to content

Commit b6a9499

Browse files
committed
use StringIO iterator for nrows as used in chunks
1 parent 730d6d8 commit b6a9499

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

pandas/io/json/_json.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from io import StringIO
44
from itertools import islice
55
import os
6-
import re
76
from typing import Any, Callable, Optional, Type
87

98
import numpy as np
@@ -507,6 +506,7 @@ def read_json(
507506
The number of lines from the line-delimited jsonfile that has to be read.
508507
This can only be passed if `lines=True`.
509508
If this is None, all the rows will be returned.
509+
510510
.. versionadded:: 1.1
511511
512512
Returns
@@ -688,9 +688,9 @@ def _preprocess_data(self, data):
688688
If self.chunksize, we prepare the data for the `__next__` method.
689689
Otherwise, we read it into memory for the `read` method.
690690
"""
691-
if hasattr(data, "read") and not self.chunksize:
691+
if hasattr(data, "read") and (not self.chunksize or not self.nrows):
692692
data = data.read()
693-
if not hasattr(data, "read") and self.chunksize:
693+
if not hasattr(data, "read") and (self.chunksize or self.nrows):
694694
data = StringIO(data)
695695

696696
return data
@@ -738,17 +738,17 @@ def read(self):
738738
"""
739739
Read the whole JSON input into a pandas object.
740740
"""
741-
if self.lines and self.chunksize:
742-
obj = concat(self)
743-
elif self.lines:
744-
data = ensure_str(self.data)
745-
if self.nrows:
746-
compiled_pattern = re.compile(".*\n")
747-
data = (line.group(0) for line in compiled_pattern.finditer(data))
748-
data = islice(data, self.nrows)
741+
if self.lines:
742+
if self.chunksize:
743+
obj = concat(self)
744+
elif self.nrows:
745+
lines = list(islice(self.data, self.nrows))
746+
lines_json = self._combine_lines(lines)
747+
obj = self._get_object_parser(lines_json)
749748
else:
749+
data = ensure_str(self.data)
750750
data = data.split("\n")
751-
obj = self._get_object_parser(self._combine_lines(data))
751+
obj = self._get_object_parser(self._combine_lines(data))
752752
else:
753753
obj = self._get_object_parser(self.data)
754754
self.close()

0 commit comments

Comments
 (0)