是窃听器还是我做错了什么?我创建一个df,把它放在sql表中,df和表中有一个列,其中有一个点。现在,当我从sql表中读取df时,列名是不一样的。我写了这段小代码让人们可以测试它。
import sqlalchemy
import pandas as pd
import numpy as np
engine = sqlalchemy.create_engine('sqlite:///test.sqlite')
dfin = pd.DataFrame(np.random.randn(10,2), columns=['column with a . dot', 'without'])
print(dfin)
dfin.to_sql('testtable', engine, if_exists='fail')
tables = engine.table_names()
for table in tables:
sql = 'SELECT t.* FROM "' + table + '" t'
dfout = pd.read_sql(sql, engine)
print(dfout.columns)
print dfout发布于 2015-02-04 10:33:42
解决方案是将sqlite_raw_colnames=True传递给引擎。
In [141]: engine = sqlalchemy.create_engine('sqlite:///', execution_options={'sqlite_raw_colnames':True})
In [142]: dfin.to_sql('testtable', engine, if_exists='fail')
In [143]: pd.read_sql("SELECT * FROM testtable", engine).head()
Out[143]:
index column with a . dot without
0 0 0.213645 0.321328
1 1 -0.511033 0.496510
2 2 -1.114511 -0.030571
3 3 -1.370342 0.359123
4 4 0.101111 -1.010498SQLAlchemy故意删除点(在某些情况下,SQLite可能会将名称存储为"tablename.colname"),参见sqlalchemy+sqlite stripping column names with dots?和https://groups.google.com/forum/?hl=en&fromgroups#!topic/sqlalchemy/EqAuTFlMNZk。
这似乎是一个bug,但在熊猫read_sql函数中并不一定如此,因为它依赖于SQLAlchemy ResultProxy对象的keys方法来确定列名。这似乎截断了列名:
In [15]: result = engine.execute("SELECT * FROM testtable")
In [16]: result.keys()
Out[16]: [u'index', u' dot', u'without']所以问题是,这是SQLAlchemy中的一个bug,还是熊猫应该做一个解决办法(比如使用result.cursor.description,给出正确的名称)。
现在,您还可以使用sqlite回退模式,使用DBAPI连接而不是SQLAlchemy引擎(因为这依赖于cursor.description,这里使用正确的列名:
In [20]: con = sqlite3.connect(':memory:')
In [21]: dfin.to_sql('testtable', con, if_exists='fail')
In [22]: pd.read_sql("SELECT * FROM testtable", con).head()
Out[22]:
index column with a . dot without
0 0 0.213645 0.321328
1 1 -0.511033 0.496510
2 2 -1.114511 -0.030571
3 3 -1.370342 0.359123
4 4 0.101111 -1.010498https://stackoverflow.com/questions/28318722
复制相似问题