Skip to content
9 changes: 9 additions & 0 deletions doc/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ Binary operator functions
Series.product
Series.dot

Unary operator functions
------------------------
.. 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
38 changes: 38 additions & 0 deletions pandas/core/ops/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,41 @@ def make_flex_doc(op_name: str, typ: str) -> str:
B True False
C True False
"""

# --- add at bottom of pandas/core/ops/docstrings.py ---

_doc_series_invert = """
Bitwise NOT (``~``).

For boolean dtype, behaves as logical NOT. Missing values (``NA``) propagate.
For integer dtypes, performs bitwise inversion (two's complement) elementwise.

Returns
-------
Series
Elementwise result with the same index and name.

Notes
-----
On ``boolean[pyarrow]`` dtype, ``NA`` values also propagate under ``~``.

See Also
--------
Series.__and__, Series.__or__, Series.__xor__
Copy link
Member

Choose a reason for hiding this comment

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

Can you separate out to one line each, with a short description. See other docstrings in this file for examples.


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

>>> s = pd.Series([1, 2, -3], dtype="Int32")
>>> ~s
0 -2
1 -3
2 2
dtype: Int32
"""
4 changes: 4 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -7338,3 +7338,7 @@ def cumsum(self, axis: Axis = 0, skipna: bool = True, *args, **kwargs) -> Self:
@doc(make_doc("cumprod", 1))
def cumprod(self, axis: Axis = 0, skipna: bool = True, *args, **kwargs) -> Self:
return NDFrame.cumprod(self, axis, skipna, *args, **kwargs)

# at bottom of pandas/core/series.py (after class Series ...)
from pandas.core.ops.docstrings import _doc_series_invert
Series.__invert__.__doc__ = _doc_series_invert
Comment on lines +7342 to +7344
Copy link
Member

Choose a reason for hiding this comment

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

I think we can instead add the following to series.py in the Series class:

def __invert__(self) -> Self:
    return super().__invert__()

Then we can add the docstring there. But I would like a 2nd opinion here, cc @mroeschke.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed. It's best to keep docstring with the function definitions

Loading