I have a dataframe that looks like this:
df = pd.DataFrame(index= pd.date_range(\'2014-01-01\', periods=10))
df[\'date\'] = df.index.map(lambda x: x.strftime(\'%
Update: starting with pandas 0.15, to_sql
supports writing datetime values for both sqlite connections as sqlalchemy engines. So the workaround described below should not be needed anymore.
Pandas 0.15 will be released in coming October, and the feature is merged in the development version.
The reason of the above error is that the df
'date' column is a datetime64
column, and this type is not supported by sqlite3. So you should convert it to a string first (that this is not done automatically for sqlite is maybe a bug/missing feature), or to a datetime.date
object (which is recognized by sqlite3, but it will also be converted to a string as sqlite has no datetime type).
You did that in your code example with df['date'] = df.index.map(lambda x: x.strftime('%d-%m-%Y'))
, but then you overwrite the column again with df['date'] = df.index
, so maybe that was an error in your code example. But if you first convert it to strings, it works:
df = pd.DataFrame(index= pd.date_range('2014-01-01', periods=10))
df['date'] = df.index.map(lambda x: x.strftime('%d-%m-%Y'))
df['profit']= rand(10)
df['perf_period_id']=2
df.to_sql(name='fee_profit', index=False, con=db, if_exists='append')
Starting from pandas 0.14, the main sql functions are refactored to use sqlalchemy to deal with different database flavors. If you use this, it will work correctly with the datetime column (it will convert it to a string automatically):
df = pd.DataFrame(index= pd.date_range('2014-01-01', periods=10))
df['profit']= rand(10)
df['perf_period_id']=2
import sqlalchemy
db2 = sqlalchemy.create_engine('...')
df.to_sql(name='fee_profit', index=False, con=db2, if_exists='append')
Using a plain sqlite connection object instead of a sqlalchemy engine, like you did, will still be supported in the future (but only for sqlite!).