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

Function map_array

pandas/core/algorithms.py:1637–1717  ·  view source on GitHub ↗

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

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

Source from the content-addressed store, hash-verified

1635
1636
1637def map_array(
1638 arr: ArrayLike,
1639 mapper,
1640 na_action: Literal["ignore"] | None = None,
1641) -> np.ndarray | ExtensionArray | Index:
1642 """
1643 Map values using an input mapping or function.
1644
1645 Parameters
1646 ----------
1647 mapper : function, dict, or Series
1648 Mapping correspondence.
1649 na_action : {None, 'ignore'}, default None
1650 If 'ignore', propagate NA values, without passing them to the
1651 mapping correspondence.
1652
1653 Returns
1654 -------
1655 Union[ndarray, Index, ExtensionArray]
1656 The output of the mapping function applied to the array.
1657 If the function returns a tuple with more than one element
1658 a MultiIndex will be returned.
1659 """
1660 from pandas import Index
1661
1662 if na_action not in (None, "ignore"):
1663 msg = f"na_action must either be 'ignore' or None, {na_action} was passed"
1664 raise ValueError(msg)
1665
1666 # we can fastpath dict/Series to an efficient map
1667 # as we know that we are not going to have to yield
1668 # python types
1669 if is_dict_like(mapper):
1670 if isinstance(mapper, dict) and hasattr(mapper, "__missing__"):
1671 # If a dictionary subclass defines a default value method,
1672 # convert mapper to a lookup function (GH #15999).
1673 dict_with_default = mapper
1674 mapper = lambda x: dict_with_default[
1675 np.nan if isinstance(x, float) and np.isnan(x) else x
1676 ]
1677 else:
1678 # Dictionary does not have a default. Thus it's safe to
1679 # convert to a Series for efficiency.
1680 # we specify the keys here to handle the
1681 # possibility that they are tuples
1682
1683 # The return value of mapping with an empty mapper is
1684 # expected to be pd.Series(np.nan, ...). As np.nan is
1685 # of dtype float64 the return value of this method should
1686 # be float64 as well
1687 from pandas import Series
1688
1689 if len(mapper) == 0:
1690 mapper = Series(mapper, dtype=np.float64)
1691 elif isinstance(mapper, dict):
1692 mapper = Series(
1693 mapper.values(), index=Index(mapper.keys(), tupleize_cols=False)
1694 )

Callers 4

mapMethod · 0.90
mapMethod · 0.90
mapMethod · 0.90
mapMethod · 0.90

Calls 12

valuesMethod · 0.95
keysMethod · 0.95
SeriesClass · 0.90
IndexClass · 0.90
take_ndFunction · 0.90
isnaFunction · 0.90
is_dict_likeFunction · 0.85
notnaMethod · 0.45
get_indexerMethod · 0.45
copyMethod · 0.45
astypeMethod · 0.45
viewMethod · 0.45

Tested by

no test coverage detected