BUG: or, and and not operators not correctly implemented for pd.NA

This issue has been created since 2022-11-22.

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

edit: formatting problem

Reproducible Example

import pandas as pd
not pd.NA
import pandas as pd
pd.NA and False
import pandas as pd
pd.NA or True

Issue Description

or, and and not operators not correctly implemented for pd.NA. Changing the order even raises error:

>>> True and pd.NA
<NA>
>>> False and pd.NA
False
>>> pd.NA and pd.NA
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/missing.pyx", line 413, in pandas._libs.missing.NAType.__bool__
    raise TypeError("boolean value of NA is ambiguous")
TypeError: boolean value of NA is ambiguous
>>> pd.NA and True
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/missing.pyx", line 413, in pandas._libs.missing.NAType.__bool__
    raise TypeError("boolean value of NA is ambiguous")
TypeError: boolean value of NA is ambiguous
>>> pd.NA and False
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/missing.pyx", line 413, in pandas._libs.missing.NAType.__bool__
    raise TypeError("boolean value of NA is ambiguous")
TypeError: boolean value of NA is ambiguous
>>> True or pd.NA
True
>>> False and pd.NA
False
>>> pd.NA and pd.NA
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/missing.pyx", line 413, in pandas._libs.missing.NAType.__bool__
    raise TypeError("boolean value of NA is ambiguous")
TypeError: boolean value of NA is ambiguous
>>> pd.NA and True
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/missing.pyx", line 413, in pandas._libs.missing.NAType.__bool__
    raise TypeError("boolean value of NA is ambiguous")
TypeError: boolean value of NA is ambiguous
>>> pd.NA and False
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/missing.pyx", line 413, in pandas._libs.missing.NAType.__bool__
    raise TypeError("boolean value of NA is ambiguous")
TypeError: boolean value of NA is ambiguous
>>> not pd.NA
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/missing.pyx", line 413, in pandas._libs.missing.NAType.__bool__
    raise TypeError("boolean value of NA is ambiguous")
TypeError: boolean value of NA is ambiguous

Expected Behavior

Same as R language:

> !NA
[1] NA
> NA | TRUE
[1] TRUE
> NA | FALSE
[1] NA
> NA & TRUE
[1] NA
> NA & FALSE
[1] FALSE

Installed Versions

INSTALLED VERSIONS

commit : 6f90ac3
python : 3.8.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.16.3-microsoft-standard-WSL2
Version : #1 SMP Fri Apr 2 22:23:49 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.0.0.dev0+719.g6f90ac3b2a.dirty
numpy : 1.23.4
pytz : 2022.6
dateutil : 2.8.2
setuptools : 65.5.1
pip : 22.3.1
Cython : 0.29.32
pytest : 7.2.0
hypothesis : 6.57.1
sphinx : 4.5.0
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.9.1
html5lib : 1.1
pymysql : 1.0.2
psycopg2 : 2.9.3
jinja2 : 3.0.3
IPython : 8.6.0
pandas_datareader: 0.10.0
bs4 : 4.11.1
bottleneck : 1.3.5
brotli :
fastparquet : 2022.11.0
fsspec : 2021.11.0
gcsfs : 2021.11.0
matplotlib : 3.6.2
numba : 0.56.3
numexpr : 2.8.3
odfpy : None
openpyxl : 3.0.10
pandas_gbq : 0.17.9
pyarrow : 9.0.0
pyreadstat : 1.2.0
pyxlsb : 1.0.10
s3fs : 2021.11.0
scipy : 1.9.3
snappy :
sqlalchemy : 1.4.44
tables : 3.7.0
tabulate : 0.9.0
xarray : 2022.11.0
xlrd : 2.0.1
zstandard : 0.19.0
tzdata : None
qtpy : None
pyqt5 : None

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-12-07
Star Count 36164
Watcher Count 1118
Fork Count 15472
Issue Count 3683

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Color correction during video streaming 1 2021-12-26 2022-11-25
ItemComponentCallback 1 2022-04-01 2022-11-15
Myanmar 3 Keyboard Issues 0 2022-09-17 2022-11-25
Don' Show "SwitchKey" Icon in menu bar on macOs 12.5 0 2022-10-21 2022-10-27
[Bug]: Calibrate Audio Sync crashes if song filters are applied that don't match the calibration file 1 2022-06-29 2022-09-21
I wonder how can i request a balance of address? 2 2021-08-27 2022-11-19
Distributed training 3 2021-04-23 2022-11-22
Values in filter should be translated and in user language 0 2021-11-08 2022-09-16
favorites do not open 2 2021-11-08 2022-10-03
Adafactor fails to run on a custom (rfs) resnet12 (with MAML) 0 2021-12-03 2022-11-18
Rack awareness doesn't work if nodes don't have rack id configured when first joining the cluster 1 2022-10-19 2022-10-17
DOC: Change documentation process 2 2022-08-18 2022-10-27
Security vulnerability in url-parse, please update module to version >=1.5.8 3 2022-03-28 2022-11-21
Is this project abandoned? 3 2022-04-15 2022-11-13
Error Compiling in Ubuntu 22.04 3 2022-11-17 2022-12-07
Clarify gimlet-seq power state representation 0 2022-09-30 2022-10-28
Problem with build_win.R in radiant-master 2 2022-03-22 2022-11-14
Remove old v1 version code 0 2021-09-11 2022-12-04
Feature Request 0 2021-12-22 2022-01-03
Hitting Docker Resource Limit when trying to create ChartMuseum Container 7 2021-04-16 2022-11-04
continue pretrain的时候遇到loss scale的问题,怎么解决? 4 2022-09-14 2022-11-19
Adding Facct? 0 2021-07-22 2021-12-30
not able to export edited NOTO 10 2021-10-18 2022-11-19
Remove all annotation according to their content? 1 2022-05-15 2022-11-19
python convert.py model_data/yolov4.cfg model_data/yolov4.weights model_data/yolo.h5 0 2021-11-25 2022-11-16
Causing issues with latest Ajv 2 2021-03-03 2022-11-24
Program doesn't run when I try to use a GTKBuilder file. 2 2021-01-24 2022-12-01
Incorrect path to CSS file 2 2021-01-27 2022-11-27
Need to correct README about model coverage 1 2021-05-26 2022-11-26
Default major and minor grid lines on plots are too dim 1 2022-01-21 2022-10-13
Add an option to display the size of a snapshot 5 2022-03-05 2022-10-06
marginal_counts cannot marginalize over previously Result object in main branch 1 2022-05-11 2022-11-22
`SabreSwap` can throw away classically conditioned gates and all successors 0 2022-05-10 2022-11-09
Add text reflow option 2 2021-10-23 2022-09-23
Create a security.md file for repo 1 2021-09-15 2022-08-12
Agent make check fails on lint with rust/clippy >= 1.58 0 2022-03-22 2022-10-23
config max matrix locations as total locations of all sources and targets 1 2022-03-09 2022-10-09
Deprecated and outdated parameters in Hub&Spoke sample 0 2022-05-27 2022-12-04
Reflection Function - support callbacks 0 2022-09-08 2022-10-02
Migrate homescreen Tip to Compose 3 2022-02-24 2022-07-16
www.city.habikino.lg.jp 0 2022-07-17 2022-09-30
Stage 2 - Hydra Heads with Pre-Calculated Peer IDs 2 2020-03-09 2022-11-27
content-type is not set by default for monitoring endpoints 2 2021-10-27 2022-11-01
StripeClient.customers.create doesn't accept Stripe.Customer interface as param 4 2022-09-01 2022-11-28
Ioring/Io_buffer Race Condition 5 2020-05-20 2022-11-05
[security] XMemcached deserialization vulnerability 1 2018-01-25 2022-10-31
chore(deps): update dependency prettier to v2.3.1 1 2021-06-05 2022-02-01
QR code button 0 2020-08-26 2022-01-08
InfluxQL: Create InfluxQL parser crate 0 2022-08-17 2022-09-19
CI broken: lint, ioutil 1 2022-09-21 2022-09-21