-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Groupby.nth includes group key inconsistently #12839 #13316
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 2 commits
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 |
---|---|---|
|
@@ -457,6 +457,11 @@ def _selected_obj(self): | |
else: | ||
return self.obj[self._selection] | ||
|
||
def _reset_group_selection(self): | ||
if self._group_selection is not None: | ||
self._group_selection = None | ||
self._reset_cache('_selected_obj') | ||
|
||
def _set_selection_from_grouper(self): | ||
""" we may need create a selection if we have non-level groupers """ | ||
grp = self.grouper | ||
|
@@ -468,6 +473,7 @@ def _set_selection_from_grouper(self): | |
|
||
if len(groupers): | ||
self._group_selection = ax.difference(Index(groupers)).tolist() | ||
self._reset_cache('_selected_obj') | ||
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. I agree I think this is sufficient 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 I think maybe move 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. I am not sure, I think |
||
|
||
def _set_result_index_ordered(self, result): | ||
# set the result index on the passed values object and | ||
|
@@ -1402,6 +1408,7 @@ def head(self, n=5): | |
0 1 2 | ||
2 5 6 | ||
""" | ||
self._reset_group_selection() | ||
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. dont' create a new method, just call 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. Based on the documentation for 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. ok that is fine. just doc the method
|
||
mask = self._cumcount_array() < n | ||
return self._selected_obj[mask] | ||
|
||
|
@@ -1428,6 +1435,7 @@ def tail(self, n=5): | |
0 a 1 | ||
2 b 1 | ||
""" | ||
self._reset_group_selection() | ||
mask = self._cumcount_array(ascending=False) < n | ||
return self._selected_obj[mask] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,6 +354,31 @@ def test_nth_multi_index_as_expected(self): | |
names=['A', 'B'])) | ||
assert_frame_equal(result, expected) | ||
|
||
def test_group_selection_cache(self): | ||
# GH 12839 nth, head, and tail should return same result consistently | ||
df = DataFrame([[1, 2], [1, 4], [5, 6]], columns=['A', 'B']) | ||
expected = df.iloc[[0, 2]].set_index('A') | ||
|
||
g = df.groupby('A') | ||
g.head() | ||
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. check both |
||
result = g.nth(0) | ||
assert_frame_equal(result, expected) | ||
|
||
g = df.groupby('A') | ||
g.tail() | ||
result = g.nth(0) | ||
assert_frame_equal(result, expected) | ||
|
||
g = df.groupby('A') | ||
g.nth(0) | ||
result = g.head(n=2) | ||
assert_frame_equal(result, df) | ||
|
||
g = df.groupby('A') | ||
g.nth(0) | ||
result = g.tail(n=2) | ||
assert_frame_equal(result, df) | ||
|
||
def test_grouper_index_types(self): | ||
# related GH5375 | ||
# groupby misbehaving when using a Floatlike index | ||
|
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.
can you add a comment here (and to
_set_selection
) to what these are doing