Skip to content

DOC: Use more executed instead of static code blocks #54282

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
merged 15 commits into from
Jul 31, 2023
43 changes: 17 additions & 26 deletions doc/source/user_guide/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -620,31 +620,23 @@ inefficient (and show a ``PerformanceWarning``). It will also
return a copy of the data rather than a view:

.. ipython:: python
:okwarning:

dfm = pd.DataFrame(
{"jim": [0, 0, 1, 1], "joe": ["x", "x", "z", "y"], "jolie": np.random.rand(4)}
)
dfm = dfm.set_index(["jim", "joe"])
dfm

.. code-block:: ipython

In [4]: dfm.loc[(1, 'z')]
PerformanceWarning: indexing past lexsort depth may impact performance.

Out[4]:
jolie
jim joe
1 z 0.64094
dfm.loc[(1, 'z')]

.. _advanced.unsorted:

Furthermore, if you try to index something that is not fully lexsorted, this can raise:

.. code-block:: ipython
.. ipython:: python
:okexcept:

In [5]: dfm.loc[(0, 'y'):(1, 'z')]
UnsortedIndexError: 'Key length (2) was greater than MultiIndex lexsort depth (1)'
dfm.loc[(0, 'y'):(1, 'z')]

The :meth:`~MultiIndex.is_monotonic_increasing` method on a ``MultiIndex`` shows if the
index is sorted:
Expand Down Expand Up @@ -836,10 +828,10 @@ values **not** in the categories, similarly to how you can reindex **any** panda
df5 = df5.set_index("B")
df5.index

.. code-block:: ipython
.. ipython:: python
:okexcept:

In [1]: pd.concat([df4, df5])
TypeError: categories must match existing categories when appending
pd.concat([df4, df5])

.. _advanced.rangeindex:

Expand Down Expand Up @@ -921,11 +913,10 @@ Selecting using an ``Interval`` will only return exact matches.

Trying to select an ``Interval`` that is not exactly contained in the ``IntervalIndex`` will raise a ``KeyError``.

.. code-block:: python
.. ipython:: python
:okexcept:

In [7]: df.loc[pd.Interval(0.5, 2.5)]
---------------------------------------------------------------------------
KeyError: Interval(0.5, 2.5, closed='right')
df.loc[pd.Interval(0.5, 2.5)]

Selecting all ``Intervals`` that overlap a given ``Interval`` can be performed using the
:meth:`~IntervalIndex.overlaps` method to create a boolean indexer.
Expand Down Expand Up @@ -1062,15 +1053,14 @@ On the other hand, if the index is not monotonic, then both slice bounds must be
# OK because 2 and 4 are in the index
df.loc[2:4, :]

.. code-block:: ipython
.. ipython:: python
:okexcept:

# 0 is not in the index
In [9]: df.loc[0:4, :]
KeyError: 0
df.loc[0:4, :]

# 3 is not a unique label
In [11]: df.loc[2:3, :]
KeyError: 'Cannot get right slice bound for non-unique label: 3'
df.loc[2:3, :]

``Index.is_monotonic_increasing`` and ``Index.is_monotonic_decreasing`` only check that
an index is weakly monotonic. To check for strict monotonicity, you can combine one of those with
Expand Down Expand Up @@ -1109,7 +1099,8 @@ accomplished as such:
However, if you only had ``c`` and ``e``, determining the next element in the
index can be somewhat complicated. For example, the following does not work:

::
.. ipython:: python
:okexcept:

s.loc['c':'e' + 1]

Expand Down
43 changes: 18 additions & 25 deletions doc/source/user_guide/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,24 +322,21 @@ You can test if a pandas object is empty, via the :attr:`~DataFrame.empty` prope

.. warning::

You might be tempted to do the following:
Asserting the truthiness of a pandas object will raise an error, as the testing of the emptiness
or values is ambiguous.

.. code-block:: python

>>> if df:
... pass

Or

.. code-block:: python
.. ipython:: python
:okexcept:

>>> df and df2
if df:
print(True)

These will both raise errors, as you are trying to compare multiple values.::
.. ipython:: python
:okexcept:

ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all().
df and df2

See :ref:`gotchas<gotchas.truth>` for a more detailed discussion.
See :ref:`gotchas<gotchas.truth>` for a more detailed discussion.

.. _basics.equals:

Expand Down Expand Up @@ -404,13 +401,12 @@ objects of the same length:
Trying to compare ``Index`` or ``Series`` objects of different lengths will
raise a ValueError:

.. code-block:: ipython
.. ipython:: python
:okexcept:

In [55]: pd.Series(['foo', 'bar', 'baz']) == pd.Series(['foo', 'bar'])
ValueError: Series lengths must match to compare
pd.Series(['foo', 'bar', 'baz']) == pd.Series(['foo', 'bar'])

In [56]: pd.Series(['foo', 'bar', 'baz']) == pd.Series(['foo'])
ValueError: Series lengths must match to compare
pd.Series(['foo', 'bar', 'baz']) == pd.Series(['foo'])

Note that this is different from the NumPy behavior where a comparison can
be broadcast:
Expand Down Expand Up @@ -910,18 +906,15 @@ maximum value for each column occurred:
tsdf.apply(lambda x: x.idxmax())

You may also pass additional arguments and keyword arguments to the :meth:`~DataFrame.apply`
method. For instance, consider the following function you would like to apply:
method.

.. code-block:: python
.. ipython:: python

def subtract_and_divide(x, sub, divide=1):
return (x - sub) / divide

You may then apply this function as follows:

.. code-block:: python

df.apply(subtract_and_divide, args=(5,), divide=3)
df_udf = pd.DataFrame(np.ones((2, 2)))
df_udf.apply(subtract_and_divide, args=(5,), divide=3)

Another useful feature is the ability to pass Series methods to carry out some
Series operation on each column or row:
Expand Down
11 changes: 5 additions & 6 deletions doc/source/user_guide/categorical.rst
Original file line number Diff line number Diff line change
Expand Up @@ -873,13 +873,12 @@ categoricals of the same categories and order information

The below raises ``TypeError`` because the categories are ordered and not identical.

.. code-block:: ipython
.. ipython:: python
:okexcept:

In [1]: a = pd.Categorical(["a", "b"], ordered=True)
In [2]: b = pd.Categorical(["a", "b", "c"], ordered=True)
In [3]: union_categoricals([a, b])
Out[3]:
TypeError: to union ordered Categoricals, all categories must be the same
a = pd.Categorical(["a", "b"], ordered=True)
b = pd.Categorical(["a", "b", "c"], ordered=True)
union_categoricals([a, b])

Ordered categoricals with different categories or orderings can be combined by
using the ``ignore_ordered=True`` argument.
Expand Down
4 changes: 2 additions & 2 deletions doc/source/user_guide/dsintro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Series
type (integers, strings, floating point numbers, Python objects, etc.). The axis
labels are collectively referred to as the **index**. The basic method to create a :class:`Series` is to call:

::
.. code-block:: python

>>> s = pd.Series(data, index=index)
s = pd.Series(data, index=index)

Here, ``data`` can be many different things:

Expand Down
Loading