BUG: categorical Series.map() errors when mapping to tuples. #51488
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
topper-123 commentedon Feb 23, 2023
I checked this and can confirm this.
The underlying issue is that
Index.map
gives aMultiIndex
when gives tuples:and
Categorical
doesn't supportMultiIndex
as categories. So for backwards compatibility we probably want to keep the current behavior ofIndex.map
.Suggestion how to overcome this welcome.