|
119 | 119 | from pandas.core.algorithms import (
|
120 | 120 | checked_add_with_arr,
|
121 | 121 | isin,
|
| 122 | + map_array, |
122 | 123 | unique1d,
|
123 | 124 | )
|
124 | 125 | from pandas.core.array_algos import datetimelike_accumulations
|
@@ -751,17 +752,34 @@ def _unbox(self, other) -> np.int64 | np.datetime64 | np.timedelta64 | np.ndarra
|
751 | 752 |
|
752 | 753 | @ravel_compat
|
753 | 754 | def map(self, mapper, na_action=None):
|
754 |
| - if na_action is not None: |
755 |
| - raise NotImplementedError |
756 |
| - |
757 | 755 | # TODO(GH-23179): Add ExtensionArray.map
|
758 | 756 | # Need to figure out if we want ExtensionArray.map first.
|
759 | 757 | # If so, then we can refactor IndexOpsMixin._map_values to
|
760 | 758 | # a standalone function and call from here..
|
761 | 759 | # Else, just rewrite _map_infer_values to do the right thing.
|
762 | 760 | from pandas import Index
|
763 | 761 |
|
764 |
| - return Index(self).map(mapper).array |
| 762 | + idx = Index(self) |
| 763 | + |
| 764 | + try: |
| 765 | + if na_action is not None: |
| 766 | + raise ValueError("calling mapper directly only na_action=None") |
| 767 | + result = mapper(idx) |
| 768 | + |
| 769 | + # Try to use this result if we can |
| 770 | + if isinstance(result, np.ndarray): |
| 771 | + result = Index(result) |
| 772 | + |
| 773 | + if not isinstance(result, Index): |
| 774 | + raise TypeError("The map function must return an Index object") |
| 775 | + except Exception: |
| 776 | + result = map_array(self, mapper, na_action=na_action) |
| 777 | + result = Index(result) |
| 778 | + |
| 779 | + if isinstance(result, ABCMultiIndex): |
| 780 | + return result.to_numpy() |
| 781 | + else: |
| 782 | + return result.array |
765 | 783 |
|
766 | 784 | def isin(self, values) -> npt.NDArray[np.bool_]:
|
767 | 785 | """
|
|
0 commit comments