test_map.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. from pandas import (
  5. CategoricalIndex,
  6. Index,
  7. Series,
  8. )
  9. import pandas._testing as tm
  10. class TestMap:
  11. @pytest.mark.parametrize(
  12. "data, categories",
  13. [
  14. (list("abcbca"), list("cab")),
  15. (pd.interval_range(0, 3).repeat(3), pd.interval_range(0, 3)),
  16. ],
  17. ids=["string", "interval"],
  18. )
  19. def test_map_str(self, data, categories, ordered):
  20. # GH 31202 - override base class since we want to maintain categorical/ordered
  21. index = CategoricalIndex(data, categories=categories, ordered=ordered)
  22. result = index.map(str)
  23. expected = CategoricalIndex(
  24. map(str, data), categories=map(str, categories), ordered=ordered
  25. )
  26. tm.assert_index_equal(result, expected)
  27. def test_map(self):
  28. ci = CategoricalIndex(list("ABABC"), categories=list("CBA"), ordered=True)
  29. result = ci.map(lambda x: x.lower())
  30. exp = CategoricalIndex(list("ababc"), categories=list("cba"), ordered=True)
  31. tm.assert_index_equal(result, exp)
  32. ci = CategoricalIndex(
  33. list("ABABC"), categories=list("BAC"), ordered=False, name="XXX"
  34. )
  35. result = ci.map(lambda x: x.lower())
  36. exp = CategoricalIndex(
  37. list("ababc"), categories=list("bac"), ordered=False, name="XXX"
  38. )
  39. tm.assert_index_equal(result, exp)
  40. # GH 12766: Return an index not an array
  41. tm.assert_index_equal(
  42. ci.map(lambda x: 1), Index(np.array([1] * 5, dtype=np.int64), name="XXX")
  43. )
  44. # change categories dtype
  45. ci = CategoricalIndex(list("ABABC"), categories=list("BAC"), ordered=False)
  46. def f(x):
  47. return {"A": 10, "B": 20, "C": 30}.get(x)
  48. result = ci.map(f)
  49. exp = CategoricalIndex(
  50. [10, 20, 10, 20, 30], categories=[20, 10, 30], ordered=False
  51. )
  52. tm.assert_index_equal(result, exp)
  53. result = ci.map(Series([10, 20, 30], index=["A", "B", "C"]))
  54. tm.assert_index_equal(result, exp)
  55. result = ci.map({"A": 10, "B": 20, "C": 30})
  56. tm.assert_index_equal(result, exp)
  57. def test_map_with_categorical_series(self):
  58. # GH 12756
  59. a = Index([1, 2, 3, 4])
  60. b = Series(["even", "odd", "even", "odd"], dtype="category")
  61. c = Series(["even", "odd", "even", "odd"])
  62. exp = CategoricalIndex(["odd", "even", "odd", np.nan])
  63. tm.assert_index_equal(a.map(b), exp)
  64. exp = Index(["odd", "even", "odd", np.nan])
  65. tm.assert_index_equal(a.map(c), exp)
  66. @pytest.mark.parametrize(
  67. ("data", "f"),
  68. (
  69. ([1, 1, np.nan], pd.isna),
  70. ([1, 2, np.nan], pd.isna),
  71. ([1, 1, np.nan], {1: False}),
  72. ([1, 2, np.nan], {1: False, 2: False}),
  73. ([1, 1, np.nan], Series([False, False])),
  74. ([1, 2, np.nan], Series([False, False, False])),
  75. ),
  76. )
  77. def test_map_with_nan(self, data, f): # GH 24241
  78. values = pd.Categorical(data)
  79. result = values.map(f)
  80. if data[1] == 1:
  81. expected = pd.Categorical([False, False, np.nan])
  82. tm.assert_categorical_equal(result, expected)
  83. else:
  84. expected = Index([False, False, np.nan])
  85. tm.assert_index_equal(result, expected)
  86. def test_map_with_dict_or_series(self):
  87. orig_values = ["a", "B", 1, "a"]
  88. new_values = ["one", 2, 3.0, "one"]
  89. cur_index = CategoricalIndex(orig_values, name="XXX")
  90. expected = CategoricalIndex(new_values, name="XXX", categories=[3.0, 2, "one"])
  91. mapper = Series(new_values[:-1], index=orig_values[:-1])
  92. result = cur_index.map(mapper)
  93. # Order of categories in result can be different
  94. tm.assert_index_equal(result, expected)
  95. mapper = dict(zip(orig_values[:-1], new_values[:-1]))
  96. result = cur_index.map(mapper)
  97. # Order of categories in result can be different
  98. tm.assert_index_equal(result, expected)