Skip to content

groupby in combination with rolling provides unintuitve errors #26462

Closed
@bgruening

Description

@bgruening

I think the minimal example here is


In [28]: df = pd.DataFrame({"A": range(12)})

In [29]: df.rolling(3, win_type='gaussian').mean()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-82bac00d4424> in <module>
----> 1 df.rolling(3, win_type='gaussian').mean()

~/sandbox/pandas/pandas/core/window.py in mean(self, *args, **kwargs)
    827     def mean(self, *args, **kwargs):
    828         nv.validate_window_func("mean", args, kwargs)
--> 829         return self._apply_window(mean=True, **kwargs)
    830
    831

~/sandbox/pandas/pandas/core/window.py in _apply_window(self, mean, **kwargs)
    711
    712         """
--> 713         window = self._prep_window(**kwargs)
    714         center = self.center
    715

~/sandbox/pandas/pandas/core/window.py in _prep_window(self, **kwargs)
    693                 return all_args
    694
--> 695             win_type = _validate_win_type(self.win_type, kwargs)
    696             # GH #15662. `False` makes symmetric window, rather than periodic.
    697             return sig.get_window(win_type, window, False).astype(float)

~/sandbox/pandas/pandas/core/window.py in _validate_win_type(win_type, kwargs)
    674
    675                 if win_type in arg_map:
--> 676                     win_args = _pop_args(win_type, arg_map[win_type], kwargs)
    677                     if win_type == "exponential":
    678                         # exponential window requires the first arg (center)

~/sandbox/pandas/pandas/core/window.py in _pop_args(win_type, arg_names, kwargs)
    689                 for n in arg_names:
    690                     if n not in kwargs:
--> 691                         raise ValueError(msg % n)
    692                     all_args.append(kwargs.pop(n))
    693                 return all_args

ValueError: gaussian window requires std


Can you confirm @bgruening?

Apparently, passing std is required.

In [40]: df.rolling(3, win_type='gaussian').mean(std=1)
Out[40]:
       A
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
5    4.0
6    5.0
7    6.0
8    7.0
9    8.0
10   9.0
11  10.0

An example in the docs, and / or a better error message would be nice.

_shared_docs["mean"] = dedent(

original example below


Code Sample, a copy-pastable example if possible

import pandas as pd

iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
# this one works, but should not imho
iris['gaussian'] = iris.groupby('species')['petal_width'].rolling(3, win_type='gaussian', center=False).mean().reset_index(drop=True)

# this one raises an `ValueError: gaussian window requires std`
iris['gaussian'] = iris['petal_width'].rolling(3, win_type='gaussian', center=False).mean().reset_index(drop=True)

Problem description

According to https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html the win_type='gaussian' needs a std. The above example without the groupby errors if no std is given and this is the expected behaviour. However, the first example with the groupby does not error. I'm not even sure what happens in the first case.

Expected Output

The groupby example should give an error.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Linux
OS-release: 5.0.0-15-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: de_DE.UTF-8

pandas: 0.24.2
pytest: None
pip: 19.1
setuptools: 41.0.1
Cython: None
numpy: 1.16.3
scipy: 1.2.1
pyarrow: None
xarray: None
IPython: 7.5.0
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions