13
13
import numpy as np
14
14
15
15
import pandas ._libs .lib as lib
16
+ from pandas ._typing import DtypeArg
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
@@ -132,10 +133,14 @@ def _wrap_result(
132
133
index_col = None ,
133
134
coerce_float : bool = True ,
134
135
parse_dates = None ,
136
+ dtype : Optional [DtypeArg ] = None ,
135
137
):
136
138
"""Wrap result set of query in a DataFrame."""
137
139
frame = DataFrame .from_records (data , columns = columns , coerce_float = coerce_float )
138
140
141
+ if dtype :
142
+ frame = frame .astype (dtype )
143
+
139
144
frame = _parse_date_columns (frame , parse_dates )
140
145
141
146
if index_col is not None :
@@ -308,6 +313,7 @@ def read_sql_query(
308
313
params = None ,
309
314
parse_dates = None ,
310
315
chunksize : None = None ,
316
+ dtype : Optional [DtypeArg ] = None ,
311
317
) -> DataFrame :
312
318
...
313
319
@@ -321,6 +327,7 @@ def read_sql_query(
321
327
params = None ,
322
328
parse_dates = None ,
323
329
chunksize : int = 1 ,
330
+ dtype : Optional [DtypeArg ] = None ,
324
331
) -> Iterator [DataFrame ]:
325
332
...
326
333
@@ -333,6 +340,7 @@ def read_sql_query(
333
340
params = None ,
334
341
parse_dates = None ,
335
342
chunksize : Optional [int ] = None ,
343
+ dtype : Optional [DtypeArg ] = None ,
336
344
) -> Union [DataFrame , Iterator [DataFrame ]]:
337
345
"""
338
346
Read SQL query into a DataFrame.
@@ -371,6 +379,9 @@ def read_sql_query(
371
379
chunksize : int, default None
372
380
If specified, return an iterator where `chunksize` is the number of
373
381
rows to include in each chunk.
382
+ dtype : Type name or dict of columns
383
+ Data type for data or columns. E.g. np.float64 or
384
+ {‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’}
374
385
375
386
Returns
376
387
-------
@@ -394,6 +405,7 @@ def read_sql_query(
394
405
coerce_float = coerce_float ,
395
406
parse_dates = parse_dates ,
396
407
chunksize = chunksize ,
408
+ dtype = dtype ,
397
409
)
398
410
399
411
@@ -1307,6 +1319,7 @@ def _query_iterator(
1307
1319
index_col = None ,
1308
1320
coerce_float = True ,
1309
1321
parse_dates = None ,
1322
+ dtype : Optional [DtypeArg ] = None ,
1310
1323
):
1311
1324
"""Return generator through chunked result set"""
1312
1325
while True :
@@ -1320,6 +1333,7 @@ def _query_iterator(
1320
1333
index_col = index_col ,
1321
1334
coerce_float = coerce_float ,
1322
1335
parse_dates = parse_dates ,
1336
+ dtype = dtype ,
1323
1337
)
1324
1338
1325
1339
def read_query (
@@ -1330,6 +1344,7 @@ def read_query(
1330
1344
parse_dates = None ,
1331
1345
params = None ,
1332
1346
chunksize : Optional [int ] = None ,
1347
+ dtype : Optional [DtypeArg ] = None ,
1333
1348
):
1334
1349
"""
1335
1350
Read SQL query into a DataFrame.
@@ -1361,6 +1376,11 @@ def read_query(
1361
1376
chunksize : int, default None
1362
1377
If specified, return an iterator where `chunksize` is the number
1363
1378
of rows to include in each chunk.
1379
+ dtype : Type name or dict of columns
1380
+ Data type for data or columns. E.g. np.float64 or
1381
+ {‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’}
1382
+
1383
+ .. versionadded:: 1.3.0
1364
1384
1365
1385
Returns
1366
1386
-------
@@ -1385,6 +1405,7 @@ def read_query(
1385
1405
index_col = index_col ,
1386
1406
coerce_float = coerce_float ,
1387
1407
parse_dates = parse_dates ,
1408
+ dtype = dtype ,
1388
1409
)
1389
1410
else :
1390
1411
data = result .fetchall ()
@@ -1394,6 +1415,7 @@ def read_query(
1394
1415
index_col = index_col ,
1395
1416
coerce_float = coerce_float ,
1396
1417
parse_dates = parse_dates ,
1418
+ dtype = dtype ,
1397
1419
)
1398
1420
return frame
1399
1421
@@ -1799,6 +1821,7 @@ def _query_iterator(
1799
1821
index_col = None ,
1800
1822
coerce_float : bool = True ,
1801
1823
parse_dates = None ,
1824
+ dtype : Optional [DtypeArg ] = None ,
1802
1825
):
1803
1826
"""Return generator through chunked result set"""
1804
1827
while True :
@@ -1815,6 +1838,7 @@ def _query_iterator(
1815
1838
index_col = index_col ,
1816
1839
coerce_float = coerce_float ,
1817
1840
parse_dates = parse_dates ,
1841
+ dtype = dtype ,
1818
1842
)
1819
1843
1820
1844
def read_query (
@@ -1825,6 +1849,7 @@ def read_query(
1825
1849
params = None ,
1826
1850
parse_dates = None ,
1827
1851
chunksize : Optional [int ] = None ,
1852
+ dtype : Optional [DtypeArg ] = None ,
1828
1853
):
1829
1854
1830
1855
args = _convert_params (sql , params )
@@ -1839,6 +1864,7 @@ def read_query(
1839
1864
index_col = index_col ,
1840
1865
coerce_float = coerce_float ,
1841
1866
parse_dates = parse_dates ,
1867
+ dtype = dtype ,
1842
1868
)
1843
1869
else :
1844
1870
data = self ._fetchall_as_list (cursor )
@@ -1850,6 +1876,7 @@ def read_query(
1850
1876
index_col = index_col ,
1851
1877
coerce_float = coerce_float ,
1852
1878
parse_dates = parse_dates ,
1879
+ dtype = dtype ,
1853
1880
)
1854
1881
return frame
1855
1882
0 commit comments