From d8f844439134be0eacf6257ef0ce0eb856ce0bca Mon Sep 17 00:00:00 2001 From: stonebig Date: Fri, 17 May 2013 19:59:01 +0200 Subject: [PATCH 1/3] systematic failure when writing a dataframe of 1 column of integer to sqlite ( Issue #3628 ) A push of a dataframe of one column of integers fail ungraciously, because sqlite doesn't recognize well the data type. This should solve the problem by converting back to normal types --- pandas/io/sql.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 3002f2f620f5e..b54a30d95bb54 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -228,7 +228,11 @@ def _write_sqlite(frame, table, names, cur): wildcards = ','.join(['?'] * len(names)) insert_query = 'INSERT INTO %s (%s) VALUES (%s)' % ( table, col_names, wildcards) - data = [tuple(x) for x in frame.values] + # pandas types are badly handled if there is only 1 column ( Issue #3628 ) + if not len(frame.columns )==1 : + data = [tuple(x) for x in frame.values] + else : + data = [tuple(x) for x in frame.values.tolist()] cur.executemany(insert_query, data) def _write_mysql(frame, table, names, cur): From 3f50801d75498772991865461b8f3009d34dff1b Mon Sep 17 00:00:00 2001 From: stonebig Date: Sat, 18 May 2013 13:07:26 +0300 Subject: [PATCH 2/3] testing Issue #3628 checks if the push of a dataframe of one column to sql (sqlite) works --- pandas/io/tests/test_sql.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/io/tests/test_sql.py b/pandas/io/tests/test_sql.py index b443c55f97b8d..bcaff716f66ea 100644 --- a/pandas/io/tests/test_sql.py +++ b/pandas/io/tests/test_sql.py @@ -219,6 +219,17 @@ def test_keyword_as_column_names(self): df = DataFrame({'From':np.ones(5)}) sql.write_frame(df, con = self.db, name = 'testkeywords') + def test_onecolumn_of_integer(self): + ''' + a column_of_integers dataframe should transfer well to sql + ''' + mono_df=DataFrame([1 , 2], columns=['c0']) + sql.write_frame(mono_df, con = self.db, name = 'mono_df') + # computing the sum via sql + the_sum=sum([my_c0[0] for my_c0 in con.execute("select * from mono_df")]) + # it should not fail, and gives 3 ( Issue #3628 ) + self.assertEqual(the_sum , 3) + class TestMySQL(unittest.TestCase): From db8894dba35f53ecf85f82a9f606e196badcd6f1 Mon Sep 17 00:00:00 2001 From: stonebig Date: Sat, 18 May 2013 13:32:17 +0300 Subject: [PATCH 3/3] Missed a line in the test of Issue #3628 --- pandas/io/tests/test_sql.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/io/tests/test_sql.py b/pandas/io/tests/test_sql.py index bcaff716f66ea..1daa50c70a900 100644 --- a/pandas/io/tests/test_sql.py +++ b/pandas/io/tests/test_sql.py @@ -226,7 +226,8 @@ def test_onecolumn_of_integer(self): mono_df=DataFrame([1 , 2], columns=['c0']) sql.write_frame(mono_df, con = self.db, name = 'mono_df') # computing the sum via sql - the_sum=sum([my_c0[0] for my_c0 in con.execute("select * from mono_df")]) + con_x=self.db + the_sum=sum([my_c0[0] for my_c0 in con_x.execute("select * from mono_df")]) # it should not fail, and gives 3 ( Issue #3628 ) self.assertEqual(the_sum , 3)