Skip to content

BUG: Series.map(dict) doesn't work when dict keys are tuples of length 1? #7333

Closed
@onesandzeroes

Description

@onesandzeroes

I ran across an odd issue today where some analysis code that was working pre-pandas 0.14.0 started failing. The issue seems to be specific to trying to use Series.map(some_dict) when the dict keys (and values in the series) are single item tuples. Code to demonstrate the issue:

import pandas as pd
pd.version.version
Out[10]: '0.14.0'

df = pd.DataFrame({'a': [(1,), (2,), (3, 4), (5, 6)]})
label_mappings = {
    (1,): 'A',
    (2,): 'B',
    (3, 4): 'A',
    (5, 6): 'B'
}
df['labels'] = df['a'].map(label_mappings)
df
Out[7]: 
        a labels
0    (1,)    NaN
1    (2,)    NaN
2  (3, 4)      A
3  (5, 6)      B

The expected output here is:

df
Out[7]: 
        a labels
0    (1,)      A
1    (2,)      B
2  (3, 4)      A
3  (5, 6)      B

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions