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: DataFrame/Series.transform with list and non-list dict values #40018

Closed
rhshadrach opened this issue Feb 24, 2021 · 3 comments · Fixed by #40090
Closed

BUG: DataFrame/Series.transform with list and non-list dict values #40018

rhshadrach opened this issue Feb 24, 2021 · 3 comments · Fixed by #40090
Assignees
Labels
Apply Apply, Aggregate, Transform, Map Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@rhshadrach
Copy link
Member

df = pd.DataFrame({'a': [1, 2], 'b': [1, 4], 'c': [1, 4]})
print(df.transform({'b': ['sqrt', 'abs'], 'c': 'sqrt'}))
print(df['a'].transform({'b': ['sqrt', 'abs'], 'c': 'sqrt'}))

produces

     b        c
  sqrt abs    c
0  1.0   1  1.0
1  2.0   4  2.0
          b             c
       sqrt abs         a
0  1.000000   1  1.000000
1  1.414214   2  1.414214

In each case the last column label on the 2nd level should be "sqrt" and not "a" or "c".

@rhshadrach rhshadrach added Bug Apply Apply, Aggregate, Transform, Map labels Feb 24, 2021
@rhshadrach rhshadrach self-assigned this Feb 24, 2021
@rhshadrach rhshadrach changed the title BUG: DataFrame/Series.transform with mix of list and non-list dictionary values BUG: DataFrame/Series.transform with list and non-list dict values Feb 24, 2021
@smohr003
Copy link

In which version are you getting this? I am running them on pandas==1.1.5 and the first case is producing the correct output.
The second case errors out as

`---------------------------------------------------------------------------
SpecificationError Traceback (most recent call last)
in
----> 1 print(df['a'].transform({'b': ['sqrt', 'abs'], 'c': 'sqrt'}))

/opt/conda/lib/python3.8/site-packages/pandas/core/series.py in transform(self, func, axis, *args, **kwargs)
4080 # Validate the axis parameter
4081 self._get_axis_number(axis)
-> 4082 return super().transform(func, *args, **kwargs)
4083
4084 def apply(self, func, convert_dtype=True, args=(), **kwds):

/opt/conda/lib/python3.8/site-packages/pandas/core/generic.py in transform(self, func, *args, **kwargs)
10674 2 1.414214 7.389056
10675 """

10676 result = self.agg(func, *args, **kwargs)
10677 if is_scalar(result) or len(result) != len(self):
10678 raise ValueError("transforms cannot produce aggregated results")

/opt/conda/lib/python3.8/site-packages/pandas/core/series.py in aggregate(self, func, axis, *args, **kwargs)
4047 func = dict(kwargs.items())
4048
-> 4049 result, how = self._aggregate(func, *args, **kwargs)
4050 if result is None:
4051

/opt/conda/lib/python3.8/site-packages/pandas/core/base.py in _aggregate(self, arg, *args, **kwargs)
338 raise SpecificationError("nested renamer is not supported")
339 elif isinstance(obj, ABCSeries):
--> 340 raise SpecificationError("nested renamer is not supported")
341 elif isinstance(obj, ABCDataFrame) and k not in obj.columns:
342 raise KeyError(f"Column '{k}' does not exist!")

SpecificationError: nested renamer is not supported
`

@rhshadrach
Copy link
Member Author

Thanks @smohr003! I've verified your reported behavior on 1.1.x and that the code matches my reported behavior on 1.2.x (and master). The first output I would classify as a regression, but the 2nd behavior (using on a Series) is the correct one. That is, passing a dictionary to Series.transform should not raise.

@rhshadrach rhshadrach added Regression Functionality that used to work in a prior pandas version and removed Bug labels Feb 26, 2021
@rhshadrach rhshadrach added this to the 1.2.3 milestone Feb 26, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Feb 27, 2021
@simonjayhawkins
Copy link
Member

from #40090 (comment)

As identified in #40018, this is a regression from 1.1 -> 1.2 (almost certainly from one of my PRs, but I haven't checked which).

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, Map 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