Skip to content

Commit 978ad36

Browse files
meeseeksmachinejschendel
authored andcommitted
Backport PR pandas-dev#31232: REGR: Fix IntervalIndex.map when result is object dtype (pandas-dev#31246)
Co-authored-by: Jeremy Schendel <[email protected]>
1 parent f55cedf commit 978ad36

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

pandas/core/indexes/datetimelike.py

-16
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,6 @@ def __contains__(self, key):
164164
except (KeyError, TypeError, ValueError):
165165
return False
166166

167-
# Try to run function on index first, and then on elements of index
168-
# Especially important for group-by functionality
169-
def map(self, mapper, na_action=None):
170-
try:
171-
result = mapper(self)
172-
173-
# Try to use this result if we can
174-
if isinstance(result, np.ndarray):
175-
result = Index(result)
176-
177-
if not isinstance(result, Index):
178-
raise TypeError("The map function must return an Index object")
179-
return result
180-
except Exception:
181-
return self.astype(object).map(mapper)
182-
183167
def sort_values(self, return_indexer=False, ascending=True):
184168
"""
185169
Return sorted copy of Index.

pandas/core/indexes/extension.py

+17
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,23 @@ def _get_unique_index(self, dropna=False):
232232
result = result[~result.isna()]
233233
return self._shallow_copy(result)
234234

235+
@Appender(Index.map.__doc__)
236+
def map(self, mapper, na_action=None):
237+
# Try to run function on index first, and then on elements of index
238+
# Especially important for group-by functionality
239+
try:
240+
result = mapper(self)
241+
242+
# Try to use this result if we can
243+
if isinstance(result, np.ndarray):
244+
result = Index(result)
245+
246+
if not isinstance(result, Index):
247+
raise TypeError("The map function must return an Index object")
248+
return result
249+
except Exception:
250+
return self.astype(object).map(mapper)
251+
235252
@Appender(Index.astype.__doc__)
236253
def astype(self, dtype, copy=True):
237254
if is_dtype_equal(self.dtype, dtype) and copy is False:

pandas/tests/indexes/categorical/test_category.py

+17
Original file line numberDiff line numberDiff line change
@@ -981,3 +981,20 @@ def test_getitem_2d_deprecated(self):
981981
idx = self.create_index()
982982
with pytest.raises(ValueError, match="cannot mask with array containing NA"):
983983
idx[:, None]
984+
985+
@pytest.mark.parametrize(
986+
"data, categories",
987+
[
988+
(list("abcbca"), list("cab")),
989+
(pd.interval_range(0, 3).repeat(3), pd.interval_range(0, 3)),
990+
],
991+
ids=["string", "interval"],
992+
)
993+
def test_map_str(self, data, categories, ordered_fixture):
994+
# GH 31202 - override base class since we want to maintain categorical/ordered
995+
index = CategoricalIndex(data, categories=categories, ordered=ordered_fixture)
996+
result = index.map(str)
997+
expected = CategoricalIndex(
998+
map(str, data), categories=map(str, categories), ordered=ordered_fixture
999+
)
1000+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/common.py

+7
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,13 @@ def test_map_dictlike(self, mapper):
808808
result = index.map(mapper(expected, index))
809809
tm.assert_index_equal(result, expected)
810810

811+
def test_map_str(self):
812+
# GH 31202
813+
index = self.create_index()
814+
result = index.map(str)
815+
expected = Index([str(x) for x in index], dtype=object)
816+
tm.assert_index_equal(result, expected)
817+
811818
def test_putmask_with_wrong_mask(self):
812819
# GH18368
813820
index = self.create_index()

0 commit comments

Comments
 (0)