|
18 | 18 | string_types, text_type)
|
19 | 19 | from pandas.core.api import DataFrame, Series
|
20 | 20 | from pandas.core.common import isnull
|
| 21 | +from pandas.core.generic import is_dictlike |
21 | 22 | from pandas.core.base import PandasObject
|
22 | 23 | from pandas.types.api import DatetimeTZDtype
|
23 | 24 | from pandas.tseries.tools import to_datetime
|
@@ -550,9 +551,10 @@ def to_sql(frame, name, con, flavor='sqlite', schema=None, if_exists='fail',
|
550 | 551 | chunksize : int, default None
|
551 | 552 | If not None, then rows will be written in batches of this size at a
|
552 | 553 | time. If None, all rows will be written at once.
|
553 |
| - dtype : dict of column name to SQL type, default None |
| 554 | + dtype : single SQLtype or dict of column name to SQL type, default None |
554 | 555 | Optional specifying the datatype for columns. The SQL type should
|
555 | 556 | be a SQLAlchemy type, or a string for sqlite3 fallback connection.
|
| 557 | + If all columns are of the same type, one single value can be used. |
556 | 558 |
|
557 | 559 | """
|
558 | 560 | if if_exists not in ('fail', 'replace', 'append'):
|
@@ -1231,11 +1233,15 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
|
1231 | 1233 | chunksize : int, default None
|
1232 | 1234 | If not None, then rows will be written in batches of this size at a
|
1233 | 1235 | time. If None, all rows will be written at once.
|
1234 |
| - dtype : dict of column name to SQL type, default None |
| 1236 | + dtype : single SQL type or dict of column name to SQL type, default None |
1235 | 1237 | Optional specifying the datatype for columns. The SQL type should
|
1236 |
| - be a SQLAlchemy type. |
| 1238 | + be a SQLAlchemy type. If all columns are of the same type, one |
| 1239 | + single value can be used. |
1237 | 1240 |
|
1238 | 1241 | """
|
| 1242 | + if dtype and not is_dictlike(dtype): |
| 1243 | + dtype = {col_name : dtype for col_name in frame} |
| 1244 | + |
1239 | 1245 | if dtype is not None:
|
1240 | 1246 | from sqlalchemy.types import to_instance, TypeEngine
|
1241 | 1247 | for col, my_type in dtype.items():
|
@@ -1644,11 +1650,15 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
|
1644 | 1650 | chunksize : int, default None
|
1645 | 1651 | If not None, then rows will be written in batches of this
|
1646 | 1652 | size at a time. If None, all rows will be written at once.
|
1647 |
| - dtype : dict of column name to SQL type, default None |
| 1653 | + dtype : single SQL type or dict of column name to SQL type, default None |
1648 | 1654 | Optional specifying the datatype for columns. The SQL type should
|
1649 |
| - be a string. |
| 1655 | + be a string. If all columns are of the same type, one single value |
| 1656 | + can be used. |
1650 | 1657 |
|
1651 | 1658 | """
|
| 1659 | + if dtype and not is_dictlike(dtype): |
| 1660 | + dtype = {col_name : dtype for col_name in frame} |
| 1661 | + |
1652 | 1662 | if dtype is not None:
|
1653 | 1663 | for col, my_type in dtype.items():
|
1654 | 1664 | if not isinstance(my_type, str):
|
|
0 commit comments