Skip to content

BUG: DataFrame from sqlalchemy 1.4 Query missing column headers #40682

Closed
@CaselIT

Description

@CaselIT
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
from sqlalchemy import create_engine, Table, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import pandas as pd
import sqlalchemy as sa

BaseModel = declarative_base()


class Test(BaseModel):
    __tablename__ = "test"
    id = Column(Integer, primary_key=True)
    foo = Column(String(50))


engine = create_engine("sqlite:///")
BaseModel.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

df = pd.DataFrame({"id": [0, 1], "foo": ["hello", "world"]})
df.to_sql("test", con=engine, index=False, if_exists="replace")

session.commit()
foo = session.query(Test.id, Test.foo)
df = pd.DataFrame(foo)
session.close()
print(df)
assert list(df.columns) == ["id", "foo"]

Problem description

Coping from sqlalchemy/sqlalchemy#6154.
In sqlalchemy 1.4 Query returns Row objects that are no longer subclass of tuple, so they don't pass the is_named_tuple check, as mentioned by Mike here sqlalchemy/sqlalchemy#6154 (comment)

Expected Output

The colums are named 0,1 instead of 'id', 'foo'

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.8.8.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.21337
machine : AMD64
processor : Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252

pandas : 1.2.3
numpy : 1.19.2
pytz : 2019.3
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0.post20210125
Cython : None
pytest : 6.2.1
hypothesis : None
sphinx : 3.5.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : 0.9.3
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.10.3
IPython : 7.14.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.4.4 (actually master)
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.52.0

Metadata

Metadata

Assignees

Labels

BugDependenciesRequired and optional dependenciesIO SQLto_sql, read_sql, read_sql_query

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions