@@ -733,7 +733,7 @@ def to_sql(
733
733
name : str ,
734
734
con ,
735
735
schema : str | None = None ,
736
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
736
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
737
737
index : bool = True ,
738
738
index_label : IndexLabel | None = None ,
739
739
chunksize : int | None = None ,
@@ -759,10 +759,12 @@ def to_sql(
759
759
schema : str, optional
760
760
Name of SQL schema in database to write to (if database flavor
761
761
supports this). If None, use default schema (default).
762
- if_exists : {'fail', 'replace', 'append'}, default 'fail'
762
+ if_exists : {'fail', 'replace', 'append', 'truncate' }, default 'fail'
763
763
- fail: If table exists, do nothing.
764
764
- replace: If table exists, drop it, recreate it, and insert data.
765
765
- append: If table exists, insert data. Create if does not exist.
766
+ - truncate: If table exists, truncate it. Create if does not exist.
767
+ Raises NotImplementedError if 'TRUNCATE TABLE' is not supported
766
768
index : bool, default True
767
769
Write DataFrame index as a column.
768
770
index_label : str or sequence, optional
@@ -813,7 +815,7 @@ def to_sql(
813
815
`sqlite3 <https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.rowcount>`__ or
814
816
`SQLAlchemy <https://docs.sqlalchemy.org/en/14/core/connections.html#sqlalchemy.engine.BaseCursorResult.rowcount>`__
815
817
""" # noqa: E501
816
- if if_exists not in ("fail" , "replace" , "append" ):
818
+ if if_exists not in ("fail" , "replace" , "append" , "truncate" ):
817
819
raise ValueError (f"'{ if_exists } ' is not valid for if_exists" )
818
820
819
821
if isinstance (frame , Series ):
@@ -921,7 +923,7 @@ def __init__(
921
923
pandas_sql_engine ,
922
924
frame = None ,
923
925
index : bool | str | list [str ] | None = True ,
924
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
926
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
925
927
prefix : str = "pandas" ,
926
928
index_label = None ,
927
929
schema = None ,
@@ -969,11 +971,13 @@ def create(self) -> None:
969
971
if self .exists ():
970
972
if self .if_exists == "fail" :
971
973
raise ValueError (f"Table '{ self .name } ' already exists." )
972
- if self .if_exists == "replace" :
974
+ elif self .if_exists == "replace" :
973
975
self .pd_sql .drop_table (self .name , self .schema )
974
976
self ._execute_create ()
975
977
elif self .if_exists == "append" :
976
978
pass
979
+ elif self .if_exists == "truncate" :
980
+ self .pd_sql .truncate_table (self .name , self .schema )
977
981
else :
978
982
raise ValueError (f"'{ self .if_exists } ' is not valid for if_exists" )
979
983
else :
@@ -1465,7 +1469,7 @@ def to_sql(
1465
1469
self ,
1466
1470
frame ,
1467
1471
name : str ,
1468
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1472
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
1469
1473
index : bool = True ,
1470
1474
index_label = None ,
1471
1475
schema = None ,
@@ -1850,7 +1854,7 @@ def prep_table(
1850
1854
self ,
1851
1855
frame ,
1852
1856
name : str ,
1853
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1857
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
1854
1858
index : bool | str | list [str ] | None = True ,
1855
1859
index_label = None ,
1856
1860
schema = None ,
@@ -1927,7 +1931,7 @@ def to_sql(
1927
1931
self ,
1928
1932
frame ,
1929
1933
name : str ,
1930
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1934
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
1931
1935
index : bool = True ,
1932
1936
index_label = None ,
1933
1937
schema : str | None = None ,
@@ -1945,10 +1949,12 @@ def to_sql(
1945
1949
frame : DataFrame
1946
1950
name : string
1947
1951
Name of SQL table.
1948
- if_exists : {'fail', 'replace', 'append'}, default 'fail'
1952
+ if_exists : {'fail', 'replace', 'append', 'truncate' }, default 'fail'
1949
1953
- fail: If table exists, do nothing.
1950
1954
- replace: If table exists, drop it, recreate it, and insert data.
1951
1955
- append: If table exists, insert data. Create if does not exist.
1956
+ - truncate: If table exists, truncate it. Create if does not exist.
1957
+ Raises NotImplementedError if 'TRUNCATE TABLE' is not supported
1952
1958
index : boolean, default True
1953
1959
Write DataFrame index as a column.
1954
1960
index_label : string or sequence, default None
@@ -2045,6 +2051,26 @@ def drop_table(self, table_name: str, schema: str | None = None) -> None:
2045
2051
self .get_table (table_name , schema ).drop (bind = self .con )
2046
2052
self .meta .clear ()
2047
2053
2054
+ def truncate_table (self , table_name : str , schema : str | None = None ) -> None :
2055
+ from sqlalchemy .exc import OperationalError
2056
+
2057
+ schema = schema or self .meta .schema
2058
+
2059
+ if self .has_table (table_name , schema ):
2060
+ self .meta .reflect (
2061
+ bind = self .con , only = [table_name ], schema = schema , views = True
2062
+ )
2063
+ with self .run_transaction ():
2064
+ table = self .get_table (table_name , schema )
2065
+ try :
2066
+ self .execute (f"TRUNCATE TABLE { table .name } " )
2067
+ except OperationalError as exc :
2068
+ raise NotImplementedError (
2069
+ "'TRUNCATE TABLE' is not supported by this database."
2070
+ ) from exc
2071
+
2072
+ self .meta .clear ()
2073
+
2048
2074
def _create_sql_schema (
2049
2075
self ,
2050
2076
frame : DataFrame ,
@@ -2301,7 +2327,7 @@ def to_sql(
2301
2327
self ,
2302
2328
frame ,
2303
2329
name : str ,
2304
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
2330
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
2305
2331
index : bool = True ,
2306
2332
index_label = None ,
2307
2333
schema : str | None = None ,
@@ -2323,6 +2349,8 @@ def to_sql(
2323
2349
- fail: If table exists, do nothing.
2324
2350
- replace: If table exists, drop it, recreate it, and insert data.
2325
2351
- append: If table exists, insert data. Create if does not exist.
2352
+ - truncate: If table exists, truncate it. Create if does not exist.
2353
+ Raises NotImplementedError if 'TRUNCATE TABLE' is not supported
2326
2354
index : boolean, default True
2327
2355
Write DataFrame index as a column.
2328
2356
index_label : string or sequence, default None
@@ -2340,6 +2368,8 @@ def to_sql(
2340
2368
engine : {'auto', 'sqlalchemy'}, default 'auto'
2341
2369
Raises NotImplementedError if not set to 'auto'
2342
2370
"""
2371
+ from adbc_driver_manager import ProgrammingError
2372
+
2343
2373
if index_label :
2344
2374
raise NotImplementedError (
2345
2375
"'index_label' is not implemented for ADBC drivers"
@@ -2373,6 +2403,15 @@ def to_sql(
2373
2403
cur .execute (f"DROP TABLE { table_name } " )
2374
2404
elif if_exists == "append" :
2375
2405
mode = "append"
2406
+ elif if_exists == "truncate" :
2407
+ mode = "append"
2408
+ with self .con .cursor () as cur :
2409
+ try :
2410
+ cur .execute (f"TRUNCATE TABLE { table_name } " )
2411
+ except ProgrammingError as exc :
2412
+ raise NotImplementedError (
2413
+ "'TRUNCATE TABLE' is not supported by this database."
2414
+ ) from exc
2376
2415
2377
2416
import pyarrow as pa
2378
2417
@@ -2774,10 +2813,12 @@ def to_sql(
2774
2813
frame: DataFrame
2775
2814
name: string
2776
2815
Name of SQL table.
2777
- if_exists: {'fail', 'replace', 'append'}, default 'fail'
2816
+ if_exists: {'fail', 'replace', 'append', 'truncate' }, default 'fail'
2778
2817
fail: If table exists, do nothing.
2779
2818
replace: If table exists, drop it, recreate it, and insert data.
2780
2819
append: If table exists, insert data. Create if it does not exist.
2820
+ truncate: If table exists, truncate it. Create if does not exist.
2821
+ Raises NotImplementedError if 'TRUNCATE TABLE' is not supported
2781
2822
index : bool, default True
2782
2823
Write DataFrame index as a column
2783
2824
index_label : string or sequence, default None
@@ -2853,6 +2894,9 @@ def drop_table(self, name: str, schema: str | None = None) -> None:
2853
2894
drop_sql = f"DROP TABLE { _get_valid_sqlite_name (name )} "
2854
2895
self .execute (drop_sql )
2855
2896
2897
+ def truncate_table (self , name : str , schema : str | None = None ) -> None :
2898
+ raise NotImplementedError ("'TRUNCATE TABLE' is not supported by this database." )
2899
+
2856
2900
def _create_sql_schema (
2857
2901
self ,
2858
2902
frame ,
0 commit comments