From 14e86facc486d3e0edeeb353657a1463a85b6a9c Mon Sep 17 00:00:00 2001 From: arw2019 Date: Tue, 23 Jun 2020 03:29:51 +0000 Subject: [PATCH 1/7] DOC: added example of Connection as con argument to to_sql --- pandas/core/generic.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 1404d225eea97..e8c1c57cbe4d7 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -589,7 +589,7 @@ def swapaxes(self: FrameOrSeries, axis1, axis2, copy=True) -> FrameOrSeries: # ignore needed because of NDFrame constructor is different than # DataFrame/Series constructors. - return self._constructor(new_values, *new_axes).__finalize__( # type: ignore + return self._constructor(new_values, *new_axes).__finalize__( self, method="swapaxes" ) @@ -2488,7 +2488,7 @@ def to_sql( ---------- name : str Name of SQL table. - con : sqlalchemy.engine.Engine or sqlite3.Connection + con : sqlalchemy.engine.(Engine or Connection) or sqlite3.Connection Using SQLAlchemy makes it possible to use any DB supported by that library. Legacy support is provided for sqlite3.Connection objects. The user is responsible for engine disposal and connection closure for the SQLAlchemy @@ -2576,18 +2576,26 @@ def to_sql( >>> engine.execute("SELECT * FROM users").fetchall() [(0, 'User 1'), (1, 'User 2'), (2, 'User 3')] - >>> df1 = pd.DataFrame({'name' : ['User 4', 'User 5']}) - >>> df1.to_sql('users', con=engine, if_exists='append') + An `sqlalchemy.engine.Connection` can also be passed to to `con`: + >>> with engine.connect() as connection: + >>> df1 = pd.DataFrame({'name' : ['User 4', 'User 5']}) + >>> df1.to_sql('users', con=connection, if_exists='append') + This is allowed to support operations that require that the same + DBAPI connection is used to the entire operation. + + >>> df2 = pd.DataFrame({'name' : ['User 6', 'User 7']}) + >>> df2.to_sql('users', con=engine, if_exists='append') >>> engine.execute("SELECT * FROM users").fetchall() [(0, 'User 1'), (1, 'User 2'), (2, 'User 3'), - (0, 'User 4'), (1, 'User 5')] + (0, 'User 4'), (1, 'User 5'), (0, 'User 6'), + (1, 'User 7')] - Overwrite the table with just ``df1``. + Overwrite the table with just ``df2``. - >>> df1.to_sql('users', con=engine, if_exists='replace', + >>> df2.to_sql('users', con=engine, if_exists='replace', ... index_label='id') >>> engine.execute("SELECT * FROM users").fetchall() - [(0, 'User 4'), (1, 'User 5')] + [(0, 'User 6'), (1, 'User 7')] Specify the dtype (especially useful for integers with missing values). Notice that while pandas is forced to store the data as floating point, @@ -4022,7 +4030,7 @@ def add_prefix(self: FrameOrSeries, prefix: str) -> FrameOrSeries: f = functools.partial("{prefix}{}".format, prefix=prefix) mapper = {self._info_axis_name: f} - return self.rename(**mapper) # type: ignore + return self.rename(**mapper) def add_suffix(self: FrameOrSeries, suffix: str) -> FrameOrSeries: """ @@ -4081,7 +4089,7 @@ def add_suffix(self: FrameOrSeries, suffix: str) -> FrameOrSeries: f = functools.partial("{}{suffix}".format, suffix=suffix) mapper = {self._info_axis_name: f} - return self.rename(**mapper) # type: ignore + return self.rename(**mapper) def sort_values( self, From fc8ec73a9a34dc3f15e062b9fd8e7cd027a98186 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Tue, 23 Jun 2020 04:11:14 +0000 Subject: [PATCH 2/7] DOC: fixed styling in to_sql docstring --- pandas/core/generic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e8c1c57cbe4d7..6d503b2514301 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2578,8 +2578,8 @@ def to_sql( An `sqlalchemy.engine.Connection` can also be passed to to `con`: >>> with engine.connect() as connection: - >>> df1 = pd.DataFrame({'name' : ['User 4', 'User 5']}) - >>> df1.to_sql('users', con=connection, if_exists='append') + df1 = pd.DataFrame({'name' : ['User 4', 'User 5']}) + df1.to_sql('users', con=connection, if_exists='append') This is allowed to support operations that require that the same DBAPI connection is used to the entire operation. From 9db20e2fba28cfada71f85a379ec9b30408f7ecd Mon Sep 17 00:00:00 2001 From: arw2019 Date: Tue, 23 Jun 2020 04:13:54 +0000 Subject: [PATCH 3/7] DOC: fixed styling in to_sql docstring --- pandas/core/generic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 6d503b2514301..b961c268b58e1 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2578,8 +2578,8 @@ def to_sql( An `sqlalchemy.engine.Connection` can also be passed to to `con`: >>> with engine.connect() as connection: - df1 = pd.DataFrame({'name' : ['User 4', 'User 5']}) - df1.to_sql('users', con=connection, if_exists='append') + ... df1 = pd.DataFrame({'name' : ['User 4', 'User 5']}) + ... df1.to_sql('users', con=connection, if_exists='append') This is allowed to support operations that require that the same DBAPI connection is used to the entire operation. From c97f112b2439f68dda3624c54b2eb3d5f60d1d83 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Tue, 23 Jun 2020 04:15:14 +0000 Subject: [PATCH 4/7] DOC: fixed styling in to_sql docstring --- pandas/core/generic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b961c268b58e1..c06a15159a796 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2580,6 +2580,7 @@ def to_sql( >>> with engine.connect() as connection: ... df1 = pd.DataFrame({'name' : ['User 4', 'User 5']}) ... df1.to_sql('users', con=connection, if_exists='append') + This is allowed to support operations that require that the same DBAPI connection is used to the entire operation. From b8f00f026ec001219a001bd690bd9e266e0b888e Mon Sep 17 00:00:00 2001 From: arw2019 Date: Tue, 23 Jun 2020 17:14:33 +0000 Subject: [PATCH 5/7] revert accidental changes --- pandas/core/generic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c06a15159a796..f7c1cb9f8fcbc 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -589,7 +589,7 @@ def swapaxes(self: FrameOrSeries, axis1, axis2, copy=True) -> FrameOrSeries: # ignore needed because of NDFrame constructor is different than # DataFrame/Series constructors. - return self._constructor(new_values, *new_axes).__finalize__( + return self._constructor(new_values, *new_axes).__finalize__( # type: ignore self, method="swapaxes" ) @@ -4031,7 +4031,7 @@ def add_prefix(self: FrameOrSeries, prefix: str) -> FrameOrSeries: f = functools.partial("{prefix}{}".format, prefix=prefix) mapper = {self._info_axis_name: f} - return self.rename(**mapper) + return self.rename(**mapper) # type: ignore def add_suffix(self: FrameOrSeries, suffix: str) -> FrameOrSeries: """ @@ -4090,7 +4090,7 @@ def add_suffix(self: FrameOrSeries, suffix: str) -> FrameOrSeries: f = functools.partial("{}{suffix}".format, suffix=suffix) mapper = {self._info_axis_name: f} - return self.rename(**mapper) + return self.rename(**mapper) # type: ignore def sort_values( self, From 7f62317ccc94b94ea963a088ed2808da645158fe Mon Sep 17 00:00:00 2001 From: arw2019 Date: Wed, 24 Jun 2020 18:56:18 +0000 Subject: [PATCH 6/7] DOC: fixed context manager example in to_sql --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index f7c1cb9f8fcbc..ea3b72b0993fc 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2577,7 +2577,7 @@ def to_sql( [(0, 'User 1'), (1, 'User 2'), (2, 'User 3')] An `sqlalchemy.engine.Connection` can also be passed to to `con`: - >>> with engine.connect() as connection: + >>> with engine.begin() as connection: ... df1 = pd.DataFrame({'name' : ['User 4', 'User 5']}) ... df1.to_sql('users', con=connection, if_exists='append') From 1bbbe9eba729276995225c20ee5f329afa897852 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Wed, 24 Jun 2020 18:57:36 +0000 Subject: [PATCH 7/7] DOC: fixed typo in to_sql docstring --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index ea3b72b0993fc..e3dd3f2994f13 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2582,7 +2582,7 @@ def to_sql( ... df1.to_sql('users', con=connection, if_exists='append') This is allowed to support operations that require that the same - DBAPI connection is used to the entire operation. + DBAPI connection is used for the entire operation. >>> df2 = pd.DataFrame({'name' : ['User 6', 'User 7']}) >>> df2.to_sql('users', con=engine, if_exists='append')