-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
pandas.rolling.apply skip calling function if window contains any NaN #21489
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
Comments
The difference you are seeing is a result of Note the difference in behavior when passing this explicitly: >>> df.rolling(5, min_periods=4).apply(my_std, raw=False)
0 1 2
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN NaN 1.290994
4 1.707825 1.825742 1.581139
5 1.825742 1.707825 1.581139
6 1.707825 1.290994 1.290994
7 1.290994 1.581139 1.707825 See also the rolling documentation: I think this issue is worth leaving open if only to update the documentation of that keyword argument. There is an example that shows how it functions, but the description of that argument could use some clarification on what |
Thank you, WillAyd. You are right. It is the effect of |
If you are open to making the change yourself contributions like that are welcome and encouraged. Just be sure to check out the pandas contributor guide: https://pandas.pydata.org/pandas-docs/stable/contributing.html |
@jwchi, are you planning on making the changes to the documentation or can I take this up? |
Is this issue currently being worked on? If not, I'd like to take it up. |
@mattboggess : Go for it! Doesn't look like anyone is currently working on it. |
Code Sample, a copy-pastable example if possible
Problem description
I wonder if this is an intended/unintended behavior. Apparently when a
Rolling
object runs theapply
method, it skips calling the function completely if data in the window contains anynp.nan
.df
looks like this:Output of
df.rolling(5).apply(my_std, raw=False)
:I understand that in older versions, pandas calls numpy primitives to handle rolling windows, which leads to NaNs as numpy function propagates it. But here, the NaNs are not caused by
my_std
, because data in the first column are not even printed, i.e.my_std
is not even called for the first column as there is no single non-NaN window.Confusingly, the
Expanding
object would not do this.Expanding.apply
actually calls the function with the expanding window despite if there is anyNaN
or not.Expected Output
expected output of running:
df.rolling(5).apply(my_std, raw=False)
currently this is achieved by running:
df.expanding(5).apply(lambda x: x[-5:].std(), raw=False)
it's confusing that one has to use
expanding(5)
to do the job ofrolling(5)
whenNaN
is present in data. Performance-wise is probably not ideal either...Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 63 Stepping 0, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.23.0
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: None
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: