-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: RollingGroupby ignored as_index=False #40789
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
Changes from 4 commits
90498f7
76f9202
37e53f3
0626125
9a99b5b
d5f272f
3fa8101
f70d381
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -732,6 +732,42 @@ def test_groupby_level(self): | |
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
def test_as_index_false(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add the multi-key groupby here as well (parameterize if you can)
e.g. 2 & 4 (we likley have 1 & 3 covered, but wouldn't object to those included as well) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm all these tested (except the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if they are elsewhere then don't (just the cases that is covering in this PR are fine). parameterize if you can. |
||
# GH 39433 | ||
data = [ | ||
["A", "2018-01-01", 100.0], | ||
["A", "2018-01-02", 200.0], | ||
["B", "2018-01-01", 150.0], | ||
["B", "2018-01-02", 250.0], | ||
] | ||
df = DataFrame(data, columns=["id", "date", "num"]) | ||
df["date"] = to_datetime(df["date"]) | ||
df = df.set_index(["date"]) | ||
|
||
result = ( | ||
df.groupby([df.id], as_index=False).rolling(window=2, min_periods=1).mean() | ||
) | ||
expected = DataFrame( | ||
{"id": ["A", "A", "B", "B"], "num": [100.0, 150.0, 150.0, 200.0]}, | ||
index=df.index, | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = ( | ||
df.groupby([df.id, df.index.weekday], as_index=False) | ||
.rolling(window=2, min_periods=1) | ||
.mean() | ||
) | ||
expected = DataFrame( | ||
{ | ||
"id": ["A", "A", "B", "B"], | ||
"date": [0, 1, 0, 1], | ||
"num": [100.0, 200.0, 150.0, 250.0], | ||
}, | ||
index=df.index, | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
class TestExpanding: | ||
def setup_method(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens here when the groupby is on an explicit list, e.g. in your test use
groupby(["A", "A", "B", "B"])
instead. What is groupby_keys in this case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the result
Not sure if the normal
groupby
has the expected result but appears thatgroupby.rolling
brings the list into the dataframe as a column