Skip to content
  • Rate limit · GitHub

    Access has been restricted

    You have triggered a rate limit.

    Please wait a few minutes before you try again;
    in some cases this may take up to an hour.

  • Notifications You must be signed in to change notification settings
  • Fork 18.2k

BUG: categorical Series.map() errors when mapping to tuples. #51488

Open
@alexlenail

Description

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
s = pd.Series(['18m', '18m', '3m', '24m']).astype("category")
d = {'18m':'a', '24m':'b', '3m':'c'}
d2 = {'18m':('a',), '24m': ('b',), '3m': ('c',)}
s.map(d) # works
s.map(d2) # error
s.astype(str).map(d2) # works

Issue Description

Currently raises an unhelpful error message:

NotImplementedError: isna is not defined for MultiIndex

Expected Behavior

It should be possible to map categorical series to tuples, not just string series.

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.9.15.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.2
numpy : 1.22.4
pytz : 2022.6
dateutil : 2.8.2
setuptools : 65.5.1
pip : 22.2.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.7.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli :
fastparquet : None
fsspec : 2023.1.0
gcsfs : None
matplotlib : 3.6.2
numba : 0.56.4
numexpr : 2.8.3
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 10.0.1
pyreadstat : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.9.3
snappy : None
sqlalchemy : None
tables : 3.7.0
tabulate : 0.9.0
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : 0.19.0
tzdata : 2022.7

Activity

added
Needs TriageIssue that has not been reviewed by a pandas team member
on Feb 19, 2023
changed the title BUG: Series.map no longer permits mapping to tuples. BUG: categorical Series.map() errors when mapping to tuples. on Feb 19, 2023
topper-123

topper-123 commented on Feb 23, 2023

@topper-123
Contributor

I checked this and can confirm this.

The underlying issue is that Index.map gives a MultiIndex when gives tuples:

>>> import pandas as pd
>>> s = pd.Series(['18m', '18m', '3m', '24m']).astype("category")
>>> d = {'18m':('a',), '24m': ('b',), '3m': ('c',)}
>>> categories = s.array.categories
>>> categories.map(d)
MultiIndex([('a',),
            ('b',),
            ('c',)],
           )
>>> pd.Index(d.values())  # same interpretation as in .map
MultiIndex([('a',),
            ('b',),
            ('c',)],
           )

and Categorical doesn't support MultiIndex as categories. So for backwards compatibility we probably want to keep the current behavior of Index.map.

Suggestion how to overcome this welcome.

removed
Needs TriageIssue that has not been reviewed by a pandas team member
on Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      BUG: categorical Series.map() errors when mapping to tuples. · Issue #51488 · pandas-dev/pandas