-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC: Document existing functionality of pandas.DataFrame.to_sql() #11886 #26795
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
Merged
TomAugspurger
merged 20 commits into
pandas-dev:master
from
oguzhanogreden:docs-pr11886
Aug 30, 2019
Merged
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
12591d1
DOC: add single dtype to NDFrame.to_sql
oguzhanogreden 4397fc7
Now passes flake8
oguzhanogreden e9b09c6
Update pandas/core/generic.py
oguzhanogreden d7990f2
Update pandas/core/generic.py
oguzhanogreden 47bafad
Type annotations for pandas.core.generic.to_sql()
oguzhanogreden 53a364f
Aligned documentation for pandas.io.sql.to_sql()
oguzhanogreden d535914
Now passes flake8
oguzhanogreden 680ed87
Completed type annotations for generic.NDFrame.to_sql()
oguzhanogreden ac44371
Follow docstring guide for parameter types of pandas.io.sql
oguzhanogreden 52a6782
Avoid circular import
oguzhanogreden 4cfee29
Have one parameter per line
oguzhanogreden 00d6a25
Closes #11415 reported by @martinburch
oguzhanogreden 13633be
Use only _typing.Dtype for dtype=
oguzhanogreden 82ebc5c
Add type checking only imports
oguzhanogreden 4700af9
Merge branch 'master' into docs-pr11886
oguzhanogreden 9eee1cf
merge upstream/master
oguzhanogreden 63ed2e3
Merge branch 'master' into docs-pr11886
oguzhanogreden b03e22f
Clean up merge
oguzhanogreden 00a081b
Remove whitespace
oguzhanogreden efa97be
Fix typo
oguzhanogreden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,8 @@ | |
import operator | ||
import pickle | ||
from textwrap import dedent | ||
from typing import Callable, FrozenSet, List, Optional, Set | ||
from typing import ( | ||
TYPE_CHECKING, Callable, FrozenSet, Iterator, List, Set) | ||
import warnings | ||
import weakref | ||
|
||
|
@@ -52,6 +53,15 @@ | |
from pandas.io.formats.printing import pprint_thing | ||
from pandas.tseries.frequencies import to_offset | ||
|
||
if TYPE_CHECKING: | ||
import sqlalchemy # noqa: F401 | ||
import sqlite3 # noqa: F401 | ||
|
||
from pandas.io.sql import SQLTable # noqa: F401 | ||
|
||
# mypy confuses the `bool()`` method of NDFrame | ||
_bool = bool | ||
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. Yea this is unfortunate and something we've seen before: The alias is the suggested approach so no change required here I think, but cc @jreback for visibility |
||
|
||
# goal is to be able to define the docs close to function, while still being | ||
# able to share | ||
_shared_docs = dict() | ||
|
@@ -2434,8 +2444,22 @@ def to_msgpack(self, path_or_buf=None, encoding='utf-8', **kwargs): | |
return packers.to_msgpack(path_or_buf, self, encoding=encoding, | ||
**kwargs) | ||
|
||
def to_sql(self, name, con, schema=None, if_exists='fail', index=True, | ||
index_label=None, chunksize=None, dtype=None, method=None): | ||
def to_sql(self, | ||
name: str, | ||
con: Union['sqlalchemy.engine.Engine', 'sqlite3.Connection'], | ||
oguzhanogreden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
schema: str = None, | ||
if_exists: str = 'fail', | ||
index: _bool = True, | ||
index_label: Union[str, List[str]] = None, | ||
chunksize: int = None, | ||
dtype: Dtype = None, | ||
method: Union[str, | ||
Callable[['SQLTable', | ||
Union['sqlalchemy.engine.Engine', | ||
'sqlite3.Connection'], | ||
List[str], Iterator[List]], None] | ||
] = None | ||
oguzhanogreden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> None: | ||
""" | ||
Write records stored in a DataFrame to a SQL database. | ||
|
||
|
@@ -2444,12 +2468,12 @@ def to_sql(self, name, con, schema=None, if_exists='fail', index=True, | |
|
||
Parameters | ||
---------- | ||
name : string | ||
name : str | ||
Name of SQL table. | ||
con : sqlalchemy.engine.Engine or sqlite3.Connection | ||
Using SQLAlchemy makes it possible to use any DB supported by that | ||
library. Legacy support is provided for sqlite3.Connection objects. | ||
schema : string, optional | ||
schema : str, optional | ||
Specify the schema (if database flavor supports this). If None, use | ||
default schema. | ||
if_exists : {'fail', 'replace', 'append'}, default 'fail' | ||
|
@@ -2462,23 +2486,24 @@ def to_sql(self, name, con, schema=None, if_exists='fail', index=True, | |
index : bool, default True | ||
Write DataFrame index as a column. Uses `index_label` as the column | ||
name in the table. | ||
index_label : string or sequence, default None | ||
index_label : string or sequence, optional | ||
Column label for index column(s). If None is given (default) and | ||
`index` is True, then the index names are used. | ||
A sequence should be given if the DataFrame uses MultiIndex. | ||
chunksize : int, optional | ||
Rows will be written in batches of this size at a time. By default, | ||
all rows will be written at once. | ||
dtype : dict, optional | ||
Specifying the datatype for columns. The keys should be the column | ||
names and the values should be the SQLAlchemy types or strings for | ||
the sqlite3 legacy mode. | ||
method : {None, 'multi', callable}, default None | ||
Specify the number of rows in each batch to be written at a time. | ||
By default, all rows will be written at once. | ||
dtype : dict or scalar, optional | ||
Specifying the datatype for columns. If a dictionary is used, the | ||
keys should be the column names and the values should be the | ||
SQLAlchemy types or strings for the sqlite3 legacy mode. If a | ||
scalar is provided, it will be applied to all columns. | ||
method : {None, 'multi', callable}, optional | ||
Controls the SQL insertion clause used: | ||
|
||
* None : Uses standard SQL ``INSERT`` clause (one per row). | ||
* 'multi': Pass multiple values in a single ``INSERT`` clause. | ||
* callable with signature ``(pd_table, conn, keys, data_iter)``. | ||
* callable with signature ``(pd_table, con, keys, data_iter)``. | ||
|
||
Details and a sample callable implementation can be found in the | ||
section :ref:`insert method <io.sql.method>`. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.