Skip to content

QST: how to use groupby.rolling correctly? Please help. Not sure what I am doing wrong #41078

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 done
terrygryffindor opened this issue Apr 21, 2021 · 2 comments
Closed
2 tasks done
Labels
Needs Triage Issue that has not been reviewed by a pandas team member Usage Question

Comments

@terrygryffindor
Copy link

terrygryffindor commented Apr 21, 2021

  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.


Question about pandas

Hi there, my understanding of the df.groupby.rolling for a multi index dataframe should preserve the level-1 index, but I can't seem to get that. I am running Pandas 1.2.4. Hope that somebody can provide some insight of what is happening

x = range(0, 6)
id = ['a', 'a', 'a', 'b', 'b', 'b']
temp = pd.DataFrame(zip(id, x), columns = ['id', 'x'])
temp['sequence']=[0,1,2,0,1,2]
temp=temp.set_index(['id','sequence'])
print(temp)

             x
id sequence   
a  0         0
   1         1
   2         2
b  0         3
   1         4
   2         5

temp.groupby(level=0,group_keys=True).rolling(2, min_periods=1).min()

      x
id     
a   0.0
a   0.0
a   1.0
b   3.0
b   3.0
b   4.0

base don the online guides, I would expect the level 1 index to be preserved...but I can't understand what is currently happening at this case? or what is the correct way of doing it.

Hope that somebody can provide some insights with regards to this problem

EDIT: Edited as text for readability, apologies for posting images earlier, forgot about it while posting it just now

@terrygryffindor terrygryffindor added Needs Triage Issue that has not been reviewed by a pandas team member Usage Question labels Apr 21, 2021
@terrygryffindor terrygryffindor changed the title QST: QST: how to use groupby.rolling correctly? Please help. Not sure what I am doing wrong Apr 21, 2021
@phofl
Copy link
Member

phofl commented Apr 21, 2021

Could you provide your example as "text" not as a screenshot?

@mroeschke Is this documented somewhere? Iirc this changed recently/is a current discussion topic how this should behave

@mroeschke
Copy link
Member

Yes this was fixed recently by #40701 and will be fixed in the 1.3 release. Unfortunately this has been broken since the 1.1.3 release.

In [1]: x = range(0, 6)
   ...: id = ['a', 'a', 'a', 'b', 'b', 'b']
   ...: temp = pd.DataFrame(zip(id, x), columns = ['id', 'x'])
   ...: temp['sequence']=[0,1,2,0,1,2]
   ...: temp=temp.set_index(['id','sequence'])
   ...: print(temp)
             x
id sequence
a  0         0
   1         1
   2         2
b  0         3
   1         4
   2         5

In [2]: temp.groupby(level=0,group_keys=True).rolling(2, min_periods=1).min()
Out[2]:
                  x
id id sequence
a  a  0         0.0
      1         0.0
      2         1.0
b  b  0         3.0
      1         3.0
      2         4.0

In [3]: pd.__version__
Out[3]: '1.3.0.dev0+1392.g11545d5023'

Thanks for the report. Going to close as fixed in a future release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Triage Issue that has not been reviewed by a pandas team member Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants