Skip to content

DOC: Don't show traceback for :okexcept: #48394

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@

extensions = [
"contributors", # custom pandas extension
"IPython.sphinxext.ipython_directive",
"IPython.sphinxext.ipython_console_highlighting",
"ipython_sphinxext.ipython_directive",
"ipython_sphinxext.ipython_console_highlighting",
"matplotlib.sphinxext.plot_directive",
"numpydoc",
"sphinx_copybutton",
Expand Down
56 changes: 19 additions & 37 deletions doc/source/user_guide/categorical.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,20 +356,16 @@ Renaming categories is done by using the
Categories must be unique or a ``ValueError`` is raised:

.. ipython:: python
:okexcept: no_traceback

try:
s = s.cat.rename_categories([1, 1, 1])
except ValueError as e:
print("ValueError:", str(e))
s = s.cat.rename_categories([1, 1, 1])

Categories must also not be ``NaN`` or a ``ValueError`` is raised:

.. ipython:: python
:okexcept: no_traceback

try:
s = s.cat.rename_categories([1, 2, np.nan])
except ValueError as e:
print("ValueError:", str(e))
s = s.cat.rename_categories([1, 2, np.nan])

Appending new categories
~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -572,11 +568,9 @@ Equality comparisons work with any list-like object of same length and scalars:
This doesn't work because the categories are not the same:

.. ipython:: python
:okexcept: no_traceback

try:
cat > cat_base2
except TypeError as e:
print("TypeError:", str(e))
cat > cat_base2

If you want to do a "non-equality" comparison of a categorical series with a list-like object
which is not categorical data, you need to be explicit and convert the categorical data back to
Expand All @@ -586,10 +580,8 @@ the original values:

base = np.array([1, 2, 3])

try:
cat > base
except TypeError as e:
print("TypeError:", str(e))
@okexcept no_traceback # noqa: E999
cat > base

np.asarray(cat) > base

Expand Down Expand Up @@ -768,21 +760,17 @@ value is included in the ``categories``:

df.iloc[2:4, :] = [["b", 2], ["b", 2]]
df
try:
df.iloc[2:4, :] = [["c", 3], ["c", 3]]
except TypeError as e:
print("TypeError:", str(e))
@okexcept no_traceback # noqa: E999
df.iloc[2:4, :] = [["c", 3], ["c", 3]]

Setting values by assigning categorical data will also check that the ``categories`` match:

.. ipython:: python

df.loc["j":"k", "cats"] = pd.Categorical(["a", "a"], categories=["a", "b"])
df
try:
df.loc["j":"k", "cats"] = pd.Categorical(["b", "b"], categories=["a", "b", "c"])
except TypeError as e:
print("TypeError:", str(e))
@okexcept no_traceback # noqa: E999
df.loc["j":"k", "cats"] = pd.Categorical(["b", "b"], categories=["a", "b", "c"])

Assigning a ``Categorical`` to parts of a column of other types will use the values:

Expand Down Expand Up @@ -1067,16 +1055,12 @@ NumPy itself doesn't know about the new ``dtype``:

.. ipython:: python

try:
np.dtype("category")
except TypeError as e:
print("TypeError:", str(e))
@okexcept no_traceback # noqa: E999
np.dtype("category")

dtype = pd.Categorical(["a"]).dtype
try:
np.dtype(dtype)
except TypeError as e:
print("TypeError:", str(e))
@okexcept no_traceback # noqa: E999
np.dtype(dtype)

Dtype comparisons work:

Expand All @@ -1098,11 +1082,9 @@ are not numeric data (even in the case that ``.categories`` is numeric).
.. ipython:: python

s = pd.Series(pd.Categorical([1, 2, 3, 4]))
try:
np.sum(s)
# same with np.log(s),...
except TypeError as e:
print("TypeError:", str(e))
@okexcept no_traceback # noqa: E999
np.sum(s)
# same with np.log(s),...

.. note::
If such a function works, please file a bug at https://github.com/pandas-dev/pandas!
Expand Down
Empty file.
60 changes: 60 additions & 0 deletions doc/sphinxext/ipython_sphinxext/ipython_console_highlighting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
reST directive for syntax-highlighting ipython interactive sessions.

"""

from IPython.lib.lexers import IPyLexer
from sphinx import highlighting


def patch_IPythonConsoleLexer():
"""Patch lexers.IPythonConsoleLexer.ipytb_start.

This extension uses IPyLexer to highlight the exception. An exception is
recognized by the traceback. The start of a traceback is found using the
ipytb_start regex. This regex has to be patched to also find exceptions
without a preceding traceback.
"""
import builtins
import re

from IPython.lib import lexers

exceptions = [
name
for name, value in builtins.__dict__.items()
if isinstance(value, type)
and issubclass(value, Exception)
and not issubclass(value, Warning)
]
lexers.IPythonConsoleLexer.ipytb_start = re.compile(
r"^(\^C)?(-+\n)|^( File)(.*)(, line )(\d+\n)|^("
+ "|".join(exceptions)
+ r"): \S.*\n"
)


patch_IPythonConsoleLexer()


def setup(app):
"""Setup as a sphinx extension."""

# This is only a lexer, so adding it below to pygments appears sufficient.
# But if somebody knows what the right API usage should be to do that via
# sphinx, by all means fix it here. At least having this setup.py
# suppresses the sphinx warning we'd get without it.
metadata = {"parallel_read_safe": True, "parallel_write_safe": True}
return metadata


# Register the extension as a valid pygments lexer.
# Alternatively, we could register the lexer with pygments instead. This would
# require using setuptools entrypoints: http://pygments.org/docs/plugins

ipy2 = IPyLexer(python3=False)
ipy3 = IPyLexer(python3=True)

highlighting.lexers["ipython"] = ipy2
highlighting.lexers["ipython2"] = ipy2
highlighting.lexers["ipython3"] = ipy3
Loading