Skip to content

BUG: #53028

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

Open
3 tasks done
SkeptiCali opened this issue May 1, 2023 · 5 comments
Open
3 tasks done

BUG: #53028

SkeptiCali opened this issue May 1, 2023 · 5 comments
Labels
Bug IO SQL to_sql, read_sql, read_sql_query Regression Functionality that used to work in a prior pandas version

Comments

@SkeptiCali
Copy link

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import pymysql
import openpyxl
from openpyxl import Workbook, load_workbook
from openpyxl.styles import Font, PatternFill
from openpyxl.utils.dataframe import dataframe_to_rows

import imports_local.mysql_connector_ygrene as mc

conn = pymysql.connect(
        host='localhost',
        user='user1', 
        password = "password1",
        db='cali',
        cursorclass=pymysql.cursors.DictCursor
        )

df=pd.read_sql_query('SELECT * FROM sample_data LIMIT 2',conn)

print(df)

Issue Description

When using DictCursor the data returned from read_sql_query is just a dataframe of column names. If you change the cursorclass to cursor all is well. I also validated that the issues was not there in 1.5.3. The issue was first noticed in v. 2.0.0 and is there in 2.0.1

Example data output:

id value state_id county_id district_id zipcode plus4
0 id value state_id county_id district_id zipcode plus4
1 id value state_id county_id district_id zipcode plus4

Expected Behavior

id   value  state_id  county_id  district_id zipcode plus4

0 117 ABCDEF 5 12 12 95823 6312
1 163 DEFRDC 5 12 12 95814 4713

Installed Versions

INSTALLED VERSIONS

commit : 37ea63d
python : 3.11.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 140 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.0.1
numpy : 1.24.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
...

@SkeptiCali SkeptiCali added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 1, 2023
@asishm
Copy link
Contributor

asishm commented May 2, 2023

Can you try with a sqlalchemy connectable as mentioned in the docs? Using a DBAPI2 connection for anything other than sqlite is not supported untested per the docs. Your code should also throw a UserWarning

@rhshadrach rhshadrach added Regression Functionality that used to work in a prior pandas version IO SQL to_sql, read_sql, read_sql_query labels May 2, 2023
@SkeptiCali
Copy link
Author

SkeptiCali commented May 2, 2023 via email

@asishm
Copy link
Contributor

asishm commented May 2, 2023

So this reproduces with a sqlite3 conn as well (which doesn't have a dictcursor built in - so using https://stackoverflow.com/questions/3300464/how-can-i-get-dict-from-sqlite-query)

reproducer:

import sqlite3
conn = sqlite3.connect(':memory:')

df = pd.DataFrame({'a': [1,2], 'b': [2,3]})
df.to_sql('foo', conn, index=False)

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

conn.row_factory = dict_factory

out = pd.read_sql('select * from foo', conn)

output:

In [27]: out
Out[27]:
   a  b
0  a  b
1  a  b

Issue stems from #50048
which tries to call

In [32]: lib.to_object_array_tuples([{'a': 1, 'b': 2}, {'a': 2, 'b': 3}])
Out[32]:
array([['a', 'b'],
       ['a', 'b']], dtype=object)

@DeaMariaLeon DeaMariaLeon removed the Needs Triage Issue that has not been reviewed by a pandas team member label May 3, 2023
@SkeptiCali
Copy link
Author

So based on your results should I just consider this deprecated behavior or a defect that needs to be fixed? Regardless, I have "work-arounds" so either way I am good.

@asishm-wk
Copy link

asishm-wk commented May 5, 2023

This is a regression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO SQL to_sql, read_sql, read_sql_query Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

5 participants