Skip to content

Add docstring to the insertion method & add empty result note #26872

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 5 commits into from
Sep 3, 2019
Merged
Changes from all commits
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
23 changes: 23 additions & 0 deletions doc/source/user_guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5047,6 +5047,17 @@ Example of a callable using PostgreSQL `COPY clause
from io import StringIO

def psql_insert_copy(table, conn, keys, data_iter):
"""
Execute SQL statement inserting data

Parameters
----------
table : pandas.io.sql.SQLTable
conn : sqlalchemy.engine.Engine or sqlalchemy.engine.Connection
keys : list of str
Column names
data_iter : Iterable that iterates the values to be inserted
"""
# gets a DBAPI connection that can provide a cursor
dbapi_conn = conn.connection
with dbapi_conn.cursor() as cur:
Expand Down Expand Up @@ -5080,6 +5091,18 @@ table name and optionally a subset of columns to read.

pd.read_sql_table('data', engine)

.. note::

Note that pandas infers column dtypes from query outputs, and not by looking
up data types in the physical database schema. For example, assume ``userid``
is an integer column in a table. Then, intuitively, ``select userid ...`` will
return integer-valued series, while ``select cast(userid as text) ...`` will
return object-valued (str) series. Accordingly, if the query output is empty,
then all resulting columns will be returned as object-valued (since they are
most general). If you foresee that your query will sometimes generate an empty
result, you may want to explicitly typecast afterwards to ensure dtype
integrity.

You can also specify the name of the column as the ``DataFrame`` index,
and specify a subset of columns to be read.

Expand Down