Skip to content

BUG: 1.1.0 breaks custom indexer support in groupby().rolling() #35557

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
2 of 3 tasks
WhistleWhileYouWork opened this issue Aug 4, 2020 · 4 comments · Fixed by #35647
Closed
2 of 3 tasks

BUG: 1.1.0 breaks custom indexer support in groupby().rolling() #35557

WhistleWhileYouWork opened this issue Aug 4, 2020 · 4 comments · Fixed by #35647
Assignees
Labels
Milestone

Comments

@WhistleWhileYouWork
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

class SimpleIndexer(pd.api.indexers.BaseIndexer):
    ''' Custom `Indexer` duplicating basic fixed length windowing '''
    def get_window_bounds(self, num_values=0, min_periods=None, center=None, closed=None):
        min_periods = self.window_size if min_periods is None else 0
        end = np.arange(num_values, dtype=np.int64) + 1
        start = end.copy() - self.window_size
        #---- Clip to `min_periods`
        start[start < 0] = min_periods
        return (start, end)

x = pd.DataFrame({'a': [1.0,2.0,3.0,4.0,5.0] * 3}, index=[0]*5+[1]*5+[2]*5)
x

Out: 
     a
0  1.0
0  2.0
0  3.0
0  4.0
0  5.0
1  1.0
1  2.0
1  3.0
1  4.0
1  5.0
2  1.0
2  2.0
2  3.0
2  4.0
2  5.0

x.groupby(x.index).rolling(SimpleIndexer(window_size=3), min_periods=1).sum()

Out: 
       a
0 0  1.0
  0  2.0
  0  3.0
  0  4.0
  0  5.0
1 1  1.0
  1  2.0
  1  3.0
  1  4.0
  1  5.0
2 2  1.0
  2  2.0
  2  3.0
  2  4.0
  2  5.0

Problem description

groupby().rolling() does not use custom indexer supplied in window= likely as a result of #34052 where a GroupbyRollingIndexer is used instead.

The output data is always the same as the input regardless of the windowing function.

1.0.5 does not have this behavior.

Expected Output

x.groupby(x.index).rolling(window=3, min_periods=1).sum()

Out: 
        a
0 0   1.0
  0   3.0
  0   6.0
  0   9.0
  0  12.0
1 1   1.0
  1   3.0
  1   6.0
  1   9.0
  1  12.0
2 2   1.0
  2   3.0
  2   6.0
  2   9.0
  2  12.0

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : d9fff27 python : 3.8.2.final.0 python-bits : 64 OS : Linux OS-release : 5.3.0-59-generic Version : #53~18.04.1-Ubuntu SMP Thu Jun 4 14:58:26 UTC 2020 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.1.0
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2
setuptools : 49.2.1.post20200802
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.14.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.17.0
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.50.1

@WhistleWhileYouWork WhistleWhileYouWork added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 4, 2020
@jorisvandenbossche
Copy link
Member

cc @mroeschke

@goodwanghan
Copy link

goodwanghan commented Aug 5, 2020

I had the same issue on groupby. To reproduce the issue:

import pandas as pd
from pandas.core.window.indexers import BaseIndexer

df = pd.DataFrame([[0, 1],[1, 20]], columns=["a", "b"])
# this should raise NotImplementedError (works in 1.0.2)
df.groupby(["a"]).rolling(window=BaseIndexer(),min_periods=1).min()

and count is an exception, it works with grouby rolling, this will raise:

df.groupby(["a"]).rolling(window=BaseIndexer(),min_periods=0).count()

@TomAugspurger
Copy link
Contributor

FYI @goodwanghan, comments like "Please fix" come off as rude.

@goodwanghan
Copy link

FYI @goodwanghan, comments like "Please fix" come off as rude.

Thank you, and I feel very sorry about that. I have changed.

@mroeschke mroeschke self-assigned this Aug 6, 2020
@simonjayhawkins simonjayhawkins added this to the 1.1.1 milestone Aug 6, 2020
@bashtage bashtage removed the Needs Triage Issue that has not been reviewed by a pandas team member label Aug 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants