Skip to content

BUG: Cannot sort by columns named None #61512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks done
zvun opened this issue May 28, 2025 · 1 comment · May be fixed by #61517
Open
3 tasks done

BUG: Cannot sort by columns named None #61512

zvun opened this issue May 28, 2025 · 1 comment · May be fixed by #61517
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Sorting e.g. sort_index, sort_values

Comments

@zvun
Copy link

zvun commented May 28, 2025

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.

Reproducible Example

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]], columns=['C1', None])
df.sort_values(None) # KeyError: None

Issue Description

Sorting a DataFrame by a column named None results in the error KeyError: None. This breaks e.g. plugins that depend on Pandas for viewing and sorting DataFrames (see a related DataWrangler issue here, where inconsistent behavior with columns named None has also been reported).

Expected Behavior

A column named None should not result in inconsistent behavior where some operations work but some others don't.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.11.2
python-bits : 64
OS : Linux
OS-release : 6.1.0-32-amd64
Version : #1 SMP PREEMPT_DYNAMIC Debian 6.1.129-1 (2025-03-06)
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 2.2.6
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.0
Cython : None
sphinx : None
IPython : 9.2.0
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 : None
lxml.etree : None
matplotlib : 3.10.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.3.5
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

@zvun zvun added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 28, 2025
@rhshadrach
Copy link
Member

rhshadrach commented May 30, 2025

Thanks for the report. In general I suspect you will have a bad time trying to use None in an index or columns. However as long as pandas allows it, we should strive to improve this behavior. Further investigations and PRs to fix are welcome.

With future.infer_string = True, this can now at least work. As part of improved NA-handling, I would like to see None no longer used as meaning "NA value for arbitrary dtypes" and only have pd.NA play that role. But that's a long way down the road.

pd.set_option("future.infer_string", True)
df = pd.DataFrame([[1, 2], [3, 4]], columns=['C1', None])
print(df)
df.sort_values(pd.NA)
#    C1  None
# 0   1     2
# 1   3     4

@rhshadrach rhshadrach added Indexing Related to indexing on series/frames, not to indexes themselves Sorting e.g. sort_index, sort_values and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Sorting e.g. sort_index, sort_values
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants