7

I have a single column dataframe df which has column TS where

In [1]: type(df.TS.values[0])
Out[1]: pandas.tslib.Timestamp

I convert the column to type datetime.datetime()

In [2]: import datetime
In [3]: df['TS'] = df['TS'].astype(datetime.datetime)

I want to write this dataframe to a table in a mssql 2008 database using pd.to_sql(). So, I run the following.

In [4]: coltype = {'TS': sqlalchemy.TIMESTAMP}

In [5]: df.to_sql(name='Table', con=engine, if_exists='append', index=False, dtype=coltype)
Out[5]: ValueError: df (<class 'sqlalchemy.sql.sqltypes.TIMESTAMP'>) not a string

I have also tried not converting the column to datetime.datetime() and get same error. I have looked through the sqlalchemy documentation on column types but cannot figure out what parameter is supposed to pass. Can someone help me understand what I should be passing to write the column to the db as a datetime object?

4
  • Does the table already exist and, if so, is the TS column of String type? Commented Oct 8, 2015 at 19:28
  • @JoeCondron Yes the dataframe 'already exists', but if it helps, it is the result object of running a query through pd.read_sql(). Also, no the entries in df.TS have type pandas.tslib.Timestamp, which I then convert to datetime.datetime. However, running df.dtypes returns datetime64[ns]. Commented Oct 8, 2015 at 20:50
  • 2
    You shouldn't need to specify the dtypes yourself (datetime values are supported). Can you try just df.to_sql(name='Table', con=engine, if_exists='append', index=False)? Commented Oct 8, 2015 at 21:33
  • 1
    @joris Yes, you are right, I had done that anyway but for some reason thought it would insert them all as TEXT. I should have checked. Thank you. Commented Oct 8, 2015 at 22:15

2 Answers 2

4

In my case (working with a PostgreSQL database) the data type for the timestamp column is sqlalchemy.DateTime

import sqlalchemy    

dtype = {
    "TS": sqlalchemy.DateTime
}
df.to_sql(name="Table", con=engine, if_exists="append", index=False, dtype=dtype)
Sign up to request clarification or add additional context in comments.

Comments

1

As @joris said

You shouldn't need to specify the dtypes yourself (datetime values are supported).

So, removing dtype=coltype should solve your problem

df.to_sql(name='Table', con=engine, if_exists='append', index=False) 

The dtype is useful for integers with missing values.

To note:

while pandas is forced to store the data as floating point, the database supports nullable integers. When fetching the data with Python, we get back integer scalars. Source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.