-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: Clarify how groupby forms groups for mixed-value columns #57526
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
Comments
Thanks for the report, I think a line in the notes section of the docstring on
An example in the user guide and your suggested workaround could then be linked to in the last sentence. Would you be interested in submitting a PR? |
Thanks for the suggestion. I'd be happy to open a PR on this. Should I insert that here at current L97? pandas/pandas/core/shared_docs.py Lines 95 to 99 in bdc79c1
Should I add an example? I just have another follow-up question: When you say hash-based, I assume this means that it always uses I also just realized that the string workaround obviously has its own drawbacks, as it would now group the string |
I think this should go in the notes section which is below
No -
That's a good point - this makes me quite hesitant to mention the workaround. |
Pandas version checks
main
hereLocation of the documentation
https://pandas.pydata.org/docs/dev/reference/api/pandas.DataFrame.groupby.html#pandas.DataFrame.groupby
https://pandas.pydata.org/docs/dev/user_guide/groupby.html#
Documentation problem
In my broad view of things, groupby can work on any (combination of) hashables, but there can be some unintuitive situations that should be mentioned in the docs.
E.g., the following should and does work (work as in there is no exception, but the results are not necessarily as expected):
We can see that the value
1
andTrue
were grouped together. From a Python background, that is not unexpected, as1 == True
evaluates asTrue
, i.e.1
andTrue
are equal and thus in the same group.While this data layout may be questionable, it certainly happens in reality. I find myself often using (abusing?)
groupby()
on multiple cols to identify unique combinations of values, and in this case, I would not want1
to be considered equal toTrue
(or0
equal toFalse
and whichever other 'convenience' conversions Python does).Suggested fix for documentation
I think the docs could benefit from mentioning this pitfall. The suggested workaround that I see would be to first convert the columns to strings before the
groupby
:The text was updated successfully, but these errors were encountered: