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: ValueError on ".transform" method applied to an empty DataFrame #39636

Closed
2 of 3 tasks
rennerocha opened this issue Feb 7, 2021 · 4 comments · Fixed by #39639
Closed
2 of 3 tasks

BUG: ValueError on ".transform" method applied to an empty DataFrame #39636

rennerocha opened this issue Feb 7, 2021 · 4 comments · Fixed by #39639
Labels
Apply Apply, Aggregate, Transform Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@rennerocha
Copy link

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

Output on version 1.1.5:

In [5]: import pandas as pd
   ...: df = pd.DataFrame([], columns=["id", "field"])
   ...: df["id"].transform(lambda x: x + 10)
Out[5]: Series([], Name: id, dtype: object)

Output on version 1.2.x:

In [4]: import pandas as pd
   ...: df = pd.DataFrame([], columns=["id", "field"])
   ...: df["id"].transform(lambda x: x + 10)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-d1e6cad57091> in <module>
----> 1 df["id"].transform(lambda x: x + 10)

~/.pyenv/versions/3.9.1/envs/odds-data-3.9.1/lib/python3.9/site-packages/pandas/core/series.py in transform(self, func, axis, *args, **kwargs)
   3975         self, func: AggFuncType, axis: Axis = 0, *args, **kwargs
   3976     ) -> FrameOrSeriesUnion:
-> 3977         return transform(self, func, axis, *args, **kwargs)
   3978 
   3979     def apply(self, func, convert_dtype=True, args=(), **kwds):

~/.pyenv/versions/3.9.1/envs/odds-data-3.9.1/lib/python3.9/site-packages/pandas/core/aggregation.py in transform(obj, func, axis, *args, **kwargs)
    458     # when the dtype is not appropriate
    459     if isinstance(result, (ABCSeries, ABCDataFrame)) and result.empty:
--> 460         raise ValueError("Transform function failed")
    461     if not isinstance(result, (ABCSeries, ABCDataFrame)) or not result.index.equals(
    462         obj.index

ValueError: Transform function failed

Problem description

Applying .transform on an empty DataFrame raises a ValueError on version 1.2.x. This is a change on the behavior of 1.1.5 version that returns the same empty DataFrame (as .apply is still doing).

The change that added this error apparently is related to this commit 7b6ab94

Expected Output

In [5]: import pandas as pd
   ...: df = pd.DataFrame([], columns=["id", "field"])
   ...: df["id"].transform(lambda x: x + 10)
Out[5]: Series([], Name: id, dtype: object)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 9d598a5
python : 3.9.1.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-65-generic
Version : #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.1
numpy : 1.20.0
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 49.2.1
Cython : None
pytest : 6.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.20.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.23
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@rennerocha rennerocha added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 7, 2021
@jorisvandenbossche jorisvandenbossche added Apply Apply, Aggregate, Transform Regression Functionality that used to work in a prior pandas version and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 7, 2021
@jorisvandenbossche jorisvandenbossche added this to the 1.2.2 milestone Feb 7, 2021
@jorisvandenbossche
Copy link
Member

Can confirm this is a regression in pandas 1.2.x compared to 1.1

@jorisvandenbossche
Copy link
Member

cc @rhshadrach the line checking if result.empty that raise the error in the above case was introduced in #35964 (but didn't check if that PR actually caused the regression)

@simonjayhawkins
Copy link
Member

but didn't check if that PR actually caused the regression

I'll check that shortly.

@simonjayhawkins
Copy link
Member

but didn't check if that PR actually caused the regression

I'll check that shortly.

can confirm

first bad commit: [ab5b38d] BUG/CLN: Decouple Series/DataFrame.transform (#35964)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants