@@ -736,7 +736,7 @@ def to_sql(
736
736
name : str ,
737
737
con ,
738
738
schema : str | None = None ,
739
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
739
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
740
740
index : bool = True ,
741
741
index_label : IndexLabel | None = None ,
742
742
chunksize : int | None = None ,
@@ -762,10 +762,12 @@ def to_sql(
762
762
schema : str, optional
763
763
Name of SQL schema in database to write to (if database flavor
764
764
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'
766
766
- fail: If table exists, do nothing.
767
767
- replace: If table exists, drop it, recreate it, and insert data.
768
768
- 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
769
771
index : bool, default True
770
772
Write DataFrame index as a column.
771
773
index_label : str or sequence, optional
@@ -816,7 +818,7 @@ def to_sql(
816
818
`sqlite3 <https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.rowcount>`__ or
817
819
`SQLAlchemy <https://docs.sqlalchemy.org/en/14/core/connections.html#sqlalchemy.engine.BaseCursorResult.rowcount>`__
818
820
""" # noqa: E501
819
- if if_exists not in ("fail" , "replace" , "append" ):
821
+ if if_exists not in ("fail" , "replace" , "append" , "truncate" ):
820
822
raise ValueError (f"'{ if_exists } ' is not valid for if_exists" )
821
823
822
824
if isinstance (frame , Series ):
@@ -924,7 +926,7 @@ def __init__(
924
926
pandas_sql_engine ,
925
927
frame = None ,
926
928
index : bool | str | list [str ] | None = True ,
927
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
929
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
928
930
prefix : str = "pandas" ,
929
931
index_label = None ,
930
932
schema = None ,
@@ -972,11 +974,13 @@ def create(self) -> None:
972
974
if self .exists ():
973
975
if self .if_exists == "fail" :
974
976
raise ValueError (f"Table '{ self .name } ' already exists." )
975
- if self .if_exists == "replace" :
977
+ elif self .if_exists == "replace" :
976
978
self .pd_sql .drop_table (self .name , self .schema )
977
979
self ._execute_create ()
978
980
elif self .if_exists == "append" :
979
981
pass
982
+ elif self .if_exists == "truncate" :
983
+ self .pd_sql .truncate_table (self .name , self .schema )
980
984
else :
981
985
raise ValueError (f"'{ self .if_exists } ' is not valid for if_exists" )
982
986
else :
@@ -1468,7 +1472,7 @@ def to_sql(
1468
1472
self ,
1469
1473
frame ,
1470
1474
name : str ,
1471
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1475
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
1472
1476
index : bool = True ,
1473
1477
index_label = None ,
1474
1478
schema = None ,
@@ -1854,7 +1858,7 @@ def prep_table(
1854
1858
self ,
1855
1859
frame ,
1856
1860
name : str ,
1857
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1861
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
1858
1862
index : bool | str | list [str ] | None = True ,
1859
1863
index_label = None ,
1860
1864
schema = None ,
@@ -1931,7 +1935,7 @@ def to_sql(
1931
1935
self ,
1932
1936
frame ,
1933
1937
name : str ,
1934
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1938
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
1935
1939
index : bool = True ,
1936
1940
index_label = None ,
1937
1941
schema : str | None = None ,
@@ -1949,10 +1953,12 @@ def to_sql(
1949
1953
frame : DataFrame
1950
1954
name : string
1951
1955
Name of SQL table.
1952
- if_exists : {'fail', 'replace', 'append'}, default 'fail'
1956
+ if_exists : {'fail', 'replace', 'append', 'truncate' }, default 'fail'
1953
1957
- fail: If table exists, do nothing.
1954
1958
- replace: If table exists, drop it, recreate it, and insert data.
1955
1959
- 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
1956
1962
index : boolean, default True
1957
1963
Write DataFrame index as a column.
1958
1964
index_label : string or sequence, default None
@@ -2049,6 +2055,26 @@ def drop_table(self, table_name: str, schema: str | None = None) -> None:
2049
2055
self .get_table (table_name , schema ).drop (bind = self .con )
2050
2056
self .meta .clear ()
2051
2057
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
+
2052
2078
def _create_sql_schema (
2053
2079
self ,
2054
2080
frame : DataFrame ,
@@ -2306,7 +2332,7 @@ def to_sql(
2306
2332
self ,
2307
2333
frame ,
2308
2334
name : str ,
2309
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
2335
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
2310
2336
index : bool = True ,
2311
2337
index_label = None ,
2312
2338
schema : str | None = None ,
@@ -2328,6 +2354,8 @@ def to_sql(
2328
2354
- fail: If table exists, do nothing.
2329
2355
- replace: If table exists, drop it, recreate it, and insert data.
2330
2356
- 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
2331
2359
index : boolean, default True
2332
2360
Write DataFrame index as a column.
2333
2361
index_label : string or sequence, default None
@@ -2345,6 +2373,8 @@ def to_sql(
2345
2373
engine : {'auto', 'sqlalchemy'}, default 'auto'
2346
2374
Raises NotImplementedError if not set to 'auto'
2347
2375
"""
2376
+ from adbc_driver_manager import ProgrammingError
2377
+
2348
2378
if index_label :
2349
2379
raise NotImplementedError (
2350
2380
"'index_label' is not implemented for ADBC drivers"
@@ -2378,6 +2408,15 @@ def to_sql(
2378
2408
cur .execute (f"DROP TABLE { table_name } " )
2379
2409
elif if_exists == "append" :
2380
2410
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
2381
2420
2382
2421
import pyarrow as pa
2383
2422
@@ -2779,10 +2818,12 @@ def to_sql(
2779
2818
frame: DataFrame
2780
2819
name: string
2781
2820
Name of SQL table.
2782
- if_exists: {'fail', 'replace', 'append'}, default 'fail'
2821
+ if_exists: {'fail', 'replace', 'append', 'truncate' }, default 'fail'
2783
2822
fail: If table exists, do nothing.
2784
2823
replace: If table exists, drop it, recreate it, and insert data.
2785
2824
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
2786
2827
index : bool, default True
2787
2828
Write DataFrame index as a column
2788
2829
index_label : string or sequence, default None
@@ -2858,6 +2899,9 @@ def drop_table(self, name: str, schema: str | None = None) -> None:
2858
2899
drop_sql = f"DROP TABLE { _get_valid_sqlite_name (name )} "
2859
2900
self .execute (drop_sql )
2860
2901
2902
+ def truncate_table (self , name : str , schema : str | None = None ) -> None :
2903
+ raise NotImplementedError ("'TRUNCATE TABLE' is not supported by this database." )
2904
+
2861
2905
def _create_sql_schema (
2862
2906
self ,
2863
2907
frame ,
0 commit comments