Skip to content
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

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

Closed
jorisvandenbossche opened this issue Dec 15, 2014 · 0 comments · Fixed by #9109
Closed

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

jorisvandenbossche opened this issue Dec 15, 2014 · 0 comments · Fixed by #9109
Labels
IO SQL to_sql, read_sql, read_sql_query
Milestone

Comments

@jorisvandenbossche
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant