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(["foo", "bar"]).astype("category")
map_all = {"foo": [1, 2], "bar": [3, 4]}
map_partial = {"foo": [1, 2]}
# mapping all categories fails
try:
s.map(map_all)
except Exception as e:
print("Mapping all categories in categorical:", type(e).__name__, ":", e)
# mapping partial categories works
print("Mapping partial categories in categorical:")
print(s.map(map_partial), sep="\n")
# workaround: convert column to object
print("Mapping all categories in object:")
print(s.astype('object').map(map_all))
# Output:
# Mapping all categories in categorical: TypeError : unhashable type: 'list'
# Mapping partial categories in categorical:
# 0 [1, 2]
# 1 NaN
# dtype: object
# Mapping all categories in object:
# 0 [1, 2]
# 1 [3, 4]
# dtype: object
Issue Description
When Series.map
is called on a categorical column and provided with a mapping that maps all categories to a list (or other unhashable object), a TypeError : unhashable type: 'list'
is raised. However, if mapping only some of the categories, or if first converting to an object column, the operation succeeds.
Expected Behavior
map
on a categorical column should succeed when a full mapping is provided, with the same results as when map
is called on an object column:
import pandas as pd
s = pd.Series(["foo", "bar"]).astype("category")
expected_output = s.map({"foo": [1, 2], "bar": [3, 4]})
print(expected_output)
# Expected output:
# 0 [1, 2]
# 1 [3, 4]
# dtype: object
Installed Versions
INSTALLED VERSIONS
commit : 0f43794
python : 3.11.4.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.90.1-microsoft-standard-WSL2
Version : #1 SMP Fri Jan 27 02:56:13 UTC 2023
machine : x86_64
processor :
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.0.3
numpy : 1.25.1
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : None
pytest : 7.4.0
hypothesis : None
sphinx : 7.1.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
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 : 3.7.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.1
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None