Skip to content
53 changes: 53 additions & 0 deletions doc/source/reference/api/pandas.Series.__invert__.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.. currentmodule:: pandas
Copy link
Member

@rhshadrach rhshadrach Oct 18, 2025

Choose a reason for hiding this comment

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

With the request above, this file can be removed. Add anything necessary to the docstring of __invert__ instead.


pandas.Series.__invert__
========================

Elementwise invert (``~``) for :class:`pandas.Series`.

**Signature**

``Series.__invert__(self) -> Series``

**Summary**

For boolean and nullable-boolean dtypes, ``~s`` toggles the mask
(``True`` ↔ ``False``) and propagates ``pd.NA``. For integer dtypes,
``~`` performs bitwise invert as in Python.

.. seealso::
:ref:`Boolean indexing <indexing.boolean>`

**Examples**

Boolean / nullable boolean::

>>> s = pd.Series([True, pd.NA, False], dtype="boolean")
>>> ~s
0 False
1 <NA>
2 True
dtype: boolean

Integer vs boolean::

>>> s_int = pd.Series([0, 1, 2], dtype="int64")
>>> ~s_int
0 -1
1 -2
2 -3
dtype: int64

Arrow-backed boolean (if pyarrow installed)::

>>> s_arrow = pd.Series([True, pd.NA, False], dtype="boolean[pyarrow]")
>>> ~s_arrow
0 False
1 <NA>
2 True
dtype: boolean[pyarrow]

**Notes**

- In Python’s stdlib, :func:`operator.__invert__` is bitwise invert on integers.
In pandas, ``~`` on boolean arrays is elementwise invert.
8 changes: 8 additions & 0 deletions doc/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ Binary operator functions
Series.product
Series.dot

Unary operator functions
------------------------
.. toctree::
:maxdepth: 1

api/pandas.Series.__invert__
Copy link
Member

Choose a reason for hiding this comment

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

Can we follow the rest of the API documentation here:

.. autosummary::
   :toctree: api/

   Series.__invert__



Function application, GroupBy & window
--------------------------------------
.. autosummary::
Expand Down
4 changes: 4 additions & 0 deletions doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,10 @@ evaluate an expression such as ``df['A'] > 2 & df['B'] < 3`` as
``df['A'] > (2 & df['B']) < 3``, while the desired evaluation order is
``(df['A'] > 2) & (df['B'] < 3)``.

For toggling boolean masks with the ``~`` operator, see
:meth:`pandas.Series.__invert__`.
Comment on lines +814 to +815
Copy link
Member

Choose a reason for hiding this comment

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

~ is already mentioned in the paragraph above; I think this can be removed.



Using a boolean vector to index a Series works exactly as in a NumPy ndarray:

.. ipython:: python
Expand Down