Skip to content

Commit bd469a0

Browse files
_query_iterator now returns a list with an empty pd.DataFrame if chunksize is set and the resultset is empty
1 parent 333db4b commit bd469a0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pandas/io/sql.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,11 +837,17 @@ def _query_iterator(
837837
self, result, chunksize, columns, coerce_float=True, parse_dates=None
838838
):
839839
"""Return generator through chunked result set."""
840+
has_read_data = False
840841
while True:
841842
data = result.fetchmany(chunksize)
842843
if not data:
844+
if not has_read_data:
845+
yield DataFrame.from_records(
846+
[], columns=columns, coerce_float=coerce_float
847+
)
843848
break
844849
else:
850+
has_read_data = True
845851
self.frame = DataFrame.from_records(
846852
data, columns=columns, coerce_float=coerce_float
847853
)
@@ -1229,11 +1235,21 @@ def _query_iterator(
12291235
result, chunksize, columns, index_col=None, coerce_float=True, parse_dates=None
12301236
):
12311237
"""Return generator through chunked result set"""
1238+
has_read_data = False
12321239
while True:
12331240
data = result.fetchmany(chunksize)
12341241
if not data:
1242+
if not has_read_data:
1243+
yield _wrap_result(
1244+
[],
1245+
columns,
1246+
index_col=index_col,
1247+
coerce_float=coerce_float,
1248+
parse_dates=parse_dates
1249+
)
12351250
break
12361251
else:
1252+
has_read_data = True
12371253
yield _wrap_result(
12381254
data,
12391255
columns,
@@ -1686,14 +1702,20 @@ def _query_iterator(
16861702
cursor, chunksize, columns, index_col=None, coerce_float=True, parse_dates=None
16871703
):
16881704
"""Return generator through chunked result set"""
1705+
has_read_data = False
16891706
while True:
16901707
data = cursor.fetchmany(chunksize)
16911708
if type(data) == tuple:
16921709
data = list(data)
16931710
if not data:
16941711
cursor.close()
1712+
if not has_read_data:
1713+
yield DataFrame.from_records(
1714+
[], columns=columns, coerce_float=coerce_float
1715+
)
16951716
break
16961717
else:
1718+
has_read_data = True
16971719
yield _wrap_result(
16981720
data,
16991721
columns,

pandas/tests/io/test_sql.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,14 @@ def test_read_sql_view(self):
601601
iris_frame = sql.read_sql_query("SELECT * FROM iris_view", self.conn)
602602
self._check_iris_loaded_frame(iris_frame)
603603

604+
def test_read_sql_with_chunksize_no_result(self):
605+
with_batch = sql.read_sql_query("SELECT * FROM iris_view WHERE SepalLength < 0.0", self.conn, chunksize=5)
606+
without_batch = sql.read_sql_query("SELECT * FROM iris_view WHERE SepalLength < 0.0", self.conn)
607+
tm.assert_frame_equal(
608+
pd.concat(with_batch),
609+
without_batch
610+
)
611+
604612
def test_to_sql(self):
605613
sql.to_sql(self.test_frame1, "test_frame1", self.conn)
606614
assert sql.has_table("test_frame1", self.conn)

0 commit comments

Comments
 (0)