diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index 4caf22357b1d3..267343ec28934 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -441,6 +441,8 @@ Enhancements df.to_sql('table', engine, schema='other_schema') pd.read_sql_table('table', engine, schema='other_schema') +- Added support for writing datetime64 columns with ``to_sql`` for all database flavors (:issue:`7103`). + - Added support for bool, uint8, uint16 and uint32 datatypes in ``to_stata`` (:issue:`7097`, :issue:`7365`) - Added ``layout`` keyword to ``DataFrame.plot`` (:issue:`6667`) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index b72c41e45c9ca..fc747a692d8da 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -540,6 +540,23 @@ def pandasSQL_builder(con, flavor=None, schema=None, meta=None, is_cursor=False) return PandasSQLLegacy(con, flavor, is_cursor=is_cursor) +try: + import sqlalchemy.types as types + + class Timestamp(types.TypeDecorator): + """convert from pandas.tslib.Timestamp type """ + + impl = types.DateTime + + def process_bind_param(self, value, dialect): + f = getattr(value,'to_datetime', None) + if f is not None: + return f() + return value +except: + pass + + class PandasSQLTable(PandasObject): """ For mapping Pandas tables to SQL tables. @@ -784,7 +801,7 @@ def _sqlalchemy_type(self, col): tz = col.tzinfo return DateTime(timezone=True) except: - return DateTime + return Timestamp if com.is_timedelta64_dtype(col): warnings.warn("the 'timedelta' type is not supported, and will be " "written as integer values (ns frequency) to the " diff --git a/pandas/io/tests/test_sql.py b/pandas/io/tests/test_sql.py index 93c95169a60d1..3f1594d2f8166 100644 --- a/pandas/io/tests/test_sql.py +++ b/pandas/io/tests/test_sql.py @@ -925,9 +925,6 @@ def test_date_parsing(self): "IntDateCol loaded with incorrect type") def test_datetime(self): - if self.driver == 'pymysql': - raise nose.SkipTest('writing datetime not working with pymysql') - df = DataFrame({'A': date_range('2013-01-01 09:00:00', periods=3), 'B': np.arange(3.0)}) df.to_sql('test_datetime', self.conn)