Skip to content

Commit 71b7087

Browse files
Merge pull request #7678 from yelite/sql-bool-bug-fix
FIX: to_sql takes the boolean column as text column
2 parents e060616 + 9708c4e commit 71b7087

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

doc/source/v0.14.1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,5 @@ Bug Fixes
288288
- Bug in ``pandas.core.strings.str_contains`` does not properly match in a case insensitive fashion when ``regex=False`` and ``case=False`` (:issue:`7505`)
289289

290290
- Bug in ``expanding_cov``, ``expanding_corr``, ``rolling_cov``, and ``rolling_corr`` for two arguments with mismatched index (:issue:`7512`)
291+
292+
- Bug in ``to_sql`` taking the boolean column as text column (:issue:`7678`)

pandas/io/sql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def _sqlalchemy_type(self, arr_or_dtype):
733733
elif com.is_integer_dtype(arr_or_dtype):
734734
# TODO: Refine integer size.
735735
return BigInteger
736-
elif com.is_bool(arr_or_dtype):
736+
elif com.is_bool_dtype(arr_or_dtype):
737737
return Boolean
738738
return Text
739739

pandas/io/tests/test_sql.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ def _load_test1_data(self):
191191

192192
self.test_frame1 = DataFrame(data, columns=columns)
193193

194+
def _load_test2_data(self):
195+
df = DataFrame(dict(A=[4, 1, 3, 6],
196+
B=['asd', 'gsq', 'ylt', 'jkl'],
197+
C=[1.1, 3.1, 6.9, 5.3],
198+
D=[False, True, True, False],
199+
E=['1990-11-22', '1991-10-26', '1993-11-26', '1995-12-12']))
200+
df['E'] = to_datetime(df['E'])
201+
202+
self.test_frame3 = df
203+
204+
def _load_test3_data(self):
205+
columns = ['index', 'A', 'B']
206+
data = [(
207+
'2000-01-03 00:00:00', 2 ** 31 - 1, -1.987670),
208+
('2000-01-04 00:00:00', -29, -0.0412318367011),
209+
('2000-01-05 00:00:00', 20000, 0.731167677815),
210+
('2000-01-06 00:00:00', -290867, 1.56762092543)]
211+
212+
self.test_frame3 = DataFrame(data, columns=columns)
213+
194214
def _load_raw_sql(self):
195215
self.drop_table('types_test_data')
196216
self._get_exec().execute(SQL_STRINGS['create_test_types'][self.flavor])
@@ -331,6 +351,8 @@ def setUp(self):
331351
self.conn = self.connect()
332352
self._load_iris_data()
333353
self._load_test1_data()
354+
self._load_test2_data()
355+
self._load_test3_data()
334356
self._load_raw_sql()
335357

336358
def test_read_sql_iris(self):
@@ -391,6 +413,13 @@ def test_to_sql_append(self):
391413
self.assertEqual(
392414
num_rows, num_entries, "not the same number of rows as entries")
393415

416+
def test_to_sql_type_mapping(self):
417+
sql.to_sql(self.test_frame3, 'test_frame5',
418+
self.conn, flavor='sqlite', index=False)
419+
result = sql.read_sql("SELECT * FROM test_frame5", self.conn)
420+
421+
tm.assert_frame_equal(self.test_frame3, result)
422+
394423
def test_to_sql_series(self):
395424
s = Series(np.arange(5, dtype='int64'), name='series')
396425
sql.to_sql(s, "test_series", self.conn, flavor='sqlite', index=False)
@@ -651,35 +680,23 @@ class TestSQLLegacyApi(_TestSQLApi):
651680
def connect(self, database=":memory:"):
652681
return sqlite3.connect(database)
653682

654-
def _load_test2_data(self):
655-
columns = ['index', 'A', 'B']
656-
data = [(
657-
'2000-01-03 00:00:00', 2 ** 31 - 1, -1.987670),
658-
('2000-01-04 00:00:00', -29, -0.0412318367011),
659-
('2000-01-05 00:00:00', 20000, 0.731167677815),
660-
('2000-01-06 00:00:00', -290867, 1.56762092543)]
661-
662-
self.test_frame2 = DataFrame(data, columns=columns)
663-
664683
def test_sql_open_close(self):
665684
# Test if the IO in the database still work if the connection closed
666685
# between the writing and reading (as in many real situations).
667686

668-
self._load_test2_data()
669-
670687
with tm.ensure_clean() as name:
671688

672689
conn = self.connect(name)
673-
sql.to_sql(self.test_frame2, "test_frame2_legacy", conn,
690+
sql.to_sql(self.test_frame3, "test_frame3_legacy", conn,
674691
flavor="sqlite", index=False)
675692
conn.close()
676693

677694
conn = self.connect(name)
678-
result = sql.read_sql_query("SELECT * FROM test_frame2_legacy;",
695+
result = sql.read_sql_query("SELECT * FROM test_frame3_legacy;",
679696
conn)
680697
conn.close()
681698

682-
tm.assert_frame_equal(self.test_frame2, result)
699+
tm.assert_frame_equal(self.test_frame3, result)
683700

684701
def test_read_sql_delegate(self):
685702
iris_frame1 = sql.read_sql_query("SELECT * FROM iris", self.conn)

0 commit comments

Comments
 (0)