Skip to content

Commit 20f0c21

Browse files
committed
ENH: Allow to_sql to recognize single sql type pandas-dev#11886
PEP pandas-dev#3
1 parent 1a73316 commit 20f0c21

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

doc/source/whatsnew/v0.18.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Other enhancements
7474

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

77+
- ``DataFrame.to_sql `` now allows a single value as the SQL type for all columns (:issue:`11886`).
78+
7779
- The ``pd.read_csv()`` with ``engine='python'`` has gained support for the ``decimal`` option (:issue:`12933`)
7880

7981
- ``Index.astype()`` now accepts an optional boolean argument ``copy``, which allows optional copying if the requirements on dtype are satisfied (:issue:`13209`)

pandas/io/sql.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1233,14 +1233,15 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
12331233
chunksize : int, default None
12341234
If not None, then rows will be written in batches of this size at a
12351235
time. If None, all rows will be written at once.
1236-
dtype : single SQL type or dict of column name to SQL type, default None
1236+
dtype : single SQL type or dict of column name to SQL type, default
1237+
None
12371238
Optional specifying the datatype for columns. The SQL type should
1238-
be a SQLAlchemy type. If all columns are of the same type, one
1239+
be a SQLAlchemy type. If all columns are of the same type, one
12391240
single value can be used.
12401241
12411242
"""
12421243
if dtype and not is_dictlike(dtype):
1243-
dtype = {col_name : dtype for col_name in frame}
1244+
dtype = {col_name: dtype for col_name in frame}
12441245

12451246
if dtype is not None:
12461247
from sqlalchemy.types import to_instance, TypeEngine
@@ -1650,15 +1651,15 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
16501651
chunksize : int, default None
16511652
If not None, then rows will be written in batches of this
16521653
size at a time. If None, all rows will be written at once.
1653-
dtype : single SQL type or dict of column name to SQL type, default None
1654+
dtype : single SQL type or dict of column name to SQL type, default
1655+
None
16541656
Optional specifying the datatype for columns. The SQL type should
16551657
be a string. If all columns are of the same type, one single value
16561658
can be used.
16571659
16581660
"""
16591661
if dtype and not is_dictlike(dtype):
1660-
dtype = {col_name : dtype for col_name in frame}
1661-
1662+
dtype = {col_name: dtype for col_name in frame}
16621663
if dtype is not None:
16631664
for col, my_type in dtype.items():
16641665
if not isinstance(my_type, str):

pandas/io/tests/test_sql.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -1552,20 +1552,18 @@ def test_dtype(self):
15521552
self.assertTrue(isinstance(sqltype, sqlalchemy.String))
15531553
self.assertEqual(sqltype.length, 10)
15541554

1555-
def test_to_sql_save_indexgle_dtype(self):
1556-
self.drop('single_dtype_test')
1557-
cols = ['A','B']
1558-
data = [('a','b'),
1559-
('c','d')]
1560-
df = DataFrame(data,columns=cols)
1561-
df.to_sql('single_dtype_test',self.conn,dtype=sqlalchemy.TEXT)
1555+
def test_to_sql_single_dtype(self):
1556+
cols = ['A', 'B']
1557+
data = [('a', 'b'),
1558+
('c', 'd')]
1559+
df = DataFrame(data, columns=cols)
1560+
df.to_sql('single_dtype_test', self.conn, dtype=sqlalchemy.TEXT)
15621561
meta = sqlalchemy.schema.MetaData(bind=self.conn)
15631562
meta.reflect()
15641563
sqltypea = meta.tables['single_dtype_test'].columns['A'].type
15651564
sqltypeb = meta.tables['single_dtype_test'].columns['B'].type
15661565
self.assertTrue(isinstance(sqltypea, sqlalchemy.TEXT))
15671566
self.assertTrue(isinstance(sqltypeb, sqlalchemy.TEXT))
1568-
self.drop_table('single_dtype_test')
15691567

15701568
def test_notnull_dtype(self):
15711569
cols = {'Bool': Series([True, None]),
@@ -2044,15 +2042,17 @@ def test_to_sql_single_dtype(self):
20442042
if self.flavor == 'mysql':
20452043
raise nose.SkipTest('Not applicable to MySQL legacy')
20462044
self.drop_table('single_dtype_test')
2047-
cols = ['A','B']
2048-
data = [('a','b'),
2049-
('c','d')]
2050-
df = DataFrame(data,columns=cols)
2051-
df.to_sql('single_dtype_test',self.conn,dtype='STRING')
2052-
self.assertEqual(self._get_sqlite_column_type('single_dtype_test','A'),'STRING')
2053-
self.assertEqual(self._get_sqlite_column_type('single_dtype_test','B'),'STRING')
2045+
cols = ['A', 'B']
2046+
data = [('a', 'b'),
2047+
('c', 'd')]
2048+
df = DataFrame(data, columns=cols)
2049+
df.to_sql('single_dtype_test', self.conn, dtype='STRING')
2050+
self.assertEqual(
2051+
self._get_sqlite_column_type('single_dtype_test', 'A'), 'STRING')
2052+
self.assertEqual(
2053+
self._get_sqlite_column_type('single_dtype_test', 'B'), 'STRING')
20542054
self.drop_table('single_dtype_test')
2055-
2055+
20562056
def test_notnull_dtype(self):
20572057
if self.flavor == 'mysql':
20582058
raise nose.SkipTest('Not applicable to MySQL legacy')

0 commit comments

Comments
 (0)