test_formats.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import numpy as np
  2. import pytest
  3. import pandas._config.config as cf
  4. from pandas import Index
  5. class TestIndexRendering:
  6. @pytest.mark.parametrize(
  7. "index,expected",
  8. [
  9. # ASCII
  10. # short
  11. (
  12. Index(["a", "bb", "ccc"]),
  13. """Index(['a', 'bb', 'ccc'], dtype='object')""",
  14. ),
  15. # multiple lines
  16. (
  17. Index(["a", "bb", "ccc"] * 10),
  18. "Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', "
  19. "'bb', 'ccc', 'a', 'bb', 'ccc',\n"
  20. " 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', "
  21. "'bb', 'ccc', 'a', 'bb', 'ccc',\n"
  22. " 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n"
  23. " dtype='object')",
  24. ),
  25. # truncated
  26. (
  27. Index(["a", "bb", "ccc"] * 100),
  28. "Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',\n"
  29. " ...\n"
  30. " 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n"
  31. " dtype='object', length=300)",
  32. ),
  33. # Non-ASCII
  34. # short
  35. (
  36. Index(["あ", "いい", "ううう"]),
  37. """Index(['あ', 'いい', 'ううう'], dtype='object')""",
  38. ),
  39. # multiple lines
  40. (
  41. Index(["あ", "いい", "ううう"] * 10),
  42. (
  43. "Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
  44. "'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
  45. " 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
  46. "'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
  47. " 'あ', 'いい', 'ううう', 'あ', 'いい', "
  48. "'ううう'],\n"
  49. " dtype='object')"
  50. ),
  51. ),
  52. # truncated
  53. (
  54. Index(["あ", "いい", "ううう"] * 100),
  55. (
  56. "Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
  57. "'あ', 'いい', 'ううう', 'あ',\n"
  58. " ...\n"
  59. " 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', "
  60. "'ううう', 'あ', 'いい', 'ううう'],\n"
  61. " dtype='object', length=300)"
  62. ),
  63. ),
  64. ],
  65. )
  66. def test_string_index_repr(self, index, expected):
  67. result = repr(index)
  68. assert result == expected
  69. @pytest.mark.parametrize(
  70. "index,expected",
  71. [
  72. # short
  73. (
  74. Index(["あ", "いい", "ううう"]),
  75. ("Index(['あ', 'いい', 'ううう'], dtype='object')"),
  76. ),
  77. # multiple lines
  78. (
  79. Index(["あ", "いい", "ううう"] * 10),
  80. (
  81. "Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
  82. "'ううう', 'あ', 'いい', 'ううう',\n"
  83. " 'あ', 'いい', 'ううう', 'あ', 'いい', "
  84. "'ううう', 'あ', 'いい', 'ううう',\n"
  85. " 'あ', 'いい', 'ううう', 'あ', 'いい', "
  86. "'ううう', 'あ', 'いい', 'ううう',\n"
  87. " 'あ', 'いい', 'ううう'],\n"
  88. " dtype='object')"
  89. ""
  90. ),
  91. ),
  92. # truncated
  93. (
  94. Index(["あ", "いい", "ううう"] * 100),
  95. (
  96. "Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
  97. "'ううう', 'あ', 'いい', 'ううう',\n"
  98. " 'あ',\n"
  99. " ...\n"
  100. " 'ううう', 'あ', 'いい', 'ううう', 'あ', "
  101. "'いい', 'ううう', 'あ', 'いい',\n"
  102. " 'ううう'],\n"
  103. " dtype='object', length=300)"
  104. ),
  105. ),
  106. ],
  107. )
  108. def test_string_index_repr_with_unicode_option(self, index, expected):
  109. # Enable Unicode option -----------------------------------------
  110. with cf.option_context("display.unicode.east_asian_width", True):
  111. result = repr(index)
  112. assert result == expected
  113. def test_repr_summary(self):
  114. with cf.option_context("display.max_seq_items", 10):
  115. result = repr(Index(np.arange(1000)))
  116. assert len(result) < 200
  117. assert "..." in result
  118. def test_summary_bug(self):
  119. # GH#3869
  120. ind = Index(["{other}%s", "~:{range}:0"], name="A")
  121. result = ind._summary()
  122. # shouldn't be formatted accidentally.
  123. assert "~:{range}:0" in result
  124. assert "{other}%s" in result
  125. def test_index_repr_bool_nan(self):
  126. # GH32146
  127. arr = Index([True, False, np.nan], dtype=object)
  128. exp1 = arr.format()
  129. out1 = ["True", "False", "NaN"]
  130. assert out1 == exp1
  131. exp2 = repr(arr)
  132. out2 = "Index([True, False, nan], dtype='object')"
  133. assert out2 == exp2
  134. def test_format_different_scalar_lengths(self):
  135. # GH#35439
  136. idx = Index(["aaaaaaaaa", "b"])
  137. expected = ["aaaaaaaaa", "b"]
  138. assert idx.format() == expected