Skip to content

Rolling standard deviation fails when used with win_type #26597

Closed
@ghost

Description

Code Sample, a copy-pastable example if possible

import pandas as pd
df = pd.DataFrame({'a': range(6)})
df['a'].rolling(3, win_type='blackman').agg(['mean', 'std'])

Problem description

When calculating rolling aggregations with a window function, sometimes there is an error which is quite hard to understand. I think it might be that the std() aggregation is not compatible with certain window types, or something like that- but it's not clear from the error messages or documentation what is going wrong.

Here is the stack trace that I see when I run the code sample above:


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in 
      3 df = pd.DataFrame({'a': range(6)})
      4 
----> 5 print(df['a'].rolling(3, win_type='blackman').agg(['mean', 'std']))

~\AppData\Local\Continuum\anaconda3\envs\petropy\lib\site-packages\pandas\core\window.py in aggregate(self, arg, *args, **kwargs)
741 @Appender(_shared_docs['aggregate'])
742 def aggregate(self, arg, *args, **kwargs):
--> 743 result, how = self._aggregate(arg, *args, **kwargs)
744 if result is None:
745

~\AppData\Local\Continuum\anaconda3\envs\petropy\lib\site-packages\pandas\core\base.py in _aggregate(self, arg, *args, **kwargs)
557 return self._aggregate_multiple_funcs(arg,
558 _level=_level,
--> 559 _axis=_axis), None
560 else:
561 result = None

~\AppData\Local\Continuum\anaconda3\envs\petropy\lib\site-packages\pandas\core\base.py in _aggregate_multiple_funcs(self, arg, _level, _axis)
587 try:
588 colg = self._gotitem(obj.name, ndim=1, subset=obj)
--> 589 results.append(colg.aggregate(a))
590
591 # make sure we find a good name

~\AppData\Local\Continuum\anaconda3\envs\petropy\lib\site-packages\pandas\core\window.py in aggregate(self, arg, *args, **kwargs)
741 @Appender(_shared_docs['aggregate'])
742 def aggregate(self, arg, *args, **kwargs):
--> 743 result, how = self._aggregate(arg, *args, **kwargs)
744 if result is None:
745

~\AppData\Local\Continuum\anaconda3\envs\petropy\lib\site-packages\pandas\core\base.py in _aggregate(self, arg, *args, **kwargs)
354 if isinstance(arg, compat.string_types):
355 return self._try_aggregate_string_function(arg, *args,
--> 356 **kwargs), None
357
358 if isinstance(arg, dict):

~\AppData\Local\Continuum\anaconda3\envs\petropy\lib\site-packages\pandas\core\base.py in _try_aggregate_string_function(self, arg, *args, **kwargs)
321 f = getattr(np, arg, None)
322 if f is not None:
--> 323 return f(self, *args, **kwargs)
324
325 raise ValueError("{arg} is an unknown string function".format(arg=arg))

~\AppData\Local\Continuum\anaconda3\envs\petropy\lib\site-packages\numpy\core\fromnumeric.py in std(a, axis, dtype, out, ddof, keepdims)
3240
3241 return _methods._std(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
-> 3242 **kwargs)
3243
3244

~\AppData\Local\Continuum\anaconda3\envs\petropy\lib\site-packages\numpy\core_methods.py in _std(a, axis, dtype, out, ddof, keepdims)
138 def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
139 ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
--> 140 keepdims=keepdims)
141
142 if isinstance(ret, mu.ndarray):

~\AppData\Local\Continuum\anaconda3\envs\petropy\lib\site-packages\numpy\core_methods.py in _var(a, axis, dtype, out, ddof, keepdims)
110 arrmean, rcount, out=arrmean, casting='unsafe', subok=False)
111 else:
--> 112 arrmean = arrmean.dtype.type(arrmean / rcount)
113
114 # Compute sum of squared deviations from mean

~\AppData\Local\Continuum\anaconda3\envs\petropy\lib\site-packages\pandas\core\window.py in getattr(self, attr)
148
149 raise AttributeError("%r object has no attribute %r" %
--> 150 (type(self).name, attr))
151
152 def _dir_additions(self):

AttributeError: 'Window' object has no attribute 'dtype'

Expected Output

When I run the code above without the win_type paramater, everything works fine:

import pandas as pd
df = pd.DataFrame({'a': range(6)})
df['a'].rolling(3, win_type=None).agg(['mean', 'std'])

Result:


   mean  std
0   NaN  NaN
1   NaN  NaN
2   1.0  1.0
3   2.0  1.0
4   3.0  1.0
5   4.0  1.0

Thanks in advance for any tips!

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Windowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions