Skip to content

Commit

Permalink
Merge pull request pandas-dev#9138 from jorisvandenbossche/sql-9083-d…
Browse files Browse the repository at this point in the history
…type-no-subclass

FIX: to_sql dtype argument accepting SQLAlchemy type instance (GH9083)
  • Loading branch information
jorisvandenbossche committed Dec 24, 2014
2 parents fa83859 + aad5ea5 commit 32a2c6f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ Bug Fixes

- Fixed bug in ``to_sql`` when mapping a Timestamp object column (datetime
column with timezone info) to the according sqlalchemy type (:issue:`9085`).

- Fixed bug in ``to_sql`` ``dtype`` argument not accepting an instantiated
SQLAlchemy type (:issue:`9083`).



Expand Down
4 changes: 2 additions & 2 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,9 +1159,9 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
"""
if dtype is not None:
import sqlalchemy.sql.type_api as type_api
from sqlalchemy.types import to_instance, TypeEngine
for col, my_type in dtype.items():
if not issubclass(my_type, type_api.TypeEngine):
if not isinstance(to_instance(my_type), TypeEngine):
raise ValueError('The type of %s is not a SQLAlchemy '
'type ' % col)

Expand Down
8 changes: 8 additions & 0 deletions pandas/io/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,14 @@ def test_dtype(self):
self.assertRaises(ValueError, df.to_sql,
'error', self.conn, dtype={'B': str})

# GH9083
df.to_sql('dtype_test3', self.conn, dtype={'B': sqlalchemy.String(10)})
meta.reflect()
sqltype = meta.tables['dtype_test3'].columns['B'].type
print(sqltype)
self.assertTrue(isinstance(sqltype, sqlalchemy.String))
self.assertEqual(sqltype.length, 10)

def test_notnull_dtype(self):
cols = {'Bool': Series([True,None]),
'Date': Series([datetime(2012, 5, 1), None]),
Expand Down

0 comments on commit 32a2c6f

Please sign in to comment.