Skip to content

problem writing DateTime data to sqlite with sql.write_frame #6561

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
lebedov opened this issue Mar 6, 2014 · 9 comments
Closed

problem writing DateTime data to sqlite with sql.write_frame #6561

lebedov opened this issue Mar 6, 2014 · 9 comments
Labels
IO SQL to_sql, read_sql, read_sql_query
Milestone

Comments

@lebedov
Copy link

lebedov commented Mar 6, 2014

Although this problem was ostensibly solved, it still seems to affect the latest commit as of the present time; the following code throws an InterfaceError at the write_frame() call; changing the dtype of the date column to something other than DateTime (e.g., by removing the parameter parse_dates=[1] in the call to read_csv()) circumvents the problem:

import StringIO
import sqlite3
import pandas.io.sql

data_csv = u"""foo,2012-01-01                                                                                                                  
foo,2012-01-02                                                                                                                                 
bar,2011-01-01                                                                                                                                 
qux,2011-01-01                                                                                                                                 
"""
buf = StringIO.StringIO(data_csv)

df = pandas.read_csv(buf, header=None, 
       names=['name', 'date'], parse_dates=[1])

conn = sqlite3.connect(':memory:')
cursor = conn.cursor()
cursor.execute('create table data (name string, date date)')

pandas.io.sql.write_frame(df, 'data', conn, 'sqlite', 'append', False)
@jreback
Copy link
Contributor

jreback commented Apr 6, 2014

@jorisvandenbossche is this an open issue?

@jreback jreback added the SQL label Apr 6, 2014
@jreback jreback added this to the 0.14.0 milestone Apr 6, 2014
@lebedov
Copy link
Author

lebedov commented Apr 6, 2014

(Still seems to be a problem as of commit 657d255.)

@jreback
Copy link
Contributor

jreback commented Apr 6, 2014

@lebedov I think you need to use to_sql() to get the new functionaility. write_frame is being deprecated.

@lebedov
Copy link
Author

lebedov commented Apr 6, 2014

Changing the last line of the code in the original post to

df.to_sql('data', conn, 'sqlite', 'append', False)

still results in an InterfaceError.

@jreback
Copy link
Contributor

jreback commented Apr 6, 2014

AFAICT this is a fundamental problem with sqlite3 itself. I would think that SQLAlchemy could/would convert the datetimes to an appropriate format, but maybe that is not correct, see here: http://www.sqlite.org/datatype3.html.

I don't think this ever worked (the issue you pointed to simply closed the issue as wont' fix for an older version).

@lebedov
Copy link
Author

lebedov commented Apr 6, 2014

Using the latest code with SQLAlchemy 0.9.4 seems to work (assuming that the type affinity of the name column is explicitly set to text):

import StringIO
import sqlalchemy
import pandas.io.sql

data_csv = u"""foo,2012-01-01                                                                                                                  
foo,2012-01-02                                                                                                                                 
bar,2011-01-01                                                                                                                                 
qux,2011-01-01                                                                                                                                 
"""
buf = StringIO.StringIO(data_csv)

df = pandas.read_csv(buf, header=None,
               names=['name', 'date'], parse_dates=[1])

eng = sqlalchemy.create_engine('sqlite://')
eng.execute('create table data (name text, date date)')

df.to_sql('data', eng, 'sqlite', 'append', False)

@jorisvandenbossche
Copy link
Member

@lebedov late answer, but the fact that you have to create the sql table yourself shouldn't be necessary. Just using df.to_sql('data', eng, index=False) should work. Otherwise it would be a bug. Can you test that? (what error did you get if you didn't explicitely set the column to text?)

@lebedov
Copy link
Author

lebedov commented Apr 25, 2014

Yes - that seems to work with the latest pandas code as of today.

@jorisvandenbossche
Copy link
Member

OK, thanks for checking!

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

No branches or pull requests

3 participants