Skip to content

Apply not available for all windows type #28369

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

Closed
MenelikV opened this issue Sep 10, 2019 · 5 comments
Closed

Apply not available for all windows type #28369

MenelikV opened this issue Sep 10, 2019 · 5 comments
Labels
Apply Apply, Aggregate, Transform, Map Bug Window rolling, ewma, expanding

Comments

@MenelikV
Copy link

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': range(1000)})
df.rolling(5, center=True, win_type='triang').apply(lambda x: np.nanstd(x), raw=False)

Problem description

The above produces an error, stating that the Window class produces by the rolling function does not have an apply attribute.

If I remove the win_type attribute, the computation goes fine and returns a result:

	A
0	NaN
1	NaN
2	1.414214
3	1.414214
4	1.414214
...	...
995	1.414214
996	1.414214
997	1.414214
998	NaN
999	NaN

Expected Output

I expect the apply attribute to be available to any win_type (I did not see any restrictions in the documentation of pd.DataFrame.rolling (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html), or the documentation of pd.DataFrame.rolling.apply (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.window.Rolling.apply.html))

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Windows
OS-release : 7
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : None.None

pandas : 0.25.1
numpy : 1.16.2
pytz : 2018.9
dateutil : 2.8.0
pip : 19.1.1
setuptools : 40.8.0
Cython : 0.29.6
pytest : 4.3.1
hypothesis : None
sphinx : 1.8.5
blosc : None
feather : None
xlsxwriter : 1.1.5
lxml.etree : 4.3.2
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10
IPython : 7.4.0
pandas_datareader: None
bs4 : 4.7.1
bottleneck : 1.2.1
fastparquet : None
gcsfs : None
lxml.etree : 4.3.2
matplotlib : 3.0.3
numexpr : 2.6.9
odfpy : None
openpyxl : 2.6.1
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.2.1
sqlalchemy : 1.3.1
tables : 3.5.1
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.1.5

@WillAyd
Copy link
Member

WillAyd commented Sep 10, 2019

Thanks for the report. If you'd like to investigate and try a PR would certainly be welcome. Relevant code is in pandas.core.window.rolling.py

@WillAyd WillAyd added the Window rolling, ewma, expanding label Sep 10, 2019
@WillAyd WillAyd added this to the Contributions Welcome milestone Sep 10, 2019
@WillAyd WillAyd added the Bug label Sep 10, 2019
@vishnuyar
Copy link

Interested to work on this issue, any helpful pointers to begin with

@rohitsanj
Copy link
Contributor

rohitsanj commented Oct 4, 2019

@WillAyd I'd like to be assigned this issue. According to the following code, when win_type is passed to the rolling function, it returns an object of Window class. The Window class, as opposed to the Rolling class does not have the following member methods: ['std', 'var', 'cov', 'kurt', 'quantile', '_validate_freq', 'skew', 'max', '_validate_monotonic', 'apply', '_agg_doc', 'corr', 'count', 'median', 'min'] - which includes apply

I tried removing the condition so as to always return a Rolling object, but I encountered issues in other dependent functions.

pandas/pandas/core/generic.py

Lines 10725 to 10758 in 88a6ee1

def rolling(
self,
window,
min_periods=None,
center=False,
win_type=None,
on=None,
axis=0,
closed=None,
):
axis = self._get_axis_number(axis)
if win_type is not None:
return Window(
self,
window=window,
min_periods=min_periods,
center=center,
win_type=win_type,
on=on,
axis=axis,
closed=closed,
)
return Rolling(
self,
window=window,
min_periods=min_periods,
center=center,
win_type=win_type,
on=on,
axis=axis,
closed=closed,
)

@WillAyd
Copy link
Member

WillAyd commented Oct 4, 2019 via email

@jbrockmendel jbrockmendel added the Apply Apply, Aggregate, Transform, Map label Oct 23, 2019
@mroeschke
Copy link
Member

Thanks for the report. This issue is a duplicate of #19286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

6 participants