-
-
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
Conversation
pandas/core/generic.py
Outdated
|
||
Examples | ||
-------- | ||
>>> import pandas as pd |
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.
pandas import is not recommended in examples.
pandas/core/generic.py
Outdated
@@ -1865,17 +1865,21 @@ 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 |
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.
pandas/core/generic.py
Outdated
>>> gen_users = lambda ids: {"id": ids, "name" : gen_names(ids)} | ||
>>> df=pd.DataFrame(gen_users(list(range(3)))) | ||
>>> # create a table from scratch | ||
>>> df.to_sql('users',con=engine,if_exists='replace') |
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.
some example lines are not compatible with PEP8
pandas/core/generic.py
Outdated
>>> engine = create_engine('sqlite:///example.db', echo=False) | ||
>>> gen_names = lambda ids: ["User" + str(x) for x in ids] | ||
>>> gen_users = lambda ids: {"id": ids, "name" : gen_names(ids)} | ||
>>> df=pd.DataFrame(gen_users(list(range(3)))) |
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.
maybe it's a bit more straightforward to generate dataframe without other functions:
i.e. df=pd.DataFrame(['User 1', 'User 2', 'User 3'])
pandas/core/generic.py
Outdated
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 comment
The 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
pandas/core/generic.py
Outdated
@@ -1892,6 +1897,29 @@ 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
python scripts/validate_docstrings.py pandas.DataFrame.to_sql complains:
Errors found:
No returns section found
pandas/core/generic.py
Outdated
|
||
See Also | ||
-------- | ||
pandas.io.sql.to_sql : this function will be called. |
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.
I don't think that method is public. I think just link to pandas.read_sql
pandas/core/generic.py
Outdated
-------- | ||
>>> from sqlalchemy import create_engine | ||
>>> engine = create_engine('sqlite:///example.db', echo=False) | ||
>>> gen_names = lambda ids: ["User" + str(x) for x in ids] |
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.
Any chance you could simplify this? It's a bit dense.
Maybe just make a simple df, display it. then show the insert. And maybe show a pd.read_sql
to view the result.
Hello @jazzmuesli! Thanks for updating the PR. Cheers ! There are no PEP8 issues in this Pull Request. 🍻 Comment last updated on March 12, 2018 at 22:15 Hours UTC |
pandas/core/generic.py
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
can add a link to SQLAlchemy in References
pandas/core/generic.py
Outdated
>>> 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 comment
The reason will be displayed to describe this comment to others. Learn more.
can you add blank lines between cases
also for comments, don't use the leading '>>>'
pandas/core/generic.py
Outdated
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
just to pandas.read_sql
Made some updates if anyone has a chance to review @jazzmuesli. |
Thanks @jazzmuesli ! |
Checklist for the pandas documentation sprint (ignore this if you are doing
an unrelated PR):
scripts/validate_docstrings.py <your-function-or-method>
git diff upstream/master -u -- "*.py" | flake8 --diff
python doc/make.py --single <your-function-or-method>
Please include the output of the validation script below between the "```" ticks:
If the validation script still gives errors, but you think there is a good reason
to deviate in this case (and there are certainly such cases), please state this
explicitly.