Skip to content
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

BUG: Unexpected KeyError in transform() with dict-like func argument #58474

Closed
2 of 3 tasks
sfo opened this issue Apr 29, 2024 · 3 comments · Fixed by #58494
Closed
2 of 3 tasks

BUG: Unexpected KeyError in transform() with dict-like func argument #58474

sfo opened this issue Apr 29, 2024 · 3 comments · Fixed by #58494
Assignees
Labels
Apply Apply, Aggregate, Transform Error Reporting Incorrect or improved errors from pandas
Milestone

Comments

@sfo
Copy link

sfo commented Apr 29, 2024

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

pd.DataFrame({'foo': [2,4,6], 'bar': [1,2,3]}).transform(
    {
        'foo': lambda x: x+2,
        'bar': lambda x: x*2
    },
    axis="columns"
)

Issue Description

The code raises KeyError: "Column(s) ['bar', 'foo'] do not exist" even though the columns are present, evidently.

Expected Behavior

Transform the columns according to the dict-like argument of the tranform() function and return a new data frame, without raising an error.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.11.9.final.0
python-bits : 64
OS : Linux
OS-release : 6.8.7-200.fc39.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Wed Apr 17 19:35:11 UTC 2024
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.2
numpy : 1.24.2
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 67.1.0
pip : 24.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.10.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.11.2
bottleneck : None
dataframe-api-compat : None
fastparquet : 2023.2.0
fsspec : 2023.1.0
gcsfs : None
matplotlib : 3.6.3
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.1
pandas_gbq : None
pyarrow : 16.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.10.0
sqlalchemy : 2.0.24
tables : None
tabulate : None
xarray : 2023.2.0
xlrd : 2.0.1
zstandard : None
tzdata : 2024.1
qtpy : 2.3.0
pyqt5 : None

@sfo sfo added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 29, 2024
@Aloqeely
Copy link
Contributor

Aloqeely commented Apr 29, 2024

You can pass axis="index" to apply the function to each column (I'm not quite sure what that means) per the doc at https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.transform.html

But I will admit this is a little confusing, I don't know why the error message says Column(s) [...] do not exist, the error message should be improved.
And I also find the docs to be a little inconsistent when it comes to the axis parameter, some docs say axis="index" will compute row-wise, and some say apply on each column, the only issue related issue I could found is #43128

@rhshadrach
Copy link
Member

But I will admit this is a little confusing, I don't know why the error message says Column(s) [...] do not exist, the error message should be improved.

Agreed.

And I also find the docs to be a little inconsistent when it comes to the axis parameter, some docs say axis="index" will compute row-wise, and some say apply on each column, the only issue related issue I could found is #43128

Best to see the context, but I believe row-wise is meant as "down the rows", which is the same as applying to each column.

@rhshadrach rhshadrach added Docs Apply Apply, Aggregate, Transform and removed Needs Triage Issue that has not been reviewed by a pandas team member Bug labels Apr 29, 2024
@luke396
Copy link
Contributor

luke396 commented Apr 30, 2024

take

@rhshadrach rhshadrach added this to the 3.0 milestone May 10, 2024
@rhshadrach rhshadrach added Error Reporting Incorrect or improved errors from pandas and removed Docs labels May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants