BUG: resample unexpectedly branching on type of how callable · Issue #7929 · pandas-dev/pandas (original) (raw)

I was about to answer an SO question giving the standard "use lambda or functools.partial" to create a new function with bound arguments when to my surprise my example didn't work with the OP's code. After some experimentation, it turns out to be because we don't always get the same return type. For example:

df = pd.DataFrame(np.arange(5, dtype=np.int64), 
                  index=pd.DatetimeIndex(start='2014/01/01', periods=5, freq='d'))

def f(x,a=1):
    print(type(x))
    return int(isinstance(x, pd.DataFrame)) + 1000*a

After which:

>>> df.resample("M", how=f)
<class 'pandas.core.series.Series'>
               0
2014-01-31  1000
>>> df.resample("M", how=lambda x: f(x,5))
<class 'pandas.core.series.Series'>
               0
2014-01-31  5000
>>> df.resample("M", how=partial(f, a=9))
<class 'pandas.core.frame.DataFrame'>
               0
2014-01-31  9001

how shouldn't care about how the function was constructed, only whether it's callable, and we should be getting the same result (whether Series or DataFrame) fed into how in every case.