|
14 | 14 | import pandas.lib as lib
|
15 | 15 | from pandas.types.missing import isnull
|
16 | 16 | from pandas.types.dtypes import DatetimeTZDtype
|
17 |
| -from pandas.types.common import (is_list_like, |
| 17 | +from pandas.types.common import (is_list_like, is_dict_like, |
18 | 18 | is_datetime64tz_dtype)
|
19 | 19 |
|
20 | 20 | from pandas.compat import (map, zip, raise_with_traceback,
|
@@ -448,9 +448,10 @@ def to_sql(frame, name, con, flavor=None, schema=None, if_exists='fail',
|
448 | 448 | chunksize : int, default None
|
449 | 449 | If not None, then rows will be written in batches of this size at a
|
450 | 450 | time. If None, all rows will be written at once.
|
451 |
| - dtype : dict of column name to SQL type, default None |
| 451 | + dtype : single SQLtype or dict of column name to SQL type, default None |
452 | 452 | Optional specifying the datatype for columns. The SQL type should
|
453 | 453 | be a SQLAlchemy type, or a string for sqlite3 fallback connection.
|
| 454 | + If all columns are of the same type, one single value can be used. |
454 | 455 |
|
455 | 456 | """
|
456 | 457 | if if_exists not in ('fail', 'replace', 'append'):
|
@@ -1121,11 +1122,15 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
|
1121 | 1122 | chunksize : int, default None
|
1122 | 1123 | If not None, then rows will be written in batches of this size at a
|
1123 | 1124 | time. If None, all rows will be written at once.
|
1124 |
| - dtype : dict of column name to SQL type, default None |
| 1125 | + dtype : single type or dict of column name to SQL type, default None |
1125 | 1126 | Optional specifying the datatype for columns. The SQL type should
|
1126 |
| - be a SQLAlchemy type. |
| 1127 | + be a SQLAlchemy type. If all columns are of the same type, one |
| 1128 | + single value can be used. |
1127 | 1129 |
|
1128 | 1130 | """
|
| 1131 | + if dtype and not is_dict_like(dtype): |
| 1132 | + dtype = {col_name: dtype for col_name in frame} |
| 1133 | + |
1129 | 1134 | if dtype is not None:
|
1130 | 1135 | from sqlalchemy.types import to_instance, TypeEngine
|
1131 | 1136 | for col, my_type in dtype.items():
|
@@ -1473,11 +1478,15 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
|
1473 | 1478 | chunksize : int, default None
|
1474 | 1479 | If not None, then rows will be written in batches of this
|
1475 | 1480 | size at a time. If None, all rows will be written at once.
|
1476 |
| - dtype : dict of column name to SQL type, default None |
| 1481 | + dtype : single type or dict of column name to SQL type, default None |
1477 | 1482 | Optional specifying the datatype for columns. The SQL type should
|
1478 |
| - be a string. |
| 1483 | + be a string. If all columns are of the same type, one single value |
| 1484 | + can be used. |
1479 | 1485 |
|
1480 | 1486 | """
|
| 1487 | + if dtype and not is_dict_like(dtype): |
| 1488 | + dtype = {col_name: dtype for col_name in frame} |
| 1489 | + |
1481 | 1490 | if dtype is not None:
|
1482 | 1491 | for col, my_type in dtype.items():
|
1483 | 1492 | if not isinstance(my_type, str):
|
|
0 commit comments