|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from pandas import ( |
| 6 | + Categorical, |
| 7 | + Index, |
| 8 | + Series, |
| 9 | +) |
| 10 | +import pandas._testing as tm |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(params=[None, "ignore"]) |
| 14 | +def na_action(request): |
| 15 | + return request.param |
| 16 | + |
| 17 | + |
| 18 | +class TestMap: |
| 19 | + @pytest.mark.parametrize( |
| 20 | + "data, categories", |
| 21 | + [ |
| 22 | + (list("abcbca"), list("cab")), |
| 23 | + (pd.interval_range(0, 3).repeat(3), pd.interval_range(0, 3)), |
| 24 | + ], |
| 25 | + ids=["string", "interval"], |
| 26 | + ) |
| 27 | + def test_map_str(self, data, categories, ordered, na_action): |
| 28 | + # GH 31202 - override base class since we want to maintain categorical/ordered |
| 29 | + cat = Categorical(data, categories=categories, ordered=ordered) |
| 30 | + result = cat.map(str, na_action=na_action) |
| 31 | + expected = Categorical( |
| 32 | + map(str, data), categories=map(str, categories), ordered=ordered |
| 33 | + ) |
| 34 | + tm.assert_categorical_equal(result, expected) |
| 35 | + |
| 36 | + def test_map(self, na_action): |
| 37 | + cat = Categorical(list("ABABC"), categories=list("CBA"), ordered=True) |
| 38 | + result = cat.map(lambda x: x.lower(), na_action=na_action) |
| 39 | + exp = Categorical(list("ababc"), categories=list("cba"), ordered=True) |
| 40 | + tm.assert_categorical_equal(result, exp) |
| 41 | + |
| 42 | + cat = Categorical(list("ABABC"), categories=list("BAC"), ordered=False) |
| 43 | + result = cat.map(lambda x: x.lower(), na_action=na_action) |
| 44 | + exp = Categorical(list("ababc"), categories=list("bac"), ordered=False) |
| 45 | + tm.assert_categorical_equal(result, exp) |
| 46 | + |
| 47 | + # GH 12766: Return an index not an array |
| 48 | + result = cat.map(lambda x: 1, na_action=na_action) |
| 49 | + exp = Index(np.array([1] * 5, dtype=np.int64)) |
| 50 | + tm.assert_index_equal(result, exp) |
| 51 | + |
| 52 | + # change categories dtype |
| 53 | + cat = Categorical(list("ABABC"), categories=list("BAC"), ordered=False) |
| 54 | + |
| 55 | + def f(x): |
| 56 | + return {"A": 10, "B": 20, "C": 30}.get(x) |
| 57 | + |
| 58 | + result = cat.map(f, na_action=na_action) |
| 59 | + exp = Categorical([10, 20, 10, 20, 30], categories=[20, 10, 30], ordered=False) |
| 60 | + tm.assert_categorical_equal(result, exp) |
| 61 | + |
| 62 | + mapper = Series([10, 20, 30], index=["A", "B", "C"]) |
| 63 | + result = cat.map(mapper, na_action=na_action) |
| 64 | + tm.assert_categorical_equal(result, exp) |
| 65 | + |
| 66 | + result = cat.map({"A": 10, "B": 20, "C": 30}, na_action=na_action) |
| 67 | + tm.assert_categorical_equal(result, exp) |
| 68 | + |
| 69 | + @pytest.mark.parametrize( |
| 70 | + ("data", "f", "expected"), |
| 71 | + ( |
| 72 | + ([1, 1, np.nan], pd.isna, Index([False, False, True])), |
| 73 | + ([1, 2, np.nan], pd.isna, Index([False, False, True])), |
| 74 | + ([1, 1, np.nan], {1: False}, Categorical([False, False, np.nan])), |
| 75 | + ([1, 2, np.nan], {1: False, 2: False}, Index([False, False, np.nan])), |
| 76 | + ( |
| 77 | + [1, 1, np.nan], |
| 78 | + Series([False, False]), |
| 79 | + Categorical([False, False, np.nan]), |
| 80 | + ), |
| 81 | + ( |
| 82 | + [1, 2, np.nan], |
| 83 | + Series([False] * 3), |
| 84 | + Index([False, False, np.nan]), |
| 85 | + ), |
| 86 | + ), |
| 87 | + ) |
| 88 | + def test_map_with_nan_none(self, data, f, expected): # GH 24241 |
| 89 | + values = Categorical(data) |
| 90 | + result = values.map(f, na_action=None) |
| 91 | + if isinstance(expected, Categorical): |
| 92 | + tm.assert_categorical_equal(result, expected) |
| 93 | + else: |
| 94 | + tm.assert_index_equal(result, expected) |
| 95 | + |
| 96 | + @pytest.mark.parametrize( |
| 97 | + ("data", "f", "expected"), |
| 98 | + ( |
| 99 | + ([1, 1, np.nan], pd.isna, Categorical([False, False, np.nan])), |
| 100 | + ([1, 2, np.nan], pd.isna, Index([False, False, np.nan])), |
| 101 | + ([1, 1, np.nan], {1: False}, Categorical([False, False, np.nan])), |
| 102 | + ([1, 2, np.nan], {1: False, 2: False}, Index([False, False, np.nan])), |
| 103 | + ( |
| 104 | + [1, 1, np.nan], |
| 105 | + Series([False, False]), |
| 106 | + Categorical([False, False, np.nan]), |
| 107 | + ), |
| 108 | + ( |
| 109 | + [1, 2, np.nan], |
| 110 | + Series([False, False, False]), |
| 111 | + Index([False, False, np.nan]), |
| 112 | + ), |
| 113 | + ), |
| 114 | + ) |
| 115 | + def test_map_with_nan_ignore(self, data, f, expected): # GH 24241 |
| 116 | + values = Categorical(data) |
| 117 | + result = values.map(f, na_action="ignore") |
| 118 | + if data[1] == 1: |
| 119 | + tm.assert_categorical_equal(result, expected) |
| 120 | + else: |
| 121 | + tm.assert_index_equal(result, expected) |
| 122 | + |
| 123 | + def test_map_with_dict_or_series(self, na_action): |
| 124 | + orig_values = ["a", "B", 1, "a"] |
| 125 | + new_values = ["one", 2, 3.0, "one"] |
| 126 | + cat = Categorical(orig_values) |
| 127 | + |
| 128 | + mapper = Series(new_values[:-1], index=orig_values[:-1]) |
| 129 | + result = cat.map(mapper, na_action=na_action) |
| 130 | + |
| 131 | + # Order of categories in result can be different |
| 132 | + expected = Categorical(new_values, categories=[3.0, 2, "one"]) |
| 133 | + tm.assert_categorical_equal(result, expected) |
| 134 | + |
| 135 | + mapper = dict(zip(orig_values[:-1], new_values[:-1])) |
| 136 | + result = cat.map(mapper, na_action=na_action) |
| 137 | + # Order of categories in result can be different |
| 138 | + tm.assert_categorical_equal(result, expected) |
0 commit comments