Skip to content

Commit 2dc22da

Browse files
committed
ENH: enable multivalues insert
1 parent 0bfb61b commit 2dc22da

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

doc/source/whatsnew/v0.23.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ Other Enhancements
338338
- For subclassed ``DataFrames``, :func:`DataFrame.apply` will now preserve the ``Series`` subclass (if defined) when passing the data to the applied function (:issue:`19822`)
339339
- :func:`DataFrame.from_dict` now accepts a ``columns`` argument that can be used to specify the column names when ``orient='index'`` is used (:issue:`18529`)
340340
- Added option ``display.html.use_mathjax`` so `MathJax <https://www.mathjax.org/>`_ can be disabled when rendering tables in ``Jupyter`` notebooks (:issue:`19856`, :issue:`19824`)
341-
341+
- :meth:`DataFrame.to_sql` now performs a multivalue insert if the underlying connection supports this rather than inserting row by row.
342+
SQLAlchemy dialects supporting multivalue inserts include mysql, postgresql, sqlite and any dialect with `supports_multivalues_insert`. (:issue:`14315`, :issue:`8953`)
342343

343344
.. _whatsnew_0230.api_breaking:
344345

pandas/io/sql.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,29 @@ def create(self):
572572
else:
573573
self._execute_create()
574574

575-
def insert_statement(self):
576-
return self.table.insert()
575+
def insert_statement(self, data, conn):
576+
"""
577+
Generate tuple of SQLAlchemy insert statement and any arguments
578+
to be executed by connection (via `_execute_insert`).
579+
580+
Parameters
581+
----------
582+
conn : SQLAlchemy connectable(engine/connection)
583+
Connection to recieve the data
584+
data : list of dict
585+
The data to be inserted
586+
587+
Returns
588+
-------
589+
SQLAlchemy statement
590+
insert statement
591+
*, optional
592+
Additional parameters to be passed when executing insert statement
593+
"""
594+
dialect = getattr(conn, 'dialect', None)
595+
if dialect and getattr(dialect, 'supports_multivalues_insert', False):
596+
return self.table.insert(data),
597+
return self.table.insert(), data
577598

578599
def insert_data(self):
579600
if self.index is not None:
@@ -612,8 +633,9 @@ def insert_data(self):
612633
return column_names, data_list
613634

614635
def _execute_insert(self, conn, keys, data_iter):
636+
"""Insert data into this table with database connection"""
615637
data = [{k: v for k, v in zip(keys, row)} for row in data_iter]
616-
conn.execute(self.insert_statement(), data)
638+
conn.execute(*self.insert_statement(data, conn))
617639

618640
def insert(self, chunksize=None):
619641
keys, data_list = self.insert_data()

pandas/tests/io/test_sql.py

+26
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,29 @@ class Temporary(Base):
16651665

16661666
tm.assert_frame_equal(df, expected)
16671667

1668+
def test_insert_multivalues(self):
1669+
# issues addressed
1670+
# https://github.com/pandas-dev/pandas/issues/14315
1671+
# https://github.com/pandas-dev/pandas/issues/8953
1672+
1673+
db = sql.SQLDatabase(self.conn)
1674+
df = DataFrame({'A': [1, 0, 0], 'B': [1.1, 0.2, 4.3]})
1675+
table = sql.SQLTable("test_table", db, frame=df)
1676+
data = [
1677+
{'A': 1, 'B': 0.46},
1678+
{'A': 0, 'B': -2.06}
1679+
]
1680+
statement = table.insert_statement(data, conn=self.conn)[0]
1681+
1682+
if self.supports_multivalues_insert:
1683+
assert statement.parameters == data, (
1684+
'insert statement should be multivalues'
1685+
)
1686+
else:
1687+
assert statement.parameters is None, (
1688+
'insert statement should not be multivalues'
1689+
)
1690+
16681691

16691692
class _TestSQLAlchemyConn(_EngineToConnMixin, _TestSQLAlchemy):
16701693

@@ -1679,6 +1702,7 @@ class _TestSQLiteAlchemy(object):
16791702
16801703
"""
16811704
flavor = 'sqlite'
1705+
supports_multivalues_insert = True
16821706

16831707
@classmethod
16841708
def connect(cls):
@@ -1727,6 +1751,7 @@ class _TestMySQLAlchemy(object):
17271751
17281752
"""
17291753
flavor = 'mysql'
1754+
supports_multivalues_insert = True
17301755

17311756
@classmethod
17321757
def connect(cls):
@@ -1796,6 +1821,7 @@ class _TestPostgreSQLAlchemy(object):
17961821
17971822
"""
17981823
flavor = 'postgresql'
1824+
supports_multivalues_insert = True
17991825

18001826
@classmethod
18011827
def connect(cls):

0 commit comments

Comments
 (0)