Skip to content

BUG: pandas.read_sql_table() returns incorrect data types #52090

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
3 tasks done
Alper2022 opened this issue Mar 20, 2023 · 3 comments
Closed
3 tasks done

BUG: pandas.read_sql_table() returns incorrect data types #52090

Alper2022 opened this issue Mar 20, 2023 · 3 comments

Comments

@Alper2022
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

### Push this to Postgresql ###
create table product(
product_id varchar(255) primary key not null,
price varchar(255) not null,
gender varchar(255),
name varchar(255) not null,
recommendable boolean not null,
is_active boolean  not null,
fk_event_number integer references event(event_number) not null
);

### call pandas function ###

df_products_db = pd.read_sql_table("product",engine.connect())

print(df_products_db.dtypes) # returns 4 object data types, should be strings

Issue Description

When using pandas.read_sql_table() to request data from a (in my case) localhost postgres database, the data types returned are different then the data types specified in the postgresql enviorment

Expected Behavior

Data types should be strings (as they are varchar(255) in the database), but return as object types

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-35-generic
Version : #36~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Feb 17 15:17:25 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.24.1
pytz : 2022.1
dateutil : 2.8.1
setuptools : 59.6.0
pip : 22.0.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : 1.1
pymysql : None
psycopg2 : 2.9.5
jinja2 : 3.1.2
IPython : None
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : 2.0.6
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@Alper2022 Alper2022 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 20, 2023
@asishm
Copy link
Contributor

asishm commented Mar 20, 2023

object is the default data type for strings, so this is expected until the default changes.

@Alper2022
Copy link
Author

Where could i change this default?

@DeaMariaLeon DeaMariaLeon added Usage Question and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 21, 2023
@phofl
Copy link
Member

phofl commented Mar 21, 2023

You can use convert_dtypes or set the dtype_backend option starting with pandas 2.0

@phofl phofl closed this as completed Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants