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

rolling().apply is inconsistent with groupby().apply #20068

Closed
Seung-hyeon opened this issue Mar 9, 2018 · 1 comment
Closed

rolling().apply is inconsistent with groupby().apply #20068

Seung-hyeon opened this issue Mar 9, 2018 · 1 comment
Labels
Duplicate Report Duplicate issue or pull request Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@Seung-hyeon
Copy link

Seung-hyeon commented Mar 9, 2018

This is the same issue with #5071, but still not solved. :(

func in GroupBy.apply(func, *args, **kwargs)[source] have DataFrame as an input, while
func in Rolling.apply(func, args=(), kwargs={}) have ndarray as an input.

Is this project still actively working to find solution?

Also, it would be better if it support parallel processing.

Code Sample

import pandas as pd

df = pd.DataFrame({'g':[1,1,1,1,2,2,2,3,3,3,3,3],
                   'A':[1,4,2,5,3,5,3,2,1,2,3,6],
                   'B':[1,5,2,3,1,5,7,3,5,2,8,4]})
print(df.groupby('g').apply(lambda x:str(type(x))))
print(df.rolling(3).apply(lambda x:str(type(x))))
g
1    <class 'pandas.core.frame.DataFrame'>
2    <class 'pandas.core.frame.DataFrame'>
3    <class 'pandas.core.frame.DataFrame'>
dtype: object

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-497-2f7f95b2a358> in <module>()
----> 1 df.rolling(3).apply(lambda x:str(type(x)))

~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py in apply(self, func, args, kwargs)
   1205     @Appender(_shared_docs['apply'])
   1206     def apply(self, func, args=(), kwargs={}):
-> 1207         return super(Rolling, self).apply(func, args=args, kwargs=kwargs)
   1208 
   1209     @Substitution(name='rolling')

~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py in apply(self, func, args, kwargs)
    854 
    855         return self._apply(f, func, args=args, kwargs=kwargs,
--> 856                            center=False)
    857 
    858     def sum(self, *args, **kwargs):

~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py in _apply(self, func, name, window, center, check_minp, how, **kwargs)
    797             with np.errstate(all='ignore'):
    798                 if values.ndim > 1:
--> 799                     result = np.apply_along_axis(calc, self.axis, values)
    800                 else:
    801                     result = calc(values)

~/anaconda3/lib/python3.6/site-packages/numpy/lib/shape_base.py in apply_along_axis(func1d, axis, arr, *args, **kwargs)
    130     except StopIteration:
    131         raise ValueError('Cannot apply_along_axis when any iteration dimensions are 0')
--> 132     res = asanyarray(func1d(inarr_view[ind0], *args, **kwargs))
    133 
    134     # build a buffer for storing evaluations of func1d.

~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py in calc(x)
    793                 def calc(x):
    794                     return func(x, window, min_periods=self.min_periods,
--> 795                                 closed=self.closed)
    796 
    797             with np.errstate(all='ignore'):

~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py in f(arg, window, min_periods, closed)
    851             minp = _use_window(min_periods, window)
    852             return _window.roll_generic(arg, window, minp, indexi, closed,
--> 853                                         offset, func, args, kwargs)
    854 
    855         return self._apply(f, func, args=args, kwargs=kwargs,

pandas/_libs/window.pyx in pandas._libs.window.roll_generic (pandas/_libs/window.c:36065)()

TypeError: must be real number, not str
@jreback
Copy link
Contributor

jreback commented Mar 9, 2018

duplicate of #5071 opening a new issue is not helpful

@jreback jreback closed this as completed Mar 9, 2018
@jreback jreback added Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode Duplicate Report Duplicate issue or pull request labels Mar 9, 2018
@jreback jreback added this to the No action milestone Mar 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants