Skip to content

BUG: Series.map fails when keys are tuples of different lengths (#7333) #7336

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

Merged
merged 1 commit into from
Jun 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,4 @@ Bug Fixes
- Bug in ``CustomBusinessDay.apply`` raiases ``NameError`` when ``np.datetime64`` object is passed (:issue:`7196`)
- Bug in ``MultiIndex.append``, ``concat`` and ``pivot_table`` don't preserve timezone (:issue:`6606`)
- Bug all ``StringMethods`` now work on empty Series (:issue:`7242`)
- Bug in ``Series.map`` when mapping a dict with tuple keys of different lengths (:issue:`7333`)
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ def map_f(values, f):

if isinstance(arg, (dict, Series)):
if isinstance(arg, dict):
arg = self._constructor(arg)
arg = self._constructor(arg, index=arg.keys())

indexer = arg.index.get_indexer(values)
new_values = com.take_1d(arg.values, indexer)
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4860,6 +4860,25 @@ def test_map_na_exclusion(self):
exp = s * 2
assert_series_equal(result, exp)

def test_map_dict_with_tuple_keys(self):
'''
Due to new MultiIndex-ing behaviour in v0.14.0,
dicts with tuple keys passed to map were being
converted to a multi-index, preventing tuple values
from being mapped properly.
'''
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['expected_labels'] = pd.Series(['A', 'B', 'A', 'B'], index=df.index)
# All labels should be filled now
tm.assert_series_equal(df['labels'], df['expected_labels'])

def test_apply(self):
assert_series_equal(self.ts.apply(np.sqrt), np.sqrt(self.ts))

Expand Down