Skip to content

ENH: sql support for Timestamp (GH7103) #8205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
19 changes: 18 additions & 1 deletion pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 "
Expand Down
3 changes: 0 additions & 3 deletions pandas/io/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down