Skip to content

BUG: Frozensets are not supported as indices. #41238

Open
@pezize

Description

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

df = DataFrame.from_dict({'key': [frozenset([1])], 'value': [2]}).set_index('key')

k = frozenset([1])

k in df.index  # True

df.loc[[k]]  # OK
df.loc[k]  # raises: KeyError: "None of [Int64Index([1], dtype='int64', name='key')] are in the [index]"

df.value.loc[[k]]  # OK
df.value.loc[k]  # raises: KeyError: "None of [Int64Index([1], dtype='int64', name='key')] are in the [index]"

Problem description

Frozensets are hashable and could serve as indices. They are only partially supported in the current version:

  • set a frozenset as index: OK,
  • is frozenset in index: OK,
  • index with a collection of frozensets: OK,
  • index with a frozenset: KeyError.

This applies to both Series and DataFrame.

Full support would be nice.

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : 2cb96529396d93b46abab7bbc73a208e708c642e
python           : 3.9.2.final.0
python-bits      : 64
OS               : Windows
OS-release       : 10
Version          : 10.0.19041
machine          : AMD64
processor        : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder        : little
LC_ALL           : None
LANG             : None
LOCALE           : English_United States.utf8

pandas           : 1.2.4
numpy            : 1.20.1
pytz             : 2021.1
dateutil         : 2.8.1
pip              : 21.0.1
setuptools       : 49.6.0.post20210108
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.11.3
IPython          : 7.21.0
pandas_datareader: None
bs4              : None
bottleneck       : None
fsspec           : 0.9.0
fastparquet      : None
gcsfs            : None
matplotlib       : 3.3.4
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pyxlsb           : None
s3fs             : None
scipy            : 1.6.2
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
numba            : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions