Pandas version checks

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

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

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

Reproducible Example

import pandas as pd

data = {"A": [1, 2, 3], "B": [4, 5, 6], True: [7, 8, 9]}

df = pd.DataFrame(data)

cols = ["A"]
df[cols]
#    A
# 0  1
# 1  2
# 2  3

cols = [True]
df[cols]  # ValueError: Item wrong length 1 instead of 3.

Issue Description

The issue arises when attempting to access a pandas.DataFrame using a list of boolean values as column names.

Expected Behavior

   A
0  7
1  8
2  9

Installed Versions

INSTALLED VERSIONS ------------------ commit : 0691c5cf90477d3503834d983f69350f250a6ff7 python : 3.12.4 python-bits : 64 OS : Darwin OS-release : 24.5.0 Version : Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 2.2.3 numpy : 2.2.4 pytz : 2025.2 dateutil : 2.9.0.post0 pip : 25.0.1 Cython : None sphinx : None IPython : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None blosc : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None html5lib : None hypothesis : None gcsfs : None jinja2 : 3.1.6 lxml.etree : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None psycopg2 : None pymysql : None pyarrow : None pyreadstat : None pytest : None python-calamine : None pyxlsb : None s3fs : None scipy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlsxwriter : None zstandard : None tzdata : 2025.2 qtpy : None pyqt5 : None

Comment From: rhshadrach

Thanks for the report. Unfortunately because there are multiple acceptable inputs similar to [True] (Boolean mask or list of columns), pandas has to make some choice as to what takes priority. It isn't clear to me that we should do one or the other, and because of that I'd be hesitant to change behavior here.

This is a great example of why I think pandas should only accept strings as columns (but of course, that would involve a significant amount of API changes and backwards incompatibility issues).

Comment From: arthurlw

-1 for the reasons above as well. [True] is ambiguous between a mask and a label, and changing the behavior risks breaking existing usage.

Thanks for raising this edge case though!