Skip to content

Commit 075947f

Browse files
authored
BUG: sql ignores dtype when chunksize is set and result is empty (#50288)
1 parent cbf80c2 commit 075947f

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,7 @@ I/O
872872
- Bug in :func:`read_sas` caused fragmentation of :class:`DataFrame` and raised :class:`.errors.PerformanceWarning` (:issue:`48595`)
873873
- Improved error message in :func:`read_excel` by including the offending sheet name when an exception is raised while reading a file (:issue:`48706`)
874874
- Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`)
875+
- Bug in :func:`read_sql_query` ignoring ``dtype`` argument when ``chunksize`` is specified and result is empty (:issue:`50245`)
875876
- Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`)
876877
- Bug in displaying ``string`` dtypes not showing storage option (:issue:`50099`)
877878
- Bug in :func:`DataFrame.to_string` with ``header=False`` that printed the index name on the same line as the first row of the data (:issue:`49230`)

pandas/io/sql.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,7 @@ def _query_iterator(
15971597
index_col=index_col,
15981598
coerce_float=coerce_float,
15991599
parse_dates=parse_dates,
1600+
dtype=dtype,
16001601
use_nullable_dtypes=use_nullable_dtypes,
16011602
)
16021603
break
@@ -2162,9 +2163,12 @@ def _query_iterator(
21622163
if not data:
21632164
cursor.close()
21642165
if not has_read_data:
2165-
yield DataFrame.from_records(
2166+
result = DataFrame.from_records(
21662167
[], columns=columns, coerce_float=coerce_float
21672168
)
2169+
if dtype:
2170+
result = result.astype(dtype)
2171+
yield result
21682172
break
21692173

21702174
has_read_data = True

pandas/tests/io/test_sql.py

+15
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,21 @@ def nullable_expected(self, storage) -> DataFrame:
23622362
}
23632363
)
23642364

2365+
def test_chunksize_empty_dtypes(self):
2366+
# GH#50245
2367+
dtypes = {"a": "int64", "b": "object"}
2368+
df = DataFrame(columns=["a", "b"]).astype(dtypes)
2369+
expected = df.copy()
2370+
df.to_sql("test", self.conn, index=False, if_exists="replace")
2371+
2372+
for result in read_sql_query(
2373+
"SELECT * FROM test",
2374+
self.conn,
2375+
dtype=dtypes,
2376+
chunksize=1,
2377+
):
2378+
tm.assert_frame_equal(result, expected)
2379+
23652380

23662381
class TestSQLiteAlchemy(_TestSQLAlchemy):
23672382
"""

0 commit comments

Comments
 (0)