-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: SeriesGroupBy.value_counts - index name missing in categorical columns #45625
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
Merged
mroeschke
merged 12 commits into
pandas-dev:main
from
NumberPiOso:index-value-count-cat-col
Feb 5, 2022
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
32cf991
BUG: SeriesGroupBy.value_counts index name missing
NumberPiOso 4ae936e
Merge branch 'main' into index-value-count-cat-col
NumberPiOso 8e8b37c
TST: Change test to correct categorical naming
NumberPiOso 337d220
Merge branch 'main' into index-value-count-cat-col
NumberPiOso 7692b36
REF: Refactor conditionals in value_counts()
NumberPiOso b26d0d6
Merge branch 'main' into index-value-count-cat-col
NumberPiOso d81d7b9
RFT: correct mistake introduced via RFT
NumberPiOso 9cdc5d8
Merge remote-tracking branch 'upstream/main' into index-value-count-c…
NumberPiOso 9d561b9
RFT: Change variable names and comment #38672
NumberPiOso 55622f3
Merge remote-tracking branch 'upstream/main' into index-value-count-c…
NumberPiOso 4bedca5
Merge remote-tracking branch 'upstream/main' into index-value-count-c…
NumberPiOso 49a375d
BUG: Update conditional to is None to consider series
NumberPiOso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -602,14 +602,18 @@ def value_counts( | |
ids, _, _ = self.grouper.group_info | ||
val = self.obj._values | ||
|
||
names = self.grouper.names + [self.obj.name] | ||
|
||
def apply_series_value_counts(): | ||
return self.apply( | ||
s = self.apply( | ||
Series.value_counts, | ||
normalize=normalize, | ||
sort=sort, | ||
ascending=ascending, | ||
bins=bins, | ||
) | ||
s.index.names = names | ||
return s | ||
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. nitpick: s -> ser |
||
|
||
if bins is not None: | ||
if not np.iterable(bins): | ||
|
@@ -683,7 +687,6 @@ def apply_series_value_counts(): | |
levels = [ping.group_index for ping in self.grouper.groupings] + [ | ||
lev # type: ignore[list-item] | ||
] | ||
names = self.grouper.names + [self.obj.name] | ||
rhshadrach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if dropna: | ||
mask = codes[-1] != -1 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could you take this opportunity to refactor and inline this logic like
Also will s always have a MultiIndex here?
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.
If I run the test code with the following change
I get these results
And I think the fact that we are inside a group by should always ensure that we are always using MultiIndex.
Running the following code calls a different method. Specifically,
pandas.core.algorithms.value_counts
.