Skip to content

Commit 330caa3

Browse files
committed
BUG: sql ignores dtype when chunksize is set and result is empty (pandas-dev#50288)
1 parent 6845411 commit 330caa3

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
@@ -874,6 +874,7 @@ I/O
874874
- Bug in :func:`read_sas` caused fragmentation of :class:`DataFrame` and raised :class:`.errors.PerformanceWarning` (:issue:`48595`)
875875
- Improved error message in :func:`read_excel` by including the offending sheet name when an exception is raised while reading a file (:issue:`48706`)
876876
- Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`)
877+
- Bug in :func:`read_sql_query` ignoring ``dtype`` argument when ``chunksize`` is specified and result is empty (:issue:`50245`)
877878
- Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`)
878879
- Bug in displaying ``string`` dtypes not showing storage option (:issue:`50099`)
879880
- 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
@@ -1617,6 +1617,7 @@ def _query_iterator(
16171617
index_col=index_col,
16181618
coerce_float=coerce_float,
16191619
parse_dates=parse_dates,
1620+
dtype=dtype,
16201621
use_nullable_dtypes=use_nullable_dtypes,
16211622
)
16221623
break
@@ -2182,9 +2183,12 @@ def _query_iterator(
21822183
if not data:
21832184
cursor.close()
21842185
if not has_read_data:
2185-
yield DataFrame.from_records(
2186+
result = DataFrame.from_records(
21862187
[], columns=columns, coerce_float=coerce_float
21872188
)
2189+
if dtype:
2190+
result = result.astype(dtype)
2191+
yield result
21882192
break
21892193

21902194
has_read_data = True

pandas/tests/io/test_sql.py

+15
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,21 @@ def nullable_expected(self, storage) -> DataFrame:
23642364
}
23652365
)
23662366

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

23682383
class TestSQLiteAlchemy(_TestSQLAlchemy):
23692384
"""

0 commit comments

Comments
 (0)