@@ -736,7 +736,7 @@ def to_sql(
736736 name : str ,
737737 con ,
738738 schema : str | None = None ,
739- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
739+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
740740 index : bool = True ,
741741 index_label : IndexLabel | None = None ,
742742 chunksize : int | None = None ,
@@ -762,10 +762,12 @@ def to_sql(
762762 schema : str, optional
763763 Name of SQL schema in database to write to (if database flavor
764764 supports this). If None, use default schema (default).
765- if_exists : {'fail', 'replace', 'append'}, default 'fail'
765+ if_exists : {'fail', 'replace', 'append', 'truncate' }, default 'fail'
766766 - fail: If table exists, do nothing.
767767 - replace: If table exists, drop it, recreate it, and insert data.
768768 - append: If table exists, insert data. Create if does not exist.
769+ - truncate: If table exists, truncate it. Create if does not exist.
770+ Raises NotImplementedError if 'TRUNCATE TABLE' is not supported
769771 index : bool, default True
770772 Write DataFrame index as a column.
771773 index_label : str or sequence, optional
@@ -816,7 +818,7 @@ def to_sql(
816818 `sqlite3 <https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.rowcount>`__ or
817819 `SQLAlchemy <https://docs.sqlalchemy.org/en/14/core/connections.html#sqlalchemy.engine.BaseCursorResult.rowcount>`__
818820 """ # noqa: E501
819- if if_exists not in ("fail" , "replace" , "append" ):
821+ if if_exists not in ("fail" , "replace" , "append" , "truncate" ):
820822 raise ValueError (f"'{ if_exists } ' is not valid for if_exists" )
821823
822824 if isinstance (frame , Series ):
@@ -924,7 +926,7 @@ def __init__(
924926 pandas_sql_engine ,
925927 frame = None ,
926928 index : bool | str | list [str ] | None = True ,
927- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
929+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
928930 prefix : str = "pandas" ,
929931 index_label = None ,
930932 schema = None ,
@@ -972,11 +974,13 @@ def create(self) -> None:
972974 if self .exists ():
973975 if self .if_exists == "fail" :
974976 raise ValueError (f"Table '{ self .name } ' already exists." )
975- if self .if_exists == "replace" :
977+ elif self .if_exists == "replace" :
976978 self .pd_sql .drop_table (self .name , self .schema )
977979 self ._execute_create ()
978980 elif self .if_exists == "append" :
979981 pass
982+ elif self .if_exists == "truncate" :
983+ self .pd_sql .truncate_table (self .name , self .schema )
980984 else :
981985 raise ValueError (f"'{ self .if_exists } ' is not valid for if_exists" )
982986 else :
@@ -1468,7 +1472,7 @@ def to_sql(
14681472 self ,
14691473 frame ,
14701474 name : str ,
1471- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1475+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
14721476 index : bool = True ,
14731477 index_label = None ,
14741478 schema = None ,
@@ -1854,7 +1858,7 @@ def prep_table(
18541858 self ,
18551859 frame ,
18561860 name : str ,
1857- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1861+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
18581862 index : bool | str | list [str ] | None = True ,
18591863 index_label = None ,
18601864 schema = None ,
@@ -1931,7 +1935,7 @@ def to_sql(
19311935 self ,
19321936 frame ,
19331937 name : str ,
1934- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1938+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
19351939 index : bool = True ,
19361940 index_label = None ,
19371941 schema : str | None = None ,
@@ -1949,10 +1953,12 @@ def to_sql(
19491953 frame : DataFrame
19501954 name : string
19511955 Name of SQL table.
1952- if_exists : {'fail', 'replace', 'append'}, default 'fail'
1956+ if_exists : {'fail', 'replace', 'append', 'truncate' }, default 'fail'
19531957 - fail: If table exists, do nothing.
19541958 - replace: If table exists, drop it, recreate it, and insert data.
19551959 - append: If table exists, insert data. Create if does not exist.
1960+ - truncate: If table exists, truncate it. Create if does not exist.
1961+ Raises NotImplementedError if 'TRUNCATE TABLE' is not supported
19561962 index : boolean, default True
19571963 Write DataFrame index as a column.
19581964 index_label : string or sequence, default None
@@ -2049,6 +2055,26 @@ def drop_table(self, table_name: str, schema: str | None = None) -> None:
20492055 self .get_table (table_name , schema ).drop (bind = self .con )
20502056 self .meta .clear ()
20512057
2058+ def truncate_table (self , table_name : str , schema : str | None = None ) -> None :
2059+ from sqlalchemy .exc import OperationalError
2060+
2061+ schema = schema or self .meta .schema
2062+
2063+ if self .has_table (table_name , schema ):
2064+ self .meta .reflect (
2065+ bind = self .con , only = [table_name ], schema = schema , views = True
2066+ )
2067+ with self .run_transaction ():
2068+ table = self .get_table (table_name , schema )
2069+ try :
2070+ self .execute (f"TRUNCATE TABLE { table .name } " )
2071+ except OperationalError as exc :
2072+ raise NotImplementedError (
2073+ "'TRUNCATE TABLE' is not supported by this database."
2074+ ) from exc
2075+
2076+ self .meta .clear ()
2077+
20522078 def _create_sql_schema (
20532079 self ,
20542080 frame : DataFrame ,
@@ -2306,7 +2332,7 @@ def to_sql(
23062332 self ,
23072333 frame ,
23082334 name : str ,
2309- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
2335+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
23102336 index : bool = True ,
23112337 index_label = None ,
23122338 schema : str | None = None ,
@@ -2328,6 +2354,8 @@ def to_sql(
23282354 - fail: If table exists, do nothing.
23292355 - replace: If table exists, drop it, recreate it, and insert data.
23302356 - append: If table exists, insert data. Create if does not exist.
2357+ - truncate: If table exists, truncate it. Create if does not exist.
2358+ Raises NotImplementedError if 'TRUNCATE TABLE' is not supported
23312359 index : boolean, default True
23322360 Write DataFrame index as a column.
23332361 index_label : string or sequence, default None
@@ -2345,6 +2373,8 @@ def to_sql(
23452373 engine : {'auto', 'sqlalchemy'}, default 'auto'
23462374 Raises NotImplementedError if not set to 'auto'
23472375 """
2376+ from adbc_driver_manager import ProgrammingError
2377+
23482378 if index_label :
23492379 raise NotImplementedError (
23502380 "'index_label' is not implemented for ADBC drivers"
@@ -2378,6 +2408,15 @@ def to_sql(
23782408 cur .execute (f"DROP TABLE { table_name } " )
23792409 elif if_exists == "append" :
23802410 mode = "append"
2411+ elif if_exists == "truncate" :
2412+ mode = "append"
2413+ with self .con .cursor () as cur :
2414+ try :
2415+ cur .execute (f"TRUNCATE TABLE { table_name } " )
2416+ except ProgrammingError as exc :
2417+ raise NotImplementedError (
2418+ "'TRUNCATE TABLE' is not supported by this database."
2419+ ) from exc
23812420
23822421 import pyarrow as pa
23832422
@@ -2779,10 +2818,12 @@ def to_sql(
27792818 frame: DataFrame
27802819 name: string
27812820 Name of SQL table.
2782- if_exists: {'fail', 'replace', 'append'}, default 'fail'
2821+ if_exists: {'fail', 'replace', 'append', 'truncate' }, default 'fail'
27832822 fail: If table exists, do nothing.
27842823 replace: If table exists, drop it, recreate it, and insert data.
27852824 append: If table exists, insert data. Create if it does not exist.
2825+ truncate: If table exists, truncate it. Create if does not exist.
2826+ Raises NotImplementedError if 'TRUNCATE TABLE' is not supported
27862827 index : bool, default True
27872828 Write DataFrame index as a column
27882829 index_label : string or sequence, default None
@@ -2858,6 +2899,9 @@ def drop_table(self, name: str, schema: str | None = None) -> None:
28582899 drop_sql = f"DROP TABLE { _get_valid_sqlite_name (name )} "
28592900 self .execute (drop_sql )
28602901
2902+ def truncate_table (self , name : str , schema : str | None = None ) -> None :
2903+ raise NotImplementedError ("'TRUNCATE TABLE' is not supported by this database." )
2904+
28612905 def _create_sql_schema (
28622906 self ,
28632907 frame ,
0 commit comments