-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: update the pandas.DataFrame.to_sql docstring #20126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
13fd3bd
b653565
0b1eb27
c48870f
9cce9e9
b4dd9b1
9af573d
be02b35
62e9cb0
e315f67
15a8938
c82208b
7c503b6
5668eec
9e1bec4
fa9cc36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1865,17 +1865,22 @@ def to_sql(self, name, con, schema=None, if_exists='fail', index=True, | |
""" | ||
Write records stored in a DataFrame to a SQL database. | ||
|
||
This function inserts all rows of the dataframe into the given | ||
table and recreates it if if_exists='replace'. Databases supported by | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can add a link to SQLAlchemy in References |
||
SQLAlchemy or DBAPI2 are also supported. | ||
|
||
Parameters | ||
---------- | ||
name : string | ||
Name of SQL table | ||
Name of SQL table. | ||
con : SQLAlchemy engine or DBAPI2 connection (legacy mode) | ||
Using SQLAlchemy makes it possible to use any DB supported by that | ||
library. If a DBAPI2 object, only sqlite3 is supported. | ||
schema : string, default None | ||
Specify the schema (if database flavor supports this). If None, use | ||
default schema. | ||
if_exists : {'fail', 'replace', 'append'}, default 'fail' | ||
Accepted values: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, I find it clearer without the line here, though I know the script complains when it isn't present :) cc @datapythonista |
||
- fail: If table exists, do nothing. | ||
- replace: If table exists, drop it, recreate it, and insert data. | ||
- append: If table exists, insert data. Create if does not exist. | ||
|
@@ -1892,6 +1897,34 @@ def to_sql(self, name, con, schema=None, if_exists='fail', index=True, | |
Optional specifying the datatype for columns. The SQL type should | ||
be a SQLAlchemy type, or a string for sqlite3 fallback connection. | ||
|
||
Returns | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Returns section can be ommitted if ther's no return value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. python scripts/validate_docstrings.py pandas.DataFrame.to_sql complains: |
||
-------- | ||
None | ||
|
||
See Also | ||
-------- | ||
pandas.read_sql_query : read a DataFrame from a table | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to |
||
|
||
Examples | ||
-------- | ||
>>> from sqlalchemy import create_engine | ||
>>> engine = create_engine('sqlite://', echo=False) | ||
>>> df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3']}) | ||
>>> # create a table from scratch with 3 rows | ||
>>> df.to_sql('users', con=engine, if_exists='replace') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add blank lines between cases |
||
>>> df1 = pd.DataFrame({'name' : ['User 4', 'User 5']}) | ||
>>> # 2 new rows inserted | ||
>>> df1.to_sql('users', con=engine, if_exists='append') | ||
>>> # table will be recreated and 5 rows inserted | ||
>>> df = pd.concat([df, df1], ignore_index=True) | ||
>>> df.to_sql('users', con=engine, if_exists='replace') | ||
>>> pd.read_sql_query("select * from users",con=engine) | ||
index name | ||
0 0 User 1 | ||
1 1 User 2 | ||
2 2 User 3 | ||
3 3 User 4 | ||
4 4 User 5 | ||
""" | ||
from pandas.io import sql | ||
sql.to_sql(self, name, con, schema=schema, if_exists=if_exists, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be good to know which databases are supported and how.