From 12561edbb8a7673a134cbe479636990fbbf45b8e Mon Sep 17 00:00:00 2001 From: rlawnsrl123 Date: Fri, 10 Oct 2025 00:19:06 +0900 Subject: [PATCH 1/5] Create pandas.Series.__invert__.rst --- .../api/pandas.Series.__invert__.rst | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 doc/source/reference/api/pandas.Series.__invert__.rst diff --git a/doc/source/reference/api/pandas.Series.__invert__.rst b/doc/source/reference/api/pandas.Series.__invert__.rst new file mode 100644 index 0000000000000..e7586c6a3b813 --- /dev/null +++ b/doc/source/reference/api/pandas.Series.__invert__.rst @@ -0,0 +1,48 @@ +.. currentmodule:: pandas + +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. + +**Examples** + +Boolean / nullable boolean:: + + >>> s = pd.Series([True, pd.NA, False], dtype="boolean") + >>> ~s + 0 False + 1 + 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 + 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. +* See also :ref:`user_guide.boolean_indexing`. From e34048db4bf2030ac52def653f2232ad499c2b1b Mon Sep 17 00:00:00 2001 From: rlawnsrl123 Date: Fri, 10 Oct 2025 00:28:22 +0900 Subject: [PATCH 2/5] DOC: add unary operator section; link Series.__invert__ --- doc/source/reference/series.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/source/reference/series.rst b/doc/source/reference/series.rst index 6006acc8f5e16..a099c48613e21 100644 --- a/doc/source/reference/series.rst +++ b/doc/source/reference/series.rst @@ -107,6 +107,14 @@ Binary operator functions Series.product Series.dot +Unary operator functions +------------------------ +.. toctree:: + :maxdepth: 1 + + api/pandas.Series.__invert__ + + Function application, GroupBy & window -------------------------------------- .. autosummary:: From 10ba2d2bd2051c9a0014c3cbe10c179d8a7e5c9f Mon Sep 17 00:00:00 2001 From: rlawnsrl123 Date: Fri, 10 Oct 2025 00:35:03 +0900 Subject: [PATCH 3/5] DOC: Add docs for Series.__invert__ (~); add cross-link from Boolean indexing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add dedicated API page for Series.__invert__() so searches for “invert/~” find it. --- doc/source/user_guide/indexing.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst index ebd1791c0f4ad..413ddb7a23155 100644 --- a/doc/source/user_guide/indexing.rst +++ b/doc/source/user_guide/indexing.rst @@ -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__`. + + Using a boolean vector to index a Series works exactly as in a NumPy ndarray: .. ipython:: python From ebe43acdb7daa2bec343c365ac1f148bede0e3fb Mon Sep 17 00:00:00 2001 From: rlawnsrl123 Date: Fri, 10 Oct 2025 01:29:07 +0900 Subject: [PATCH 4/5] Update pandas.Series.__invert__.rst --- .../reference/api/pandas.Series.__invert__.rst | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/doc/source/reference/api/pandas.Series.__invert__.rst b/doc/source/reference/api/pandas.Series.__invert__.rst index e7586c6a3b813..53443d60ce668 100644 --- a/doc/source/reference/api/pandas.Series.__invert__.rst +++ b/doc/source/reference/api/pandas.Series.__invert__.rst @@ -6,12 +6,17 @@ pandas.Series.__invert__ Elementwise invert (``~``) for :class:`pandas.Series`. **Signature** - ``Series.__invert__(self) -> Series`` + +``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. + +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:`indexing.boolean` **Examples** @@ -43,6 +48,6 @@ Arrow-backed boolean (if pyarrow installed):: dtype: boolean[pyarrow] **Notes** -* In Python’s stdlib, :func:`operator.__invert__` is bitwise invert on integers. + +- In Python’s stdlib, :func:`operator.__invert__` is bitwise invert on integers. In pandas, ``~`` on boolean arrays is elementwise invert. -* See also :ref:`user_guide.boolean_indexing`. From 5eb92c655ab044d18fa49c79d9f76d93f91ca71a Mon Sep 17 00:00:00 2001 From: rlawnsrl123 Date: Fri, 10 Oct 2025 02:15:07 +0900 Subject: [PATCH 5/5] Update pandas.Series.__invert__.rst --- doc/source/reference/api/pandas.Series.__invert__.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/reference/api/pandas.Series.__invert__.rst b/doc/source/reference/api/pandas.Series.__invert__.rst index 53443d60ce668..0c82307bca351 100644 --- a/doc/source/reference/api/pandas.Series.__invert__.rst +++ b/doc/source/reference/api/pandas.Series.__invert__.rst @@ -16,7 +16,7 @@ For boolean and nullable-boolean dtypes, ``~s`` toggles the mask ``~`` performs bitwise invert as in Python. .. seealso:: - :ref:`indexing.boolean` + :ref:`Boolean indexing ` **Examples**