Skip to content

BUG: regression in DataFrame.transform('func') #19760

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

From a failing example in the doc build (http://pandas-docs.github.io/pandas-docs-travis/basics.html#transform-api):

tsdf = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'],
                    index=pd.date_range('1/1/2000', periods=10))
tsdf.iloc[3:7] = np.nan

Using a numpy func directly is fine:

In [43]: tsdf.transform(np.abs)
Out[43]: 
                   A         B         C
2000-01-01  1.038786  0.380848  1.190139
2000-01-02  2.201640  0.004028  0.704541
2000-01-03  1.803344  0.774565  0.034836
2000-01-04       NaN       NaN       NaN
2000-01-05       NaN       NaN       NaN
2000-01-06       NaN       NaN       NaN
2000-01-07       NaN       NaN       NaN
2000-01-08  0.351827  0.265785  1.424491
2000-01-09  0.962042  1.291740  1.107973
2000-01-10  0.366717  0.318791  0.117215

But specifying the function with a string now breaks:

In [44]: tsdf.transform('abs')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-44-405a88736ee3> in <module>()
----> 1 tsdf.transform('abs')

/home/joris/scipy/pandas/pandas/core/generic.py in transform(self, func, *args, **kwargs)
   7694         @Appender(_shared_docs['transform'] % _shared_doc_kwargs)
   7695         def transform(self, func, *args, **kwargs):
-> 7696             result = self.agg(func, *args, **kwargs)
   7697             if is_scalar(result) or len(result) != len(self):
   7698                 raise ValueError("transforms cannot produce "

/home/joris/scipy/pandas/pandas/core/frame.py in aggregate(self, func, axis, *args, **kwargs)
   4830                 pass
   4831         if result is None:
-> 4832             return self.apply(func, axis=axis, args=args, **kwargs)
   4833         return result
   4834 

/home/joris/scipy/pandas/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
   5020                          args=args,
   5021                          kwds=kwds)
-> 5022         return op.get_result()
   5023 
   5024     def applymap(self, func):

/home/joris/scipy/pandas/pandas/core/apply.py in get_result(self)
    309                                       *self.args, **self.kwds)
    310 
--> 311         return super(FrameRowApply, self).get_result()
    312 
    313     def apply_broadcast(self):

/home/joris/scipy/pandas/pandas/core/apply.py in get_result(self)
    113         if isinstance(self.f, compat.string_types):
    114             self.kwds['axis'] = self.axis
--> 115             return getattr(self.obj, self.f)(*self.args, **self.kwds)
    116 
    117         # ufunc

TypeError: abs() got an unexpected keyword argument 'axis'

This is on master, while it was working in 0.22 (tested with the same version of numpy, 1.13)

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