Skip to content

BUG: Timestamps in object column not written as DateTime data in to_sql #9085

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

When you have datetime data with timezone info attached, the data is stored as Timestamp objects in a object dtyped column, instead of in a ´datetime64´ column (as this cannot hold the timezone info).
But, this causes to_sql to gives this the TEXT type:

With a postgresql engine:

In [48]: times = ['201412120154', '201412110254']
   ....: df = pd.DataFrame()
   ....: df['time'] = pd.to_datetime(times, utc=True)

In [49]: df
Out[49]:
    time
0   2014-12-12 01:54:00+00:00
1   2014-12-11 02:54:00+00:00

In [50]: df.dtypes
Out[50]:
time    object
dtype: object

In [51]: df.time.values
Out[51]:
array([Timestamp('2014-12-12 01:54:00+0000', tz='UTC'),
       Timestamp('2014-12-11 02:54:00+0000', tz='UTC')], dtype=object)

In [53]: print pd.io.sql.get_schema(df, 'test', con=engine_postgres)

CREATE TABLE test (
    time TEXT
)

This is because:

In [54]: pd.lib.infer_dtype(df['time'])
Out[54]: 
'datetime'

instead of datetime64, and this case is not handled at the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    IO SQLto_sql, read_sql, read_sql_query

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions