7
7
from datetime import date , datetime , time
8
8
from functools import partial
9
9
import 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
11
11
import warnings
12
12
13
13
import numpy as np
@@ -383,6 +383,8 @@ def read_sql_query(
383
383
Data type for data or columns. E.g. np.float64 or
384
384
{‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’}
385
385
386
+ .. versionadded:: 1.3.0
387
+
386
388
Returns
387
389
-------
388
390
DataFrame or Iterator[DataFrame]
@@ -609,7 +611,7 @@ def to_sql(
609
611
index : bool = True ,
610
612
index_label = None ,
611
613
chunksize : Optional [int ] = None ,
612
- dtype = None ,
614
+ dtype : Optional [ DtypeArg ] = None ,
613
615
method : Optional [str ] = None ,
614
616
) -> None :
615
617
"""
@@ -768,7 +770,7 @@ def __init__(
768
770
index_label = None ,
769
771
schema = None ,
770
772
keys = None ,
771
- dtype = None ,
773
+ dtype : Optional [ DtypeArg ] = None ,
772
774
):
773
775
self .name = name
774
776
self .pd_sql = pandas_sql_engine
@@ -1108,9 +1110,11 @@ def _harmonize_columns(self, parse_dates=None):
1108
1110
1109
1111
def _sqlalchemy_type (self , col ):
1110
1112
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 ]
1114
1118
1115
1119
# Infer type of column, while ignoring missing values.
1116
1120
# Needed for inserting typed data containing NULLs, GH 8778.
@@ -1209,7 +1213,18 @@ def read_sql(self, *args, **kwargs):
1209
1213
"connectable or sqlite connection"
1210
1214
)
1211
1215
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
+ ):
1213
1228
raise ValueError (
1214
1229
"PandasSQL must be created with an SQLAlchemy "
1215
1230
"connectable or sqlite connection"
@@ -1436,7 +1451,7 @@ def to_sql(
1436
1451
index_label = None ,
1437
1452
schema = None ,
1438
1453
chunksize = None ,
1439
- dtype = None ,
1454
+ dtype : Optional [ DtypeArg ] = None ,
1440
1455
method = None ,
1441
1456
):
1442
1457
"""
@@ -1480,10 +1495,12 @@ def to_sql(
1480
1495
1481
1496
.. versionadded:: 0.24.0
1482
1497
"""
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 )
1485
1503
1486
- if dtype is not None :
1487
1504
from sqlalchemy .types import TypeEngine , to_instance
1488
1505
1489
1506
for col , my_type in dtype .items ():
@@ -1569,7 +1586,7 @@ def _create_sql_schema(
1569
1586
frame : DataFrame ,
1570
1587
table_name : str ,
1571
1588
keys : Optional [List [str ]] = None ,
1572
- dtype : Optional [dict ] = None ,
1589
+ dtype : Optional [DtypeArg ] = None ,
1573
1590
schema : Optional [str ] = None ,
1574
1591
):
1575
1592
table = SQLTable (
@@ -1740,9 +1757,11 @@ def _create_table_setup(self):
1740
1757
return create_stmts
1741
1758
1742
1759
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 ]
1746
1765
1747
1766
# Infer type of column, while ignoring missing values.
1748
1767
# Needed for inserting typed data containing NULLs, GH 8778.
@@ -1901,7 +1920,7 @@ def to_sql(
1901
1920
index_label = None ,
1902
1921
schema = None ,
1903
1922
chunksize = None ,
1904
- dtype = None ,
1923
+ dtype : Optional [ DtypeArg ] = None ,
1905
1924
method = None ,
1906
1925
):
1907
1926
"""
@@ -1944,10 +1963,12 @@ def to_sql(
1944
1963
1945
1964
.. versionadded:: 0.24.0
1946
1965
"""
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 )
1949
1971
1950
- if dtype is not None :
1951
1972
for col , my_type in dtype .items ():
1952
1973
if not isinstance (my_type , str ):
1953
1974
raise ValueError (f"{ col } ({ my_type } ) not a string" )
@@ -1986,7 +2007,7 @@ def _create_sql_schema(
1986
2007
frame ,
1987
2008
table_name : str ,
1988
2009
keys = None ,
1989
- dtype = None ,
2010
+ dtype : Optional [ DtypeArg ] = None ,
1990
2011
schema : Optional [str ] = None ,
1991
2012
):
1992
2013
table = SQLiteTable (
@@ -2002,7 +2023,12 @@ def _create_sql_schema(
2002
2023
2003
2024
2004
2025
def 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 ,
2006
2032
):
2007
2033
"""
2008
2034
Get the SQL db table schema for the given frame.
0 commit comments