Skip to content

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

Closed
@sfo

Description

@sfo

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

Metadata

Metadata

Assignees

Labels

ApplyApply, Aggregate, Transform, MapError ReportingIncorrect or improved errors from pandas

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions