Skip to content
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

BUG: sort_values raises error about Categorical categories must be unique when no categorical types used #54911

Closed
2 of 3 tasks
rohanjain101 opened this issue Aug 31, 2023 · 4 comments
Labels
Arrow pyarrow functionality Bug Upstream issue Issue related to pandas dependency

Comments

@rohanjain101
Copy link
Contributor

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

>>> s1 = pd.Series([-0.0, 0.0], dtype="float[pyarrow]")
>>> s2 = pd.Series([False, False], dtype="bool[pyarrow]")
>>> df = pd.DataFrame({"A": s1, "B": s2})
>>> 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 540, in validate_categories
    raise ValueError("Categorical categories must be unique")
ValueError: Categorical categories must be unique
>>>

Issue Description

There's no categorical types used in the above example, so error is unexpected.

Expected Behavior

df is already sorted, so I expect it to be returned without raising an error.

Installed Versions

INSTALLED VERSIONS

commit : ba1cccd
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.1.0
numpy : 1.25.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@rohanjain101 rohanjain101 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 31, 2023
@lukemanley
Copy link
Member

Thanks for the report. It looks like this is raising in Categorical:

import pandas as pd

ser = pd.Series([-0.0, 0.0], dtype="float[pyarrow]")
pd.Categorical(ser)

@lukemanley
Copy link
Member

More specifically, this looks like the disconnect:

In [1]: import pandas as pd

In [2]: arr = pd.array([-0.0, 0.0], dtype="float[pyarrow]")

In [3]: pd.Series(arr).is_unique
Out[3]: True

In [4]: pd.Index(arr).is_unique
Out[4]: False

@lukemanley lukemanley removed the Needs Triage Issue that has not been reviewed by a pandas team member label Sep 1, 2023
@rhshadrach rhshadrach added the Arrow pyarrow functionality label Sep 1, 2023
@lukemanley
Copy link
Member

xref: apache/arrow#26214

@mroeschke mroeschke added the Upstream issue Issue related to pandas dependency label Dec 6, 2023
@mroeschke
Copy link
Member

Closing as awaiting resolution in apache/arrow#26214

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality Bug Upstream issue Issue related to pandas dependency
Projects
None yet
Development

No branches or pull requests

4 participants