Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: The Series .map() function frequently fails when using dictionaries with tuple keys in various cases. #60988

Open
3 tasks done
Anurag-Varma opened this issue Feb 22, 2025 · 2 comments · May be fixed by #60944
Open
3 tasks done
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@Anurag-Varma
Copy link
Contributor

Anurag-Varma commented Feb 22, 2025

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.

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 : 6bcd303
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

@Anurag-Varma Anurag-Varma added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 22, 2025
@Anurag-Varma
Copy link
Contributor Author

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.

@Anurag-Varma
Copy link
Contributor Author

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
1 participant