Skip to content

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

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
2 of 3 tasks
CaselIT opened this issue Mar 29, 2021 · 4 comments · Fixed by #43253
Closed
2 of 3 tasks

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

CaselIT opened this issue Mar 29, 2021 · 4 comments · Fixed by #43253
Assignees
Labels
Bug Dependencies Required and optional dependencies IO SQL to_sql, read_sql, read_sql_query
Milestone

Comments

@CaselIT
Copy link

CaselIT commented Mar 29, 2021

  • 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

@CaselIT CaselIT added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 29, 2021
@CaselIT CaselIT changed the title BUG: DataFrame from sqlalchemy Query missing column headers BUG: DataFrame from sqlalchemy 1.4 Query missing column headers Mar 29, 2021
@Sudomarko
Copy link

I will make the change

@lithomas1 lithomas1 added Dependencies Required and optional dependencies IO SQL to_sql, read_sql, read_sql_query and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 30, 2021
@lithomas1 lithomas1 added this to the Contributions Welcome milestone Mar 30, 2021
@jnchngc
Copy link

jnchngc commented Apr 10, 2021

take

@jnchngc
Copy link

jnchngc commented Apr 10, 2021

I've been working on the issue. Just wondering, is it okay to install packages from other open-source libraries?

@notcoffeetable
Copy link

Is progress being made on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment