Skip to content

systematic bug in pandas/io/sql.py when trying to write a dataframe of one column of integers into sqlite #3628

Closed
@stonebig

Description

@stonebig

Hello,

trying this code with pandasql, with ipython, I get a bogus result .

import pandas as pd

%load https://raw.github.com/yhat/pandasql/master/pandasql/sqldf.py

df=pd.DataFrame([ 1, 2], columns=['f'])
sqldf("select * from df ", locals())

this gives the mysterious (binary ?) output:
f
0 (1, 0, 0, 0, 0, 0, 0, 0)
1 (2, 0, 0, 0, 0, 0, 0, 0)

Digging further, I found that the problem is in those lines generated by pandas/io/sql/py :

import pandas as pd
import sqlite3
df=pd.DataFrame([ 1, 2], columns=['f' ])
con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE df ( [f] INTEGER );")
data = [tuple(x) for x in df.values]
con.executemany("INSERT INTO df ([f]) VALUES (?)", data)

here is the way to see the bogus result

for row in con.execute("select * from f"):
print (row)

this print :

(b'\x01\x00\x00\x00\x00\x00\x00\x00',)

(b'\x02\x00\x00\x00\x00\x00\x00\x00',)

instead of expected :

(1,)

(2,)

I can't guess further what is causing the issue, it seems there is a misunderstanding between pandas and sqlite when passing a "data" list of integers.

problem doesn't exist if we switch to non-integers like :
df=pd.DataFrame([ 1., 2], columns=['f' ])

Nota : I'm under windows vista 32 bits, python 3.3, in a country where the decimal separator is a comma (,) , maybe it's related to the problem, maybe not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO DataIO issues that don't fit into a more specific label

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions