Skip to content

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

Closed
@rennerocha

Description

@rennerocha
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions