def test_map_str(self, data, categories, ordered): # GH 31202 - override base class since we want to maintain categorical/ordered index = CategoricalIndex(data, categories=categories, ordered=ordered) result = index.map(str) expected = CategoricalIndex(map(str, data), categories=map(str, categories), ordered=ordered) tm.assert_index_equal(result, expected)
def test_map_with_dict_or_series(self): orig_values = ["a", "B", 1, "a"] new_values = ["one", 2, 3.0, "one"] cur_index = CategoricalIndex(orig_values, name="XXX") expected = CategoricalIndex(new_values, name="XXX", categories=[3.0, 2, "one"]) mapper = Series(new_values[:-1], index=orig_values[:-1]) result = cur_index.map(mapper) # Order of categories in result can be different tm.assert_index_equal(result, expected) mapper = {o: n for o, n in zip(orig_values[:-1], new_values[:-1])} result = cur_index.map(mapper) # Order of categories in result can be different tm.assert_index_equal(result, expected)
def test_map(self): ci = CategoricalIndex(list("ABABC"), categories=list("CBA"), ordered=True) result = ci.map(lambda x: x.lower()) exp = CategoricalIndex(list("ababc"), categories=list("cba"), ordered=True) tm.assert_index_equal(result, exp) ci = CategoricalIndex(list("ABABC"), categories=list("BAC"), ordered=False, name="XXX") result = ci.map(lambda x: x.lower()) exp = CategoricalIndex(list("ababc"), categories=list("bac"), ordered=False, name="XXX") tm.assert_index_equal(result, exp) # GH 12766: Return an index not an array tm.assert_index_equal( ci.map(lambda x: 1), Index(np.array([1] * 5, dtype=np.int64), name="XXX")) # change categories dtype ci = CategoricalIndex(list("ABABC"), categories=list("BAC"), ordered=False) def f(x): return {"A": 10, "B": 20, "C": 30}.get(x) result = ci.map(f) exp = CategoricalIndex([10, 20, 10, 20, 30], categories=[20, 10, 30], ordered=False) tm.assert_index_equal(result, exp) result = ci.map(Series([10, 20, 30], index=["A", "B", "C"])) tm.assert_index_equal(result, exp) result = ci.map({"A": 10, "B": 20, "C": 30}) tm.assert_index_equal(result, exp)