Skip to content

BUG: sort_values raises error about null categorical values when no categorical types are used #54908

Open
@rohanjain101

Description

@rohanjain101

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

>>> t = pd.Series([None, None], dtype=pd.ArrowDtype(pa.bool_()))
>>> s = pd.Series([None, None], dtype=pd.ArrowDtype(pa.null()))
>>> df = pd.DataFrame({"A": s, "B": t})
>>> df.sort_values(list(df.columns))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ps_311_0722\Lib\site-packages\pandas\core\frame.py", line 6751, in sort_values
    indexer = lexsort_indexer(
              ^^^^^^^^^^^^^^^^
  File "C:\ps_311_0722\Lib\site-packages\pandas\core\sorting.py", line 341, in lexsort_indexer
    cat = Categorical(k, ordered=True)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ps_311_0722\Lib\site-packages\pandas\core\arrays\categorical.py", line 438, in __init__
    dtype = CategoricalDtype(categories, dtype.ordered)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ps_311_0722\Lib\site-packages\pandas\core\dtypes\dtypes.py", line 187, in __init__
    self._finalize(categories, ordered, fastpath=False)
  File "C:\ps_311_0722\Lib\site-packages\pandas\core\dtypes\dtypes.py", line 344, in _finalize
    categories = self.validate_categories(categories, fastpath=fastpath)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ps_311_0722\Lib\site-packages\pandas\core\dtypes\dtypes.py", line 537, in validate_categories
    raise ValueError("Categorical categories cannot be null")
ValueError: Categorical categories cannot be null
>>>

Issue Description

sort_values raises an error about categorical type categories, but no categorical types are used.

Expected Behavior

I would expect sort to succeed and all values to be null.

Installed Versions

INSTALLED VERSIONS

commit : 965ceca
python : 3.11.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 7, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.0.2
numpy : 1.25.1
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.1.2
Cython : None
pytest : 7.0.1
hypothesis : None
sphinx : 4.2.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 12.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Arrowpyarrow functionalityBugSortinge.g. sort_index, sort_values

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions