@@ -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,11 @@ 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.
766
767
index : bool, default True
767
768
Write DataFrame index as a column.
768
769
index_label : str or sequence, optional
@@ -813,7 +814,7 @@ def to_sql(
813
814
`sqlite3 <https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.rowcount>`__ or
814
815
`SQLAlchemy <https://docs.sqlalchemy.org/en/14/core/connections.html#sqlalchemy.engine.BaseCursorResult.rowcount>`__
815
816
""" # noqa: E501
816
- if if_exists not in ("fail" , "replace" , "append" ):
817
+ if if_exists not in ("fail" , "replace" , "append" , "truncate" ):
817
818
raise ValueError (f"'{ if_exists } ' is not valid for if_exists" )
818
819
819
820
if isinstance (frame , Series ):
@@ -921,7 +922,7 @@ def __init__(
921
922
pandas_sql_engine ,
922
923
frame = None ,
923
924
index : bool | str | list [str ] | None = True ,
924
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
925
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
925
926
prefix : str = "pandas" ,
926
927
index_label = None ,
927
928
schema = None ,
@@ -1465,7 +1466,7 @@ def to_sql(
1465
1466
self ,
1466
1467
frame ,
1467
1468
name : str ,
1468
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1469
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
1469
1470
index : bool = True ,
1470
1471
index_label = None ,
1471
1472
schema = None ,
@@ -1850,7 +1851,7 @@ def prep_table(
1850
1851
self ,
1851
1852
frame ,
1852
1853
name : str ,
1853
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1854
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
1854
1855
index : bool | str | list [str ] | None = True ,
1855
1856
index_label = None ,
1856
1857
schema = None ,
@@ -1927,7 +1928,7 @@ def to_sql(
1927
1928
self ,
1928
1929
frame ,
1929
1930
name : str ,
1930
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
1931
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
1931
1932
index : bool = True ,
1932
1933
index_label = None ,
1933
1934
schema : str | None = None ,
@@ -1945,10 +1946,11 @@ def to_sql(
1945
1946
frame : DataFrame
1946
1947
name : string
1947
1948
Name of SQL table.
1948
- if_exists : {'fail', 'replace', 'append'}, default 'fail'
1949
+ if_exists : {'fail', 'replace', 'append', 'truncate' }, default 'fail'
1949
1950
- fail: If table exists, do nothing.
1950
1951
- replace: If table exists, drop it, recreate it, and insert data.
1951
1952
- append: If table exists, insert data. Create if does not exist.
1953
+ - truncate: If table exists, truncate it. Create if does not exist.
1952
1954
index : boolean, default True
1953
1955
Write DataFrame index as a column.
1954
1956
index_label : string or sequence, default None
@@ -2301,7 +2303,7 @@ def to_sql(
2301
2303
self ,
2302
2304
frame ,
2303
2305
name : str ,
2304
- if_exists : Literal ["fail" , "replace" , "append" ] = "fail" ,
2306
+ if_exists : Literal ["fail" , "replace" , "append" , "truncate" ] = "fail" ,
2305
2307
index : bool = True ,
2306
2308
index_label = None ,
2307
2309
schema : str | None = None ,
@@ -2323,6 +2325,7 @@ def to_sql(
2323
2325
- fail: If table exists, do nothing.
2324
2326
- replace: If table exists, drop it, recreate it, and insert data.
2325
2327
- append: If table exists, insert data. Create if does not exist.
2328
+ - truncate: If table exists, truncate it. Create if does not exist.
2326
2329
index : boolean, default True
2327
2330
Write DataFrame index as a column.
2328
2331
index_label : string or sequence, default None
@@ -2778,6 +2781,7 @@ def to_sql(
2778
2781
fail: If table exists, do nothing.
2779
2782
replace: If table exists, drop it, recreate it, and insert data.
2780
2783
append: If table exists, insert data. Create if it does not exist.
2784
+ truncate: If table exists, truncate it. Create if does not exist.
2781
2785
index : bool, default True
2782
2786
Write DataFrame index as a column
2783
2787
index_label : string or sequence, default None
0 commit comments