77from datetime import date , datetime , time
88from functools import partial
99import re
10- from typing import Any , Dict , Iterator , List , Optional , Sequence , Union , overload
10+ from typing import Any , Dict , Iterator , List , Optional , Sequence , Union , cast , overload
1111import warnings
1212
1313import numpy as np
@@ -383,6 +383,8 @@ def read_sql_query(
383383 Data type for data or columns. E.g. np.float64 or
384384 {‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’}
385385
386+ .. versionadded:: 1.3.0
387+
386388 Returns
387389 -------
388390 DataFrame or Iterator[DataFrame]
@@ -609,7 +611,7 @@ def to_sql(
609611 index : bool = True ,
610612 index_label = None ,
611613 chunksize : Optional [int ] = None ,
612- dtype = None ,
614+ dtype : Optional [ DtypeArg ] = None ,
613615 method : Optional [str ] = None ,
614616) -> None :
615617 """
@@ -768,7 +770,7 @@ def __init__(
768770 index_label = None ,
769771 schema = None ,
770772 keys = None ,
771- dtype = None ,
773+ dtype : Optional [ DtypeArg ] = None ,
772774 ):
773775 self .name = name
774776 self .pd_sql = pandas_sql_engine
@@ -1108,9 +1110,11 @@ def _harmonize_columns(self, parse_dates=None):
11081110
11091111 def _sqlalchemy_type (self , col ):
11101112
1111- dtype = self .dtype or {}
1112- if col .name in dtype :
1113- return self .dtype [col .name ]
1113+ dtype : DtypeArg = self .dtype or {}
1114+ if is_dict_like (dtype ):
1115+ dtype = cast (dict , dtype )
1116+ if col .name in dtype :
1117+ return dtype [col .name ]
11141118
11151119 # Infer type of column, while ignoring missing values.
11161120 # Needed for inserting typed data containing NULLs, GH 8778.
@@ -1209,7 +1213,18 @@ def read_sql(self, *args, **kwargs):
12091213 "connectable or sqlite connection"
12101214 )
12111215
1212- def to_sql (self , * args , ** kwargs ):
1216+ def to_sql (
1217+ self ,
1218+ frame ,
1219+ name ,
1220+ if_exists = "fail" ,
1221+ index = True ,
1222+ index_label = None ,
1223+ schema = None ,
1224+ chunksize = None ,
1225+ dtype : Optional [DtypeArg ] = None ,
1226+ method = None ,
1227+ ):
12131228 raise ValueError (
12141229 "PandasSQL must be created with an SQLAlchemy "
12151230 "connectable or sqlite connection"
@@ -1436,7 +1451,7 @@ def to_sql(
14361451 index_label = None ,
14371452 schema = None ,
14381453 chunksize = None ,
1439- dtype = None ,
1454+ dtype : Optional [ DtypeArg ] = None ,
14401455 method = None ,
14411456 ):
14421457 """
@@ -1480,10 +1495,12 @@ def to_sql(
14801495
14811496 .. versionadded:: 0.24.0
14821497 """
1483- if dtype and not is_dict_like (dtype ):
1484- dtype = {col_name : dtype for col_name in frame }
1498+ if dtype :
1499+ if not is_dict_like (dtype ):
1500+ dtype = {col_name : dtype for col_name in frame }
1501+ else :
1502+ dtype = cast (dict , dtype )
14851503
1486- if dtype is not None :
14871504 from sqlalchemy .types import TypeEngine , to_instance
14881505
14891506 for col , my_type in dtype .items ():
@@ -1569,7 +1586,7 @@ def _create_sql_schema(
15691586 frame : DataFrame ,
15701587 table_name : str ,
15711588 keys : Optional [List [str ]] = None ,
1572- dtype : Optional [dict ] = None ,
1589+ dtype : Optional [DtypeArg ] = None ,
15731590 schema : Optional [str ] = None ,
15741591 ):
15751592 table = SQLTable (
@@ -1740,9 +1757,11 @@ def _create_table_setup(self):
17401757 return create_stmts
17411758
17421759 def _sql_type_name (self , col ):
1743- dtype = self .dtype or {}
1744- if col .name in dtype :
1745- return dtype [col .name ]
1760+ dtype : DtypeArg = self .dtype or {}
1761+ if is_dict_like (dtype ):
1762+ dtype = cast (dict , dtype )
1763+ if col .name in dtype :
1764+ return dtype [col .name ]
17461765
17471766 # Infer type of column, while ignoring missing values.
17481767 # Needed for inserting typed data containing NULLs, GH 8778.
@@ -1901,7 +1920,7 @@ def to_sql(
19011920 index_label = None ,
19021921 schema = None ,
19031922 chunksize = None ,
1904- dtype = None ,
1923+ dtype : Optional [ DtypeArg ] = None ,
19051924 method = None ,
19061925 ):
19071926 """
@@ -1944,10 +1963,12 @@ def to_sql(
19441963
19451964 .. versionadded:: 0.24.0
19461965 """
1947- if dtype and not is_dict_like (dtype ):
1948- dtype = {col_name : dtype for col_name in frame }
1966+ if dtype :
1967+ if not is_dict_like (dtype ):
1968+ dtype = {col_name : dtype for col_name in frame }
1969+ else :
1970+ dtype = cast (dict , dtype )
19491971
1950- if dtype is not None :
19511972 for col , my_type in dtype .items ():
19521973 if not isinstance (my_type , str ):
19531974 raise ValueError (f"{ col } ({ my_type } ) not a string" )
@@ -1986,7 +2007,7 @@ def _create_sql_schema(
19862007 frame ,
19872008 table_name : str ,
19882009 keys = None ,
1989- dtype = None ,
2010+ dtype : Optional [ DtypeArg ] = None ,
19902011 schema : Optional [str ] = None ,
19912012 ):
19922013 table = SQLiteTable (
@@ -2002,7 +2023,12 @@ def _create_sql_schema(
20022023
20032024
20042025def get_schema (
2005- frame , name : str , keys = None , con = None , dtype = None , schema : Optional [str ] = None
2026+ frame ,
2027+ name : str ,
2028+ keys = None ,
2029+ con = None ,
2030+ dtype : Optional [DtypeArg ] = None ,
2031+ schema : Optional [str ] = None ,
20062032):
20072033 """
20082034 Get the SQL db table schema for the given frame.
0 commit comments