Skip to content

Commit

Permalink
ENH: Allow to_sql to recognize single sql type pandas-dev#11886
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulHP committed May 27, 2016
1 parent 1a73316 commit 20f0c21
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Other enhancements

pd.Timestamp(year=2012, month=1, day=1, hour=8, minute=30)

- ``DataFrame.to_sql `` now allows a single value as the SQL type for all columns (:issue:`11886`).

- The ``pd.read_csv()`` with ``engine='python'`` has gained support for the ``decimal`` option (:issue:`12933`)

- ``Index.astype()`` now accepts an optional boolean argument ``copy``, which allows optional copying if the requirements on dtype are satisfied (:issue:`13209`)
Expand Down
13 changes: 7 additions & 6 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,14 +1233,15 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
chunksize : int, default None
If not None, then rows will be written in batches of this size at a
time. If None, all rows will be written at once.
dtype : single SQL type or dict of column name to SQL type, default None
dtype : single SQL type or dict of column name to SQL type, default
None
Optional specifying the datatype for columns. The SQL type should
be a SQLAlchemy type. If all columns are of the same type, one
be a SQLAlchemy type. If all columns are of the same type, one
single value can be used.
"""
if dtype and not is_dictlike(dtype):
dtype = {col_name : dtype for col_name in frame}
dtype = {col_name: dtype for col_name in frame}

if dtype is not None:
from sqlalchemy.types import to_instance, TypeEngine
Expand Down Expand Up @@ -1650,15 +1651,15 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
chunksize : int, default None
If not None, then rows will be written in batches of this
size at a time. If None, all rows will be written at once.
dtype : single SQL type or dict of column name to SQL type, default None
dtype : single SQL type or dict of column name to SQL type, default
None
Optional specifying the datatype for columns. The SQL type should
be a string. If all columns are of the same type, one single value
can be used.
"""
if dtype and not is_dictlike(dtype):
dtype = {col_name : dtype for col_name in frame}

dtype = {col_name: dtype for col_name in frame}
if dtype is not None:
for col, my_type in dtype.items():
if not isinstance(my_type, str):
Expand Down
32 changes: 16 additions & 16 deletions pandas/io/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1552,20 +1552,18 @@ def test_dtype(self):
self.assertTrue(isinstance(sqltype, sqlalchemy.String))
self.assertEqual(sqltype.length, 10)

def test_to_sql_save_indexgle_dtype(self):
self.drop('single_dtype_test')
cols = ['A','B']
data = [('a','b'),
('c','d')]
df = DataFrame(data,columns=cols)
df.to_sql('single_dtype_test',self.conn,dtype=sqlalchemy.TEXT)
def test_to_sql_single_dtype(self):
cols = ['A', 'B']
data = [('a', 'b'),
('c', 'd')]
df = DataFrame(data, columns=cols)
df.to_sql('single_dtype_test', self.conn, dtype=sqlalchemy.TEXT)
meta = sqlalchemy.schema.MetaData(bind=self.conn)
meta.reflect()
sqltypea = meta.tables['single_dtype_test'].columns['A'].type
sqltypeb = meta.tables['single_dtype_test'].columns['B'].type
self.assertTrue(isinstance(sqltypea, sqlalchemy.TEXT))
self.assertTrue(isinstance(sqltypeb, sqlalchemy.TEXT))
self.drop_table('single_dtype_test')

def test_notnull_dtype(self):
cols = {'Bool': Series([True, None]),
Expand Down Expand Up @@ -2044,15 +2042,17 @@ def test_to_sql_single_dtype(self):
if self.flavor == 'mysql':
raise nose.SkipTest('Not applicable to MySQL legacy')
self.drop_table('single_dtype_test')
cols = ['A','B']
data = [('a','b'),
('c','d')]
df = DataFrame(data,columns=cols)
df.to_sql('single_dtype_test',self.conn,dtype='STRING')
self.assertEqual(self._get_sqlite_column_type('single_dtype_test','A'),'STRING')
self.assertEqual(self._get_sqlite_column_type('single_dtype_test','B'),'STRING')
cols = ['A', 'B']
data = [('a', 'b'),
('c', 'd')]
df = DataFrame(data, columns=cols)
df.to_sql('single_dtype_test', self.conn, dtype='STRING')
self.assertEqual(
self._get_sqlite_column_type('single_dtype_test', 'A'), 'STRING')
self.assertEqual(
self._get_sqlite_column_type('single_dtype_test', 'B'), 'STRING')
self.drop_table('single_dtype_test')

def test_notnull_dtype(self):
if self.flavor == 'mysql':
raise nose.SkipTest('Not applicable to MySQL legacy')
Expand Down

0 comments on commit 20f0c21

Please sign in to comment.