13
13
import numpy as np
14
14
15
15
import pandas ._libs .lib as lib
16
+ from pandas ._typing import Dtype
16
17
17
18
from pandas .core .dtypes .common import is_datetime64tz_dtype , is_dict_like , is_list_like
18
19
from pandas .core .dtypes .dtypes import DatetimeTZDtype
@@ -119,10 +120,15 @@ def _parse_date_columns(data_frame, parse_dates):
119
120
return data_frame
120
121
121
122
122
- def _wrap_result (data , columns , index_col = None , coerce_float = True , parse_dates = None ):
123
+ def _wrap_result (
124
+ data , columns , index_col = None , coerce_float = True , parse_dates = None , dtype = None
125
+ ):
123
126
"""Wrap result set of query in a DataFrame."""
124
127
frame = DataFrame .from_records (data , columns = columns , coerce_float = coerce_float )
125
128
129
+ if dtype :
130
+ frame = frame .astype (dtype )
131
+
126
132
frame = _parse_date_columns (frame , parse_dates )
127
133
128
134
if index_col is not None :
@@ -295,6 +301,7 @@ def read_sql_query(
295
301
params = None ,
296
302
parse_dates = None ,
297
303
chunksize : None = None ,
304
+ dtype : Optional [Dtype ] = None ,
298
305
) -> DataFrame :
299
306
...
300
307
@@ -308,6 +315,7 @@ def read_sql_query(
308
315
params = None ,
309
316
parse_dates = None ,
310
317
chunksize : int = 1 ,
318
+ dtype : Optional [Dtype ] = None ,
311
319
) -> Iterator [DataFrame ]:
312
320
...
313
321
@@ -320,6 +328,7 @@ def read_sql_query(
320
328
params = None ,
321
329
parse_dates = None ,
322
330
chunksize : Optional [int ] = None ,
331
+ dtype : Optional [Dtype ] = None ,
323
332
) -> Union [DataFrame , Iterator [DataFrame ]]:
324
333
"""
325
334
Read SQL query into a DataFrame.
@@ -381,6 +390,7 @@ def read_sql_query(
381
390
coerce_float = coerce_float ,
382
391
parse_dates = parse_dates ,
383
392
chunksize = chunksize ,
393
+ dtype = dtype ,
384
394
)
385
395
386
396
@@ -1225,7 +1235,13 @@ def read_table(
1225
1235
1226
1236
@staticmethod
1227
1237
def _query_iterator (
1228
- result , chunksize , columns , index_col = None , coerce_float = True , parse_dates = None
1238
+ result ,
1239
+ chunksize ,
1240
+ columns ,
1241
+ index_col = None ,
1242
+ coerce_float = True ,
1243
+ parse_dates = None ,
1244
+ dtype = None ,
1229
1245
):
1230
1246
"""Return generator through chunked result set"""
1231
1247
while True :
@@ -1239,6 +1255,7 @@ def _query_iterator(
1239
1255
index_col = index_col ,
1240
1256
coerce_float = coerce_float ,
1241
1257
parse_dates = parse_dates ,
1258
+ dtype = dtype ,
1242
1259
)
1243
1260
1244
1261
def read_query (
@@ -1249,6 +1266,7 @@ def read_query(
1249
1266
parse_dates = None ,
1250
1267
params = None ,
1251
1268
chunksize = None ,
1269
+ dtype = None ,
1252
1270
):
1253
1271
"""
1254
1272
Read SQL query into a DataFrame.
@@ -1304,6 +1322,7 @@ def read_query(
1304
1322
index_col = index_col ,
1305
1323
coerce_float = coerce_float ,
1306
1324
parse_dates = parse_dates ,
1325
+ dtype = dtype ,
1307
1326
)
1308
1327
else :
1309
1328
data = result .fetchall ()
@@ -1313,6 +1332,7 @@ def read_query(
1313
1332
index_col = index_col ,
1314
1333
coerce_float = coerce_float ,
1315
1334
parse_dates = parse_dates ,
1335
+ dtype = dtype ,
1316
1336
)
1317
1337
return frame
1318
1338
0 commit comments