Skip to content

BUG: Exception raised when to_sql used to insert empty dataframe #8316

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 1 commit into from
Sep 19, 2014
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,15 @@ def insert(self, chunksize=None):
keys, data_list = self.insert_data()

nrows = len(self.frame)

if nrows == 0:
return

if chunksize is None:
chunksize = nrows
elif chunksize == 0:
raise ValueError('chunksize argument should be non-zero')

chunks = int(nrows / chunksize) + 1

with self.pd_sql.run_transaction() as conn:
Expand Down
10 changes: 10 additions & 0 deletions pandas/io/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ def _to_sql(self):
# Nuke table
self.drop_table('test_frame1')

def _to_sql_empty(self):
self.drop_table('test_frame1')
self.pandasSQL.to_sql(self.test_frame1.iloc[:0], 'test_frame1')

def _to_sql_fail(self):
self.drop_table('test_frame1')

Expand Down Expand Up @@ -850,6 +854,9 @@ def test_read_sql_named_parameter(self):
def test_to_sql(self):
self._to_sql()

def test_to_sql_empty(self):
self._to_sql_empty()

def test_to_sql_fail(self):
self._to_sql_fail()

Expand Down Expand Up @@ -1346,6 +1353,9 @@ def test_read_sql_named_parameter(self):
def test_to_sql(self):
self._to_sql()

def test_to_sql_empty(self):
self._to_sql_empty()

def test_to_sql_fail(self):
self._to_sql_fail()

Expand Down