pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
42.62k stars 17.57k forks source link

BUG: pandas dataframe column definition or mapping does not cater for upper case values. #59045

Open c8-devops opened 2 weeks ago

c8-devops commented 2 weeks ago

Pandas version checks

Reproducible Example

import pandas as pd
import psycopg2
from sqlalchemy import create_engine

conn_string = "postgresql://postgres:password@localhost/database"
engine = create_engine(conn_string)

query = """
select 
    p.id as CustomerId
from table
"""

df = pd.read_sql(query, engine)

dtype_mappings = {
    'CustomerId': int
}

df = df.astype(dtype_mappings)

Issue Description

When running the code we get the following error

.venv\lib\site-packages\pandas\core\generic.py", line 6605, in astype raise KeyError( KeyError: "Only a column name can be used for the key in a dtype mappings argument. 'CustomerId' not found in columns."

If we change the column name to all lower case, the code works.

Expected Behavior

pandas should support both upper and lower case for the column names

Installed Versions

Name: pandas Version: 2.2.2
Aloqeely commented 2 weeks ago

Thanks for the report! Could you provide a reproducer without using read_sql? Manually creating a DataFrame with a column named CustomerId and then running astype(dtype_mappings) works fine for me.