Skip to content

BUG: groupby.rolling: Originial index is not being preserved when using date_part of DatetimeIndex and as_index key word seems to have no effect #39433

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 tasks
derHeinzer opened this issue Jan 27, 2021 · 3 comments · Fixed by #40789
Labels
Bug Groupby Window rolling, ewma, expanding
Milestone

Comments

@derHeinzer
Copy link

derHeinzer commented Jan 27, 2021

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

  • [ x] 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

import pandas as pd

data = [
    ["A", "2018-01-01", 100],
    ["A", "2018-01-02", 200],
    ["B", "2018-01-01", 150],
    ["B", "2018-01-02", 250],
]
df = pd.DataFrame(data, columns=["id", "date", "num"])
df["date"] = pd.to_datetime(df["date"])
df.set_index(["date"], inplace=True)


df_res1 = df.groupby([df.id, df.index.weekday]).rolling(window=2, min_periods=1).mean()
df_res2 = df.groupby([df.id, df.index.weekday], as_index=False).rolling(window=2, min_periods=1).mean()
df_res3 = df.groupby([df.id]).rolling(window=2, min_periods=1).mean()
df_res4 = df.groupby([df.id], as_index=False).rolling(window=2, min_periods=1).mean()
pd.show_versions()
print(df_res1.head())
print(df_res1.index.names)
print(df_res2.index.names)
print(df_res3.index.names)
print(df_res4.index.names)


if __name__ == '__main__':
    ...

Problem description

Comparing pandas 1.0.1 and 1.2.1 yields:

  1. When using a date part like df.index.weekday to group by and using some rolling window function like .mean() the original datetime index is not part of the resulting frames index anymore.
  2. The group by keyword argumen as_index seems to have no effect in that situation anymore

Expected Output

['id', 'date', 'date']
[None, 'date']
['id', 'date']
[None, 'date']

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 9d598a5
python : 3.8.4.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-45-generic
Version : #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.2.1
numpy : 1.19.1

@derHeinzer derHeinzer added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 27, 2021
@derHeinzer
Copy link
Author

as one might have expected, same applies for expanding functions.

@mroeschke mroeschke added Groupby Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 28, 2021
@ravneetg
Copy link

ravneetg commented Feb 5, 2021

I had a similar problem with rolling mean with groupby. Should i log this as a separate bug?
show_versions.txt

import pandas as pd
df_bug = pd.DataFrame([['2018-10-30', 'key2', 2.02],
       ['2018-10-31', 'key2', 1.68],
       ['2018-11-01', 'key2', 2.47],
       ['2018-11-02', 'key2', 1.0],
       ['2018-11-11', 'key2', 1.45],
       ['2021-01-07', 'key2', 1.86],
       ['2021-01-08', 'key2', 1.86],
       ['2021-01-09', 'key2', 1.64],
       ['2021-01-10', 'key2', 1.59],
       ['2021-01-11', 'key2', 1.43]], columns=["key1", "key2", "raw_value"])

print(df_bug.shape)

# below line returns NaNs
df_bug.groupby("key1")["raw_value"].rolling(3,3).mean()

# below line returns raw value as is
df_bug.groupby("key1")["raw_value"].rolling(3,1).mean()

@abhineetgupta
Copy link

Encountered the same bug, and wasn't able to find a solution in pandas version 1.2.2.

@jreback jreback added this to the 1.3 milestone Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants