-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: value_counts returning incorrect dtype for string dtype #55627
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
Conversation
pandas/core/groupby/groupby.py
Outdated
@@ -2855,7 +2859,9 @@ def _value_counts( | |||
result_series.name = name | |||
result_series.index = index.set_names(range(len(columns))) | |||
result_frame = result_series.reset_index() | |||
result_frame.columns = columns + [name] | |||
result_frame.columns = Index( | |||
columns + [name], dtype=self.grouper.groupings[0].obj.columns.dtype |
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.
@rhshadrach is there a better way of getting the dtype of the original column 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.
self.obj.columns.dtype I think.
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.
That's a Series in SeriesGroupBy unfortunately
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.
looks good - I think the size change could use a test.
added a test |
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.
Thanks - just missing the GH#
], | ||
) | ||
def test_size_strings(dtype): | ||
df = DataFrame({"a": ["a", "a", "b"], "b": "a"}, dtype=dtype) |
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.
nit: GH#
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.
added
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.
lgtm
Thanks @phofl |
Sorry for the confusion, this should go into 2.1.2 (it#s only really relevant if you use the new option) |
@meeseeksdev backport 2.1.x |
…type for string dtype
…rect dtype for string dtype) (#55682) Backport PR #55627: BUG: value_counts returning incorrect dtype for string dtype Co-authored-by: Patrick Hoefler <[email protected]>
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.