Skip to content
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

BUG/API: Inconsistent index result for groupby().rolling(on=...) between Series and DataFrame #43405

Open
3 tasks done
mroeschke opened this issue Sep 4, 2021 · 2 comments
Open
3 tasks done
Labels
API - Consistency Internal Consistency of API/Behavior Bug Groupby Regression Functionality that used to work in a prior pandas version Window rolling, ewma, expanding

Comments

@mroeschke
Copy link
Member

  • 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.

xref #43355

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "id": ["a", "a", "b", "b", "b"],
   ...:         "timestamp": pd.date_range("2021-9-1", periods=5, freq="H"),
   ...:         "y": range(5),
   ...:     }
   ...: )
   ...: df
Out[1]:
  id           timestamp  y
0  a 2021-09-01 00:00:00  0
1  a 2021-09-01 01:00:00  1
2  b 2021-09-01 02:00:00  2
3  b 2021-09-01 03:00:00  3
4  b 2021-09-01 04:00:00  4

In [2]: grp = df.groupby("id").rolling("1H", on="timestamp")
   ...: grp.count().index
Out[2]:
MultiIndex([('a', 0),
            ('a', 1),
            ('b', 2),
            ('b', 3),
            ('b', 4)],
           names=['id', None])

In [4]: grp["y"].count().index
Out[4]:
MultiIndex([('a', '2021-09-01 00:00:00'),
            ('a', '2021-09-01 01:00:00'),
            ('b', '2021-09-01 02:00:00'),
            ('b', '2021-09-01 03:00:00'),
            ('b', '2021-09-01 04:00:00')],
           names=['id', 'timestamp'])

According to the on documentation

For a DataFrame, a datetime-like column or Index level on which to calculate the rolling window, rather than the DataFrame’s index. Provided integer column is ignored and excluded from result since an integer index is not used to calculate the rolling window.

Though the docs specifically states this applies to a DataFrame, this inconsistency seems confusing to users and could be possible for a DataFrame and Series result to have the same index result

@mroeschke mroeschke added Bug Needs Triage Issue that has not been reviewed by a pandas team member API - Consistency Internal Consistency of API/Behavior Groupby MultiIndex Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member MultiIndex labels Sep 4, 2021
@simonjayhawkins simonjayhawkins changed the title BUG/API: Inconsistent index result for groupby().rolling(on=...) between Series and DataFrmae BUG/API: Inconsistent index result for groupby().rolling(on=...) between Series and DataFrame Sep 5, 2021
@simonjayhawkins
Copy link
Member

Thanks @mroeschke.

The indexes were the same on 1.1.5 so will mark as regression but grp["y"].count().index had the same index as grp.count().index in the code sample above. Is that the expected output?

@simonjayhawkins simonjayhawkins added the Regression Functionality that used to work in a prior pandas version label Sep 5, 2021
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Sep 5, 2021
@mroeschke
Copy link
Member Author

I'm not 100% sure since this is an interesting edge case. The on index behavior was only really documented for a DataFrame, and grp["y"] is equivalent of the input now being a Series.

From a user perspective, Out[4] would make the most sense in both cases for consistency sake IMO.

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Bug Groupby Regression Functionality that used to work in a prior pandas version Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

2 participants