test_monotonic.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import numpy as np
  2. import pytest
  3. from pandas import (
  4. Index,
  5. MultiIndex,
  6. )
  7. def test_is_monotonic_increasing_lexsorted(lexsorted_two_level_string_multiindex):
  8. # string ordering
  9. mi = lexsorted_two_level_string_multiindex
  10. assert mi.is_monotonic_increasing is False
  11. assert Index(mi.values).is_monotonic_increasing is False
  12. assert mi._is_strictly_monotonic_increasing is False
  13. assert Index(mi.values)._is_strictly_monotonic_increasing is False
  14. def test_is_monotonic_increasing():
  15. i = MultiIndex.from_product([np.arange(10), np.arange(10)], names=["one", "two"])
  16. assert i.is_monotonic_increasing is True
  17. assert i._is_strictly_monotonic_increasing is True
  18. assert Index(i.values).is_monotonic_increasing is True
  19. assert i._is_strictly_monotonic_increasing is True
  20. i = MultiIndex.from_product(
  21. [np.arange(10, 0, -1), np.arange(10)], names=["one", "two"]
  22. )
  23. assert i.is_monotonic_increasing is False
  24. assert i._is_strictly_monotonic_increasing is False
  25. assert Index(i.values).is_monotonic_increasing is False
  26. assert Index(i.values)._is_strictly_monotonic_increasing is False
  27. i = MultiIndex.from_product(
  28. [np.arange(10), np.arange(10, 0, -1)], names=["one", "two"]
  29. )
  30. assert i.is_monotonic_increasing is False
  31. assert i._is_strictly_monotonic_increasing is False
  32. assert Index(i.values).is_monotonic_increasing is False
  33. assert Index(i.values)._is_strictly_monotonic_increasing is False
  34. i = MultiIndex.from_product([[1.0, np.nan, 2.0], ["a", "b", "c"]])
  35. assert i.is_monotonic_increasing is False
  36. assert i._is_strictly_monotonic_increasing is False
  37. assert Index(i.values).is_monotonic_increasing is False
  38. assert Index(i.values)._is_strictly_monotonic_increasing is False
  39. i = MultiIndex(
  40. levels=[["bar", "baz", "foo", "qux"], ["mom", "next", "zenith"]],
  41. codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
  42. names=["first", "second"],
  43. )
  44. assert i.is_monotonic_increasing is True
  45. assert Index(i.values).is_monotonic_increasing is True
  46. assert i._is_strictly_monotonic_increasing is True
  47. assert Index(i.values)._is_strictly_monotonic_increasing is True
  48. # mixed levels, hits the TypeError
  49. i = MultiIndex(
  50. levels=[
  51. [1, 2, 3, 4],
  52. [
  53. "gb00b03mlx29",
  54. "lu0197800237",
  55. "nl0000289783",
  56. "nl0000289965",
  57. "nl0000301109",
  58. ],
  59. ],
  60. codes=[[0, 1, 1, 2, 2, 2, 3], [4, 2, 0, 0, 1, 3, -1]],
  61. names=["household_id", "asset_id"],
  62. )
  63. assert i.is_monotonic_increasing is False
  64. assert i._is_strictly_monotonic_increasing is False
  65. # empty
  66. i = MultiIndex.from_arrays([[], []])
  67. assert i.is_monotonic_increasing is True
  68. assert Index(i.values).is_monotonic_increasing is True
  69. assert i._is_strictly_monotonic_increasing is True
  70. assert Index(i.values)._is_strictly_monotonic_increasing is True
  71. def test_is_monotonic_decreasing():
  72. i = MultiIndex.from_product(
  73. [np.arange(9, -1, -1), np.arange(9, -1, -1)], names=["one", "two"]
  74. )
  75. assert i.is_monotonic_decreasing is True
  76. assert i._is_strictly_monotonic_decreasing is True
  77. assert Index(i.values).is_monotonic_decreasing is True
  78. assert i._is_strictly_monotonic_decreasing is True
  79. i = MultiIndex.from_product(
  80. [np.arange(10), np.arange(10, 0, -1)], names=["one", "two"]
  81. )
  82. assert i.is_monotonic_decreasing is False
  83. assert i._is_strictly_monotonic_decreasing is False
  84. assert Index(i.values).is_monotonic_decreasing is False
  85. assert Index(i.values)._is_strictly_monotonic_decreasing is False
  86. i = MultiIndex.from_product(
  87. [np.arange(10, 0, -1), np.arange(10)], names=["one", "two"]
  88. )
  89. assert i.is_monotonic_decreasing is False
  90. assert i._is_strictly_monotonic_decreasing is False
  91. assert Index(i.values).is_monotonic_decreasing is False
  92. assert Index(i.values)._is_strictly_monotonic_decreasing is False
  93. i = MultiIndex.from_product([[2.0, np.nan, 1.0], ["c", "b", "a"]])
  94. assert i.is_monotonic_decreasing is False
  95. assert i._is_strictly_monotonic_decreasing is False
  96. assert Index(i.values).is_monotonic_decreasing is False
  97. assert Index(i.values)._is_strictly_monotonic_decreasing is False
  98. # string ordering
  99. i = MultiIndex(
  100. levels=[["qux", "foo", "baz", "bar"], ["three", "two", "one"]],
  101. codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
  102. names=["first", "second"],
  103. )
  104. assert i.is_monotonic_decreasing is False
  105. assert Index(i.values).is_monotonic_decreasing is False
  106. assert i._is_strictly_monotonic_decreasing is False
  107. assert Index(i.values)._is_strictly_monotonic_decreasing is False
  108. i = MultiIndex(
  109. levels=[["qux", "foo", "baz", "bar"], ["zenith", "next", "mom"]],
  110. codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
  111. names=["first", "second"],
  112. )
  113. assert i.is_monotonic_decreasing is True
  114. assert Index(i.values).is_monotonic_decreasing is True
  115. assert i._is_strictly_monotonic_decreasing is True
  116. assert Index(i.values)._is_strictly_monotonic_decreasing is True
  117. # mixed levels, hits the TypeError
  118. i = MultiIndex(
  119. levels=[
  120. [4, 3, 2, 1],
  121. [
  122. "nl0000301109",
  123. "nl0000289965",
  124. "nl0000289783",
  125. "lu0197800237",
  126. "gb00b03mlx29",
  127. ],
  128. ],
  129. codes=[[0, 1, 1, 2, 2, 2, 3], [4, 2, 0, 0, 1, 3, -1]],
  130. names=["household_id", "asset_id"],
  131. )
  132. assert i.is_monotonic_decreasing is False
  133. assert i._is_strictly_monotonic_decreasing is False
  134. # empty
  135. i = MultiIndex.from_arrays([[], []])
  136. assert i.is_monotonic_decreasing is True
  137. assert Index(i.values).is_monotonic_decreasing is True
  138. assert i._is_strictly_monotonic_decreasing is True
  139. assert Index(i.values)._is_strictly_monotonic_decreasing is True
  140. def test_is_strictly_monotonic_increasing():
  141. idx = MultiIndex(
  142. levels=[["bar", "baz"], ["mom", "next"]], codes=[[0, 0, 1, 1], [0, 0, 0, 1]]
  143. )
  144. assert idx.is_monotonic_increasing is True
  145. assert idx._is_strictly_monotonic_increasing is False
  146. def test_is_strictly_monotonic_decreasing():
  147. idx = MultiIndex(
  148. levels=[["baz", "bar"], ["next", "mom"]], codes=[[0, 0, 1, 1], [0, 0, 0, 1]]
  149. )
  150. assert idx.is_monotonic_decreasing is True
  151. assert idx._is_strictly_monotonic_decreasing is False
  152. @pytest.mark.parametrize("attr", ["is_monotonic_increasing", "is_monotonic_decreasing"])
  153. @pytest.mark.parametrize(
  154. "values",
  155. [[(np.nan,), (1,), (2,)], [(1,), (np.nan,), (2,)], [(1,), (2,), (np.nan,)]],
  156. )
  157. def test_is_monotonic_with_nans(values, attr):
  158. # GH: 37220
  159. idx = MultiIndex.from_tuples(values, names=["test"])
  160. assert getattr(idx, attr) is False