Skip to content

BUG: sql ignores dtype when chunksize is set and result is empty #50288

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 1 commit into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ I/O
- Bug in :func:`read_sas` caused fragmentation of :class:`DataFrame` and raised :class:`.errors.PerformanceWarning` (:issue:`48595`)
- Improved error message in :func:`read_excel` by including the offending sheet name when an exception is raised while reading a file (:issue:`48706`)
- Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`)
- Bug in :func:`read_sql_query` ignoring ``dtype`` argument when ``chunksize`` is specified and result is empty (:issue:`50245`)
- Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`)
- Bug in displaying ``string`` dtypes not showing storage option (:issue:`50099`)
- 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`)
Expand Down
6 changes: 5 additions & 1 deletion pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,7 @@ def _query_iterator(
index_col=index_col,
coerce_float=coerce_float,
parse_dates=parse_dates,
dtype=dtype,
use_nullable_dtypes=use_nullable_dtypes,
)
break
Expand Down Expand Up @@ -2162,9 +2163,12 @@ def _query_iterator(
if not data:
cursor.close()
if not has_read_data:
yield DataFrame.from_records(
result = DataFrame.from_records(
[], columns=columns, coerce_float=coerce_float
)
if dtype:
result = result.astype(dtype)
yield result
break

has_read_data = True
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,21 @@ def nullable_expected(self, storage) -> DataFrame:
}
)

def test_chunksize_empty_dtypes(self):
# GH#50245
dtypes = {"a": "int64", "b": "object"}
df = DataFrame(columns=["a", "b"]).astype(dtypes)
expected = df.copy()
df.to_sql("test", self.conn, index=False, if_exists="replace")

for result in read_sql_query(
"SELECT * FROM test",
self.conn,
dtype=dtypes,
chunksize=1,
):
tm.assert_frame_equal(result, expected)


class TestSQLiteAlchemy(_TestSQLAlchemy):
"""
Expand Down