Skip to content

Commit 3135c62

Browse files
authored
REF: move ExtensionIndex.map to be part of DatetimeLikeArrayMixin.map (#51934)
1 parent 693fb71 commit 3135c62

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

pandas/core/arrays/datetimelike.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
from pandas.core.algorithms import (
120120
checked_add_with_arr,
121121
isin,
122+
map_array,
122123
unique1d,
123124
)
124125
from pandas.core.array_algos import datetimelike_accumulations
@@ -754,14 +755,26 @@ def map(self, mapper, na_action=None):
754755
if na_action is not None:
755756
raise NotImplementedError
756757

757-
# TODO(GH-23179): Add ExtensionArray.map
758-
# Need to figure out if we want ExtensionArray.map first.
759-
# If so, then we can refactor IndexOpsMixin._map_values to
760-
# a standalone function and call from here..
761-
# Else, just rewrite _map_infer_values to do the right thing.
762758
from pandas import Index
763759

764-
return Index(self).map(mapper).array
760+
idx = Index(self)
761+
try:
762+
result = mapper(idx)
763+
764+
# Try to use this result if we can
765+
if isinstance(result, np.ndarray):
766+
result = Index(result)
767+
768+
if not isinstance(result, Index):
769+
raise TypeError("The map function must return an Index object")
770+
except Exception:
771+
result = map_array(self, mapper)
772+
result = Index(result)
773+
774+
if isinstance(result, ABCMultiIndex):
775+
return result.to_numpy()
776+
else:
777+
return result.array
765778

766779
def isin(self, values) -> npt.NDArray[np.bool_]:
767780
"""

pandas/core/base.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -889,10 +889,8 @@ def _map_values(self, mapper, na_action=None):
889889
"""
890890
arr = extract_array(self, extract_numpy=True, extract_range=True)
891891

892-
if is_extension_array_dtype(arr.dtype):
893-
# Item "IndexOpsMixin" of "Union[IndexOpsMixin, ExtensionArray,
894-
# ndarray[Any, Any]]" has no attribute "map"
895-
return arr.map(mapper, na_action=na_action) # type: ignore[union-attr]
892+
if isinstance(arr, ExtensionArray):
893+
return arr.map(mapper, na_action=na_action)
896894

897895
# Argument 1 to "map_array" has incompatible type
898896
# "Union[IndexOpsMixin, ExtensionArray, ndarray[Any, Any]]";

pandas/core/indexes/extension.py

+3-23
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@
99
TypeVar,
1010
)
1111

12-
import numpy as np
13-
14-
from pandas.util._decorators import (
15-
cache_readonly,
16-
doc,
17-
)
12+
from pandas.util._decorators import cache_readonly
1813

1914
from pandas.core.dtypes.generic import ABCDataFrame
2015

2116
from pandas.core.indexes.base import Index
2217

2318
if TYPE_CHECKING:
19+
import numpy as np
20+
2421
from pandas._typing import (
2522
ArrayLike,
2623
npt,
@@ -154,23 +151,6 @@ def _validate_fill_value(self, value):
154151
"""
155152
return self._data._validate_setitem_value(value)
156153

157-
@doc(Index.map)
158-
def map(self, mapper, na_action=None):
159-
# Try to run function on index first, and then on elements of index
160-
# Especially important for group-by functionality
161-
try:
162-
result = mapper(self)
163-
164-
# Try to use this result if we can
165-
if isinstance(result, np.ndarray):
166-
result = Index(result)
167-
168-
if not isinstance(result, Index):
169-
raise TypeError("The map function must return an Index object")
170-
return result
171-
except Exception:
172-
return self.astype(object).map(mapper)
173-
174154
@cache_readonly
175155
def _isnan(self) -> npt.NDArray[np.bool_]:
176156
# error: Incompatible return value type (got "ExtensionArray", expected

0 commit comments

Comments
 (0)