Skip to content

Commit eddddd7

Browse files
Merge pull request #8316 from artemyk/empty_table_insert
BUG: Exception raised when `to_sql` used to insert empty dataframe
2 parents a713641 + 164f214 commit eddddd7

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pandas/io/sql.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,15 @@ def insert(self, chunksize=None):
654654
keys, data_list = self.insert_data()
655655

656656
nrows = len(self.frame)
657+
658+
if nrows == 0:
659+
return
660+
657661
if chunksize is None:
658662
chunksize = nrows
663+
elif chunksize == 0:
664+
raise ValueError('chunksize argument should be non-zero')
665+
659666
chunks = int(nrows / chunksize) + 1
660667

661668
with self.pd_sql.run_transaction() as conn:

pandas/io/tests/test_sql.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ def _to_sql(self):
253253
# Nuke table
254254
self.drop_table('test_frame1')
255255

256+
def _to_sql_empty(self):
257+
self.drop_table('test_frame1')
258+
self.pandasSQL.to_sql(self.test_frame1.iloc[:0], 'test_frame1')
259+
256260
def _to_sql_fail(self):
257261
self.drop_table('test_frame1')
258262

@@ -850,6 +854,9 @@ def test_read_sql_named_parameter(self):
850854
def test_to_sql(self):
851855
self._to_sql()
852856

857+
def test_to_sql_empty(self):
858+
self._to_sql_empty()
859+
853860
def test_to_sql_fail(self):
854861
self._to_sql_fail()
855862

@@ -1346,6 +1353,9 @@ def test_read_sql_named_parameter(self):
13461353
def test_to_sql(self):
13471354
self._to_sql()
13481355

1356+
def test_to_sql_empty(self):
1357+
self._to_sql_empty()
1358+
13491359
def test_to_sql_fail(self):
13501360
self._to_sql_fail()
13511361

0 commit comments

Comments
 (0)