MCPcopy
hub / github.com/pandas-dev/pandas / map

Method map

pandas/core/indexes/base.py:6580–6650  ·  view source on GitHub ↗

Map values using an input mapping or function. Parameters ---------- mapper : function, dict, or Series Mapping correspondence. na_action : {None, 'ignore'} If 'ignore', propagate NA values, without passing them to the map

(self, mapper, na_action: Literal["ignore"] | None = None)

Source from the content-addressed store, hash-verified

6578 return PrettyDict(result)
6579
6580 def map(self, mapper, na_action: Literal["ignore"] | None = None):
6581 """
6582 Map values using an input mapping or function.
6583
6584 Parameters
6585 ----------
6586 mapper : function, dict, or Series
6587 Mapping correspondence.
6588 na_action : {None, 'ignore'}
6589 If 'ignore', propagate NA values, without passing them to the
6590 mapping correspondence.
6591
6592 Returns
6593 -------
6594 Union[Index, MultiIndex]
6595 The output of the mapping function applied to the index.
6596 If the function returns a tuple with more than one element
6597 a MultiIndex will be returned.
6598
6599 See Also
6600 --------
6601 Index.where : Replace values where the condition is False.
6602
6603 Examples
6604 --------
6605 >>> idx = pd.Index([1, 2, 3])
6606 >>> idx.map({1: "a", 2: "b", 3: "c"})
6607 Index(['a', 'b', 'c'], dtype='str')
6608
6609 Using `map` with a function:
6610
6611 >>> idx = pd.Index([1, 2, 3])
6612 >>> idx.map("I am a {}".format)
6613 Index(['I am a 1', 'I am a 2', 'I am a 3'], dtype='str')
6614
6615 >>> idx = pd.Index(["a", "b", "c"])
6616 >>> idx.map(lambda x: x.upper())
6617 Index(['A', 'B', 'C'], dtype='str')
6618 """
6619 from pandas.core.indexes.multi import MultiIndex
6620
6621 new_values = self._map_values(mapper, na_action=na_action)
6622
6623 # we can return a MultiIndex
6624 if new_values.size and isinstance(new_values[0], tuple):
6625 if isinstance(self, MultiIndex):
6626 names = self.names
6627 elif self.name:
6628 names = [self.name] * len(new_values[0])
6629 else:
6630 names = None
6631 return MultiIndex.from_tuples(new_values, names=names)
6632
6633 dtype = None
6634 if not new_values.size:
6635 # empty
6636 dtype = self.dtype
6637 elif isinstance(new_values, Categorical):

Callers 14

_format_flatMethod · 0.95
test_map_with_tuplesMethod · 0.95
test_map_na_exclusionMethod · 0.95
test_map_defaultdictMethod · 0.95
test_equals2Method · 0.95
test_equals2Method · 0.95
_convert_and_box_cacheFunction · 0.45
to_datetimeFunction · 0.45

Calls 5

IndexClass · 0.85
_map_valuesMethod · 0.80
from_tuplesMethod · 0.45
to_flat_indexMethod · 0.45