Skip to content

Commit 2abbcf3

Browse files
committed
chore: add truncate option in types and documentation
1 parent 642d244 commit 2abbcf3

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

pandas/io/sql.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def to_sql(
733733
name: str,
734734
con,
735735
schema: str | None = None,
736-
if_exists: Literal["fail", "replace", "append"] = "fail",
736+
if_exists: Literal["fail", "replace", "append", "truncate"] = "fail",
737737
index: bool = True,
738738
index_label: IndexLabel | None = None,
739739
chunksize: int | None = None,
@@ -759,10 +759,11 @@ def to_sql(
759759
schema : str, optional
760760
Name of SQL schema in database to write to (if database flavor
761761
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'
763763
- fail: If table exists, do nothing.
764764
- replace: If table exists, drop it, recreate it, and insert data.
765765
- append: If table exists, insert data. Create if does not exist.
766+
- truncate: If table exists, truncate it. Create if does not exist.
766767
index : bool, default True
767768
Write DataFrame index as a column.
768769
index_label : str or sequence, optional
@@ -813,7 +814,7 @@ def to_sql(
813814
`sqlite3 <https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.rowcount>`__ or
814815
`SQLAlchemy <https://docs.sqlalchemy.org/en/14/core/connections.html#sqlalchemy.engine.BaseCursorResult.rowcount>`__
815816
""" # noqa: E501
816-
if if_exists not in ("fail", "replace", "append"):
817+
if if_exists not in ("fail", "replace", "append", "truncate"):
817818
raise ValueError(f"'{if_exists}' is not valid for if_exists")
818819

819820
if isinstance(frame, Series):
@@ -921,7 +922,7 @@ def __init__(
921922
pandas_sql_engine,
922923
frame=None,
923924
index: bool | str | list[str] | None = True,
924-
if_exists: Literal["fail", "replace", "append"] = "fail",
925+
if_exists: Literal["fail", "replace", "append", "truncate"] = "fail",
925926
prefix: str = "pandas",
926927
index_label=None,
927928
schema=None,
@@ -1465,7 +1466,7 @@ def to_sql(
14651466
self,
14661467
frame,
14671468
name: str,
1468-
if_exists: Literal["fail", "replace", "append"] = "fail",
1469+
if_exists: Literal["fail", "replace", "append", "truncate"] = "fail",
14691470
index: bool = True,
14701471
index_label=None,
14711472
schema=None,
@@ -1850,7 +1851,7 @@ def prep_table(
18501851
self,
18511852
frame,
18521853
name: str,
1853-
if_exists: Literal["fail", "replace", "append"] = "fail",
1854+
if_exists: Literal["fail", "replace", "append", "truncate"] = "fail",
18541855
index: bool | str | list[str] | None = True,
18551856
index_label=None,
18561857
schema=None,
@@ -1927,7 +1928,7 @@ def to_sql(
19271928
self,
19281929
frame,
19291930
name: str,
1930-
if_exists: Literal["fail", "replace", "append"] = "fail",
1931+
if_exists: Literal["fail", "replace", "append", "truncate"] = "fail",
19311932
index: bool = True,
19321933
index_label=None,
19331934
schema: str | None = None,
@@ -1945,10 +1946,11 @@ def to_sql(
19451946
frame : DataFrame
19461947
name : string
19471948
Name of SQL table.
1948-
if_exists : {'fail', 'replace', 'append'}, default 'fail'
1949+
if_exists : {'fail', 'replace', 'append', 'truncate'}, default 'fail'
19491950
- fail: If table exists, do nothing.
19501951
- replace: If table exists, drop it, recreate it, and insert data.
19511952
- append: If table exists, insert data. Create if does not exist.
1953+
- truncate: If table exists, truncate it. Create if does not exist.
19521954
index : boolean, default True
19531955
Write DataFrame index as a column.
19541956
index_label : string or sequence, default None
@@ -2301,7 +2303,7 @@ def to_sql(
23012303
self,
23022304
frame,
23032305
name: str,
2304-
if_exists: Literal["fail", "replace", "append"] = "fail",
2306+
if_exists: Literal["fail", "replace", "append", "truncate"] = "fail",
23052307
index: bool = True,
23062308
index_label=None,
23072309
schema: str | None = None,
@@ -2323,6 +2325,7 @@ def to_sql(
23232325
- fail: If table exists, do nothing.
23242326
- replace: If table exists, drop it, recreate it, and insert data.
23252327
- append: If table exists, insert data. Create if does not exist.
2328+
- truncate: If table exists, truncate it. Create if does not exist.
23262329
index : boolean, default True
23272330
Write DataFrame index as a column.
23282331
index_label : string or sequence, default None
@@ -2778,6 +2781,7 @@ def to_sql(
27782781
fail: If table exists, do nothing.
27792782
replace: If table exists, drop it, recreate it, and insert data.
27802783
append: If table exists, insert data. Create if it does not exist.
2784+
truncate: If table exists, truncate it. Create if does not exist.
27812785
index : bool, default True
27822786
Write DataFrame index as a column
27832787
index_label : string or sequence, default None

0 commit comments

Comments
 (0)