Pandas version checks

  • [x] I have checked that this issue has not already been reported.

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

  • [x] I have confirmed this bug exists on the main branch of pandas.

Issue Description

The Series .map() function frequently fails when using dictionaries with tuple keys which is given as parameter to the map function. See the below examples:

Ex 1:

import pandas as pd

df = pd.DataFrame({"a": [(1,1), (2,2), (3,4), (5,6)]})  
label_mappings = {(1,): "A", (2,2): "B", (3,4): "A", (5,6): "B"}  
df["mapped_labels"] = df["a"].map(label_mappings)  

print(df)
# Output:
#        a        mapped_labels
# 0  (1, 1)             A
# 1  (2, 2)             B
# 2  (3, 4)             A
# 3  (5, 6)             B

# Expected Ouput:
#        a        mapped_labels
# 0  (1, 1)           NaN
# 1  (2, 2)             B
# 2  (3, 4)             A
# 3  (5, 6)             B

Ex 2:

import pandas as pd

df = pd.DataFrame({"a": [(1,1), (2,2), (3,4), (5,6)]})  
label_mappings = {(2,): "A", (2,2): "B", (3,4): "A", (5,6): "B"}  
df["mapped_labels"] = df["a"].map(label_mappings)  

print(df)
# Output:
# Produces error: pandas.errors.InvalidIndexError: Reindexing only valid with uniquely valued Index objects

# Expected Ouput:
#        a        mapped_labels
# 0  (1, 1)           NaN
# 1  (2, 2)             B
# 2  (3, 4)             A
# 3  (5, 6)             B

EX 3:

import pandas as pd

df = pd.DataFrame({"a": [(1,), (2,2), (3,4), (5,6)]})  
label_mappings = {(1,None): "A", (2,2): "B", (3,4): "A", (5,6): "B"}  
df["mapped_labels"] = df["a"].map(label_mappings)  

print(df)

# Output:
# Produces error: AssertionError: Length of new_levels (2) must be <= self.nlevels (1)

# Expected Ouput:
#        a        mapped_labels
# 0  (1, )               NaN
# 1  (2, 2)             B
# 2  (3, 4)             A
# 3  (5, 6)             B

EX 4:

import pandas as pd

df = pd.DataFrame({"a": [(1,), (2,2), (3,4), (5,6)]})  
label_mappings = {(1,1): "A", (2,2): "B", (3,4): "A", (5,6): "B"}   
df["mapped_labels"] = df["a"].map(label_mappings)  

print(df)

# Output:
# Produces error: AssertionError: Length of new_levels (2) must be <= self.nlevels (1)

# Expected Ouput:
#        a        mapped_labels
# 0  (1, )               NaN
# 1  (2, 2)             B
# 2  (3, 4)             A
# 3  (5, 6)             B

EX 5:

import pandas as pd

df = pd.DataFrame({"a": [(1,1), (1,2), (3,4), (5,6)]})  
label_mappings = {(1,): "A", (2,2): "B", (3,4): "A", (5,6): "B"}   
df["mapped_labels"] = df["a"].map(label_mappings)  

print(df)

# Output:
#        a        mapped_labels
# 0  (1, 1)             A
# 1  (1, 2)             A
# 2  (3, 4)             A
# 3  (5, 6)             B

# Expected Ouput:
#        a        mapped_labels
# 0  (1, 1)             NaN
# 1  (1, 2)             NaN
# 2  (3, 4)             A
# 3  (5, 6)             B

Expected Behavior

Gave the actual outputs and expected outputs.

Installed Versions

INSTALLED VERSIONS ------------------ commit : 6bcd30397d67c3887288c7a82c2c235ce8bc3c7f python : 3.10.14 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.26120 machine : AMD64 processor : AMD64 Family 23 Model 96 Stepping 1, AuthenticAMD byteorder : little LC_ALL : None LANG : None LOCALE : English_United States.1252 pandas : 3.0.0.dev0+1944.g6bcd30397d.dirty numpy : 1.26.4 dateutil : 2.9.0.post0 pip : 24.0 Cython : 3.0.10 sphinx : 7.3.7 IPython : 8.18.1 adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : 4.12.3 blosc : None bottleneck : None fastparquet : None fsspec : 2024.6.1 html5lib : 1.1 hypothesis : 6.105.0 gcsfs : 2024.6.1 jinja2 : 3.1.4 lxml.etree : None matplotlib : None numba : 0.61.0 numexpr : None odfpy : None openpyxl : 3.1.5 psycopg2 : None pymysql : 1.4.6 pyarrow : 19.0.0 pyreadstat : None pytest : 8.3.4 python-calamine : None pytz : 2025.1 pyxlsb : 1.0.10 s3fs : 2024.6.1 scipy : 1.15.2 sqlalchemy : 2.0.31 tables : None tabulate : 0.9.0 xarray : 2024.6.0 xlrd : 2.0.1 xlsxwriter : 3.2.0 zstandard : None tzdata : 2025.1 qtpy : None pyqt5 : None

Comment From: Anurag-Varma

When I was trying to fix the issue #60695

The below test case was failing: pandas/tests/series/methods/test_map.py::test_map_dict_with_tuple_keys

When i further tried to fix it, then found that Series.map() was failing for multiple examples as mentioned above.

Comment From: Anurag-Varma

take