7
7
from datetime import date , datetime , time
8
8
from functools import partial
9
9
import re
10
- from typing import Iterator , List , Optional , Union , overload
10
+ from typing import Any , Dict , Iterator , List , Optional , Sequence , Union , overload
11
11
import warnings
12
12
13
13
import numpy as np
@@ -77,7 +77,9 @@ def _process_parse_dates_argument(parse_dates):
77
77
return parse_dates
78
78
79
79
80
- def _handle_date_column (col , utc = None , format = None ):
80
+ def _handle_date_column (
81
+ col , utc : Optional [bool ] = None , format : Optional [Union [str , Dict [str , Any ]]] = None
82
+ ):
81
83
if isinstance (format , dict ):
82
84
# GH35185 Allow custom error values in parse_dates argument of
83
85
# read_sql like functions.
@@ -124,7 +126,13 @@ def _parse_date_columns(data_frame, parse_dates):
124
126
return data_frame
125
127
126
128
127
- def _wrap_result (data , columns , index_col = None , coerce_float = True , parse_dates = None ):
129
+ def _wrap_result (
130
+ data ,
131
+ columns ,
132
+ index_col = None ,
133
+ coerce_float : bool = True ,
134
+ parse_dates = None ,
135
+ ):
128
136
"""Wrap result set of query in a DataFrame."""
129
137
frame = DataFrame .from_records (data , columns = columns , coerce_float = coerce_float )
130
138
@@ -197,11 +205,11 @@ def read_sql_table(
197
205
198
206
199
207
def read_sql_table (
200
- table_name ,
208
+ table_name : str ,
201
209
con ,
202
- schema = None ,
203
- index_col = None ,
204
- coerce_float = True ,
210
+ schema : Optional [ str ] = None ,
211
+ index_col : Optional [ Union [ str , Sequence [ str ]]] = None ,
212
+ coerce_float : bool = True ,
205
213
parse_dates = None ,
206
214
columns = None ,
207
215
chunksize : Optional [int ] = None ,
@@ -321,7 +329,7 @@ def read_sql_query(
321
329
sql ,
322
330
con ,
323
331
index_col = None ,
324
- coerce_float = True ,
332
+ coerce_float : bool = True ,
325
333
params = None ,
326
334
parse_dates = None ,
327
335
chunksize : Optional [int ] = None ,
@@ -420,8 +428,8 @@ def read_sql(
420
428
def read_sql (
421
429
sql ,
422
430
con ,
423
- index_col = None ,
424
- coerce_float = True ,
431
+ index_col : Optional [ Union [ str , Sequence [ str ]]] = None ,
432
+ coerce_float : bool = True ,
425
433
params = None ,
426
434
parse_dates = None ,
427
435
columns = None ,
@@ -582,15 +590,15 @@ def read_sql(
582
590
583
591
def to_sql (
584
592
frame ,
585
- name ,
593
+ name : str ,
586
594
con ,
587
- schema = None ,
588
- if_exists = "fail" ,
589
- index = True ,
595
+ schema : Optional [ str ] = None ,
596
+ if_exists : str = "fail" ,
597
+ index : bool = True ,
590
598
index_label = None ,
591
- chunksize = None ,
599
+ chunksize : Optional [ int ] = None ,
592
600
dtype = None ,
593
- method = None ,
601
+ method : Optional [ str ] = None ,
594
602
) -> None :
595
603
"""
596
604
Write records stored in a DataFrame to a SQL database.
@@ -663,7 +671,7 @@ def to_sql(
663
671
)
664
672
665
673
666
- def has_table (table_name , con , schema = None ):
674
+ def has_table (table_name : str , con , schema : Optional [ str ] = None ):
667
675
"""
668
676
Check if DataBase has named table.
669
677
@@ -708,7 +716,9 @@ def _engine_builder(con):
708
716
return con
709
717
710
718
711
- def pandasSQL_builder (con , schema = None , meta = None , is_cursor = False ):
719
+ def pandasSQL_builder (
720
+ con , schema : Optional [str ] = None , meta = None , is_cursor : bool = False
721
+ ):
712
722
"""
713
723
Convenience function to return the correct PandasSQL subclass based on the
714
724
provided parameters.
@@ -737,7 +747,7 @@ class SQLTable(PandasObject):
737
747
738
748
def __init__ (
739
749
self ,
740
- name ,
750
+ name : str ,
741
751
pandas_sql_engine ,
742
752
frame = None ,
743
753
index = True ,
@@ -795,7 +805,7 @@ def create(self):
795
805
else :
796
806
self ._execute_create ()
797
807
798
- def _execute_insert (self , conn , keys , data_iter ):
808
+ def _execute_insert (self , conn , keys : List [ str ] , data_iter ):
799
809
"""
800
810
Execute SQL statement inserting data
801
811
@@ -810,7 +820,7 @@ def _execute_insert(self, conn, keys, data_iter):
810
820
data = [dict (zip (keys , row )) for row in data_iter ]
811
821
conn .execute (self .table .insert (), data )
812
822
813
- def _execute_insert_multi (self , conn , keys , data_iter ):
823
+ def _execute_insert_multi (self , conn , keys : List [ str ] , data_iter ):
814
824
"""
815
825
Alternative to _execute_insert for DBs support multivalue INSERT.
816
826
@@ -857,7 +867,7 @@ def insert_data(self):
857
867
858
868
return column_names , data_list
859
869
860
- def insert (self , chunksize = None , method = None ):
870
+ def insert (self , chunksize : Optional [ int ] = None , method : Optional [ str ] = None ):
861
871
862
872
# set insert method
863
873
if method is None :
@@ -894,7 +904,12 @@ def insert(self, chunksize=None, method=None):
894
904
exec_insert (conn , keys , chunk_iter )
895
905
896
906
def _query_iterator (
897
- self , result , chunksize , columns , coerce_float = True , parse_dates = None
907
+ self ,
908
+ result ,
909
+ chunksize : Optional [str ],
910
+ columns ,
911
+ coerce_float : bool = True ,
912
+ parse_dates = None ,
898
913
):
899
914
"""Return generator through chunked result set."""
900
915
while True :
@@ -1203,7 +1218,7 @@ class SQLDatabase(PandasSQL):
1203
1218
1204
1219
"""
1205
1220
1206
- def __init__ (self , engine , schema = None , meta = None ):
1221
+ def __init__ (self , engine , schema : Optional [ str ] = None , meta = None ):
1207
1222
self .connectable = engine
1208
1223
if not meta :
1209
1224
from sqlalchemy .schema import MetaData
@@ -1228,13 +1243,13 @@ def execute(self, *args, **kwargs):
1228
1243
1229
1244
def read_table (
1230
1245
self ,
1231
- table_name ,
1232
- index_col = None ,
1233
- coerce_float = True ,
1246
+ table_name : str ,
1247
+ index_col : Optional [ Union [ str , Sequence [ str ]]] = None ,
1248
+ coerce_float : bool = True ,
1234
1249
parse_dates = None ,
1235
1250
columns = None ,
1236
- schema = None ,
1237
- chunksize = None ,
1251
+ schema : Optional [ str ] = None ,
1252
+ chunksize : Optional [ int ] = None ,
1238
1253
):
1239
1254
"""
1240
1255
Read SQL database table into a DataFrame.
@@ -1288,7 +1303,12 @@ def read_table(
1288
1303
1289
1304
@staticmethod
1290
1305
def _query_iterator (
1291
- result , chunksize , columns , index_col = None , coerce_float = True , parse_dates = None
1306
+ result ,
1307
+ chunksize : int ,
1308
+ columns ,
1309
+ index_col = None ,
1310
+ coerce_float = True ,
1311
+ parse_dates = None ,
1292
1312
):
1293
1313
"""Return generator through chunked result set"""
1294
1314
while True :
@@ -1306,12 +1326,12 @@ def _query_iterator(
1306
1326
1307
1327
def read_query (
1308
1328
self ,
1309
- sql ,
1310
- index_col = None ,
1311
- coerce_float = True ,
1329
+ sql : str ,
1330
+ index_col : Optional [ str ] = None ,
1331
+ coerce_float : bool = True ,
1312
1332
parse_dates = None ,
1313
1333
params = None ,
1314
- chunksize = None ,
1334
+ chunksize : Optional [ int ] = None ,
1315
1335
):
1316
1336
"""
1317
1337
Read SQL query into a DataFrame.
@@ -1490,12 +1510,12 @@ def to_sql(
1490
1510
def tables (self ):
1491
1511
return self .meta .tables
1492
1512
1493
- def has_table (self , name , schema = None ):
1513
+ def has_table (self , name : str , schema : Optional [ str ] = None ):
1494
1514
return self .connectable .run_callable (
1495
1515
self .connectable .dialect .has_table , name , schema or self .meta .schema
1496
1516
)
1497
1517
1498
- def get_table (self , table_name , schema = None ):
1518
+ def get_table (self , table_name : str , schema : Optional [ str ] = None ):
1499
1519
schema = schema or self .meta .schema
1500
1520
if schema :
1501
1521
tbl = self .meta .tables .get ("." .join ([schema , table_name ]))
@@ -1511,7 +1531,7 @@ def get_table(self, table_name, schema=None):
1511
1531
1512
1532
return tbl
1513
1533
1514
- def drop_table (self , table_name , schema = None ):
1534
+ def drop_table (self , table_name : str , schema : Optional [ str ] = None ):
1515
1535
schema = schema or self .meta .schema
1516
1536
if self .has_table (table_name , schema ):
1517
1537
self .meta .reflect (only = [table_name ], schema = schema )
@@ -1608,7 +1628,7 @@ def _execute_create(self):
1608
1628
for stmt in self .table :
1609
1629
conn .execute (stmt )
1610
1630
1611
- def insert_statement (self , * , num_rows ):
1631
+ def insert_statement (self , * , num_rows : int ):
1612
1632
names = list (map (str , self .frame .columns ))
1613
1633
wld = "?" # wildcard char
1614
1634
escape = _get_valid_sqlite_name
@@ -1737,7 +1757,7 @@ class SQLiteDatabase(PandasSQL):
1737
1757
1738
1758
"""
1739
1759
1740
- def __init__ (self , con , is_cursor = False ):
1760
+ def __init__ (self , con , is_cursor : bool = False ):
1741
1761
self .is_cursor = is_cursor
1742
1762
self .con = con
1743
1763
@@ -1775,7 +1795,12 @@ def execute(self, *args, **kwargs):
1775
1795
1776
1796
@staticmethod
1777
1797
def _query_iterator (
1778
- cursor , chunksize , columns , index_col = None , coerce_float = True , parse_dates = None
1798
+ cursor ,
1799
+ chunksize : int ,
1800
+ columns ,
1801
+ index_col = None ,
1802
+ coerce_float : bool = True ,
1803
+ parse_dates = None ,
1779
1804
):
1780
1805
"""Return generator through chunked result set"""
1781
1806
while True :
@@ -1798,10 +1823,10 @@ def read_query(
1798
1823
self ,
1799
1824
sql ,
1800
1825
index_col = None ,
1801
- coerce_float = True ,
1826
+ coerce_float : bool = True ,
1802
1827
params = None ,
1803
1828
parse_dates = None ,
1804
- chunksize = None ,
1829
+ chunksize : Optional [ int ] = None ,
1805
1830
):
1806
1831
1807
1832
args = _convert_params (sql , params )
@@ -1908,7 +1933,7 @@ def to_sql(
1908
1933
table .create ()
1909
1934
table .insert (chunksize , method )
1910
1935
1911
- def has_table (self , name , schema = None ):
1936
+ def has_table (self , name : str , schema : Optional [ str ] = None ):
1912
1937
# TODO(wesm): unused?
1913
1938
# escape = _get_valid_sqlite_name
1914
1939
# esc_name = escape(name)
@@ -1918,14 +1943,21 @@ def has_table(self, name, schema=None):
1918
1943
1919
1944
return len (self .execute (query , [name ]).fetchall ()) > 0
1920
1945
1921
- def get_table (self , table_name , schema = None ):
1946
+ def get_table (self , table_name : str , schema : Optional [ str ] = None ):
1922
1947
return None # not supported in fallback mode
1923
1948
1924
- def drop_table (self , name , schema = None ):
1949
+ def drop_table (self , name : str , schema : Optional [ str ] = None ):
1925
1950
drop_sql = f"DROP TABLE { _get_valid_sqlite_name (name )} "
1926
1951
self .execute (drop_sql )
1927
1952
1928
- def _create_sql_schema (self , frame , table_name , keys = None , dtype = None , schema = None ):
1953
+ def _create_sql_schema (
1954
+ self ,
1955
+ frame ,
1956
+ table_name : str ,
1957
+ keys = None ,
1958
+ dtype = None ,
1959
+ schema : Optional [str ] = None ,
1960
+ ):
1929
1961
table = SQLiteTable (
1930
1962
table_name ,
1931
1963
self ,
@@ -1938,7 +1970,9 @@ def _create_sql_schema(self, frame, table_name, keys=None, dtype=None, schema=No
1938
1970
return str (table .sql_schema ())
1939
1971
1940
1972
1941
- def get_schema (frame , name , keys = None , con = None , dtype = None , schema = None ):
1973
+ def get_schema (
1974
+ frame , name : str , keys = None , con = None , dtype = None , schema : Optional [str ] = None
1975
+ ):
1942
1976
"""
1943
1977
Get the SQL db table schema for the given frame.
1944
1978
0 commit comments