8000 DOC: Document existing functionality of pandas.DataFrame.to_sql() #11886 by oguzhanogreden · Pull Request #26795 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

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
merged 20 commits into from
Aug 30, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Completed type annotations for generic.NDFrame.to_sql()
  • Loading branch information
oguzhanogreden committed Jun 15, 2019
commit 680ed87ccedaa0ec64121badff7801d348013bdd
27 changes: 17 additions & 10 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import operator
import pickle
from textwrap import dedent
from typing import Callable, FrozenSet, List, Optional, Set, Union
from typing import Any, Callable, Dict, FrozenSet, Iterator, List, Set, Union
import warnings
import weakref

Expand Down Expand Up @@ -34,6 +34,7 @@
from pandas.core.dtypes.missing import isna, notna

import pandas as pd
from pandas._typing import Dtype
from pandas.core import missing, nanops
import pandas.core.algorithms as algos
from pandas.core.base import PandasObject, SelectionMixin
Expand All @@ -48,8 +49,12 @@

from pandas.io.formats.format import DataFrameFormatter, format_percentiles
from pandas.io.formats.printing import pprint_thing
from pandas.io.sql import SQLTable
from pandas.tseries.frequencies import to_offset

# mypy confuses the `bool()`` method of NDFrame
_bool = bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea this is unfortunate and something we've seen before:

#26029 (comment)

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()
Expand Down Expand Up @@ -2459,11 +2464,13 @@ def to_msgpack(self, path_or_buf=None, encoding='utf-8', **kwargs):
**kwargs)

def to_sql(self, name: str, con,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry should have asked this before but can you put each parameter on a separate line? Will help with readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also added Any to con= and added explanation to the note so that less thinking is required later.

schema: Optional[str] = None, if_exists: str = 'fail',
index: bool = True,
index_label: Optional[Union[str, List[str]]] = None,
chunksize: Optional[int] = None, dtype: Union[dict] = None,
method: Union[str, Callable] = None):
schema: str = None, if_exists: str = 'fail',
index: _bool = True, index_label: Union[str, List[str]] = None,
chunksize: int = None,
dtype: Union[Dict[str, Dtype], Dtype] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops sorry missed this but can you just import Dtype from pandas._typing and use that as the annotation here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You actually mentioned Dict[Any, Dtype] but I took the liberty to 'interpret' your comment, since I don't see how:

  1. Dict[Any, Dtype] annotates plain Dtype case and,
  2. Dict[str, Dtype] is more precise for dtype={'column_name': dtype} case.

And now I don't see how dtype: Dtype annotates dtype={'column_name': Dtype} situation. Seeing that you insist on this, I'll suppose I'm really off the mark here and change this as follows:

dtype: Dtype = None

After that I'll take another look at core.dtypes (and probably chase you down on Gitter soon).

method: Union[str, Callable[[SQLTable, Any, List[str],
Iterator[List]], None]] = None
) -> None:
"""
Write records stored in a DataFrame to a SQL database.

Expand All @@ -2472,12 +2479,12 @@ def to_sql(self, name: str, con,

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'
Expand All @@ -2490,7 +2497,7 @@ def to_sql(self, name: str, con,
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.
Expand All @@ -2502,7 +2509,7 @@ def to_sql(self, name: str, con,
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}, default None
method : {None, 'multi', callable}, optional
Controls the SQL insertion clause used:

* None : Uses standard SQL ``INSERT`` clause (one per row).
Expand Down
0