Skip to content

Doc/categorical okexcept #47980

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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
DOC: Clarify sorting and order of categoricals
Without the addition in the text it‘s unclear, what s.sort_values(inplace=True)
should demonstrate. Show some meaningful output now. Also, when using astype to
contrast ordered/unordered behavior, I think it‘s clearer to use the existing,
unordered Series than to create a new one.
Use :okexept: directive as requested by mroeschke.
  • Loading branch information
StefRe committed Aug 5, 2022
commit f77645f68aa79e8dc3036fd17211854e5be769d8
6 changes: 2 additions & 4 deletions doc/source/user_guide/categorical.rst
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,11 @@ meaning and certain operations are possible. If the categorical is unordered, th
but ``.min()/.max()`` will raise a ``TypeError``.

.. ipython:: python
:okexcept:

s = pd.Series(pd.Categorical(["a", "b", "c", "a"], ordered=False))
s.sort_values(ascending=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not sort inplace, e.g. s is not updated

try:
s.min()
except TypeError as e:
print(f"TypeError: {e}")
s.min()
s = s.astype(CategoricalDtype(ordered=True))
s.sort_values()
s.min(), s.max()
Expand Down