test_printing.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import string
  2. import numpy as np
  3. import pandas._config.config as cf
  4. import pandas as pd
  5. from pandas.io.formats import printing
  6. import pandas.io.formats.format as fmt
  7. def test_adjoin():
  8. data = [["a", "b", "c"], ["dd", "ee", "ff"], ["ggg", "hhh", "iii"]]
  9. expected = "a dd ggg\nb ee hhh\nc ff iii"
  10. adjoined = printing.adjoin(2, *data)
  11. assert adjoined == expected
  12. def test_repr_binary_type():
  13. letters = string.ascii_letters
  14. try:
  15. raw = bytes(letters, encoding=cf.get_option("display.encoding"))
  16. except TypeError:
  17. raw = bytes(letters)
  18. b = str(raw.decode("utf-8"))
  19. res = printing.pprint_thing(b, quote_strings=True)
  20. assert res == repr(b)
  21. res = printing.pprint_thing(b, quote_strings=False)
  22. assert res == b
  23. class TestFormattBase:
  24. def test_adjoin(self):
  25. data = [["a", "b", "c"], ["dd", "ee", "ff"], ["ggg", "hhh", "iii"]]
  26. expected = "a dd ggg\nb ee hhh\nc ff iii"
  27. adjoined = printing.adjoin(2, *data)
  28. assert adjoined == expected
  29. def test_adjoin_unicode(self):
  30. data = [["あ", "b", "c"], ["dd", "ええ", "ff"], ["ggg", "hhh", "いいい"]]
  31. expected = "あ dd ggg\nb ええ hhh\nc ff いいい"
  32. adjoined = printing.adjoin(2, *data)
  33. assert adjoined == expected
  34. adj = fmt.EastAsianTextAdjustment()
  35. expected = """あ dd ggg
  36. b ええ hhh
  37. c ff いいい"""
  38. adjoined = adj.adjoin(2, *data)
  39. assert adjoined == expected
  40. cols = adjoined.split("\n")
  41. assert adj.len(cols[0]) == 13
  42. assert adj.len(cols[1]) == 13
  43. assert adj.len(cols[2]) == 16
  44. expected = """あ dd ggg
  45. b ええ hhh
  46. c ff いいい"""
  47. adjoined = adj.adjoin(7, *data)
  48. assert adjoined == expected
  49. cols = adjoined.split("\n")
  50. assert adj.len(cols[0]) == 23
  51. assert adj.len(cols[1]) == 23
  52. assert adj.len(cols[2]) == 26
  53. def test_justify(self):
  54. adj = fmt.EastAsianTextAdjustment()
  55. def just(x, *args, **kwargs):
  56. # wrapper to test single str
  57. return adj.justify([x], *args, **kwargs)[0]
  58. assert just("abc", 5, mode="left") == "abc "
  59. assert just("abc", 5, mode="center") == " abc "
  60. assert just("abc", 5, mode="right") == " abc"
  61. assert just("abc", 5, mode="left") == "abc "
  62. assert just("abc", 5, mode="center") == " abc "
  63. assert just("abc", 5, mode="right") == " abc"
  64. assert just("パンダ", 5, mode="left") == "パンダ"
  65. assert just("パンダ", 5, mode="center") == "パンダ"
  66. assert just("パンダ", 5, mode="right") == "パンダ"
  67. assert just("パンダ", 10, mode="left") == "パンダ "
  68. assert just("パンダ", 10, mode="center") == " パンダ "
  69. assert just("パンダ", 10, mode="right") == " パンダ"
  70. def test_east_asian_len(self):
  71. adj = fmt.EastAsianTextAdjustment()
  72. assert adj.len("abc") == 3
  73. assert adj.len("abc") == 3
  74. assert adj.len("パンダ") == 6
  75. assert adj.len("パンダ") == 5
  76. assert adj.len("パンダpanda") == 11
  77. assert adj.len("パンダpanda") == 10
  78. def test_ambiguous_width(self):
  79. adj = fmt.EastAsianTextAdjustment()
  80. assert adj.len("¡¡ab") == 4
  81. with cf.option_context("display.unicode.ambiguous_as_wide", True):
  82. adj = fmt.EastAsianTextAdjustment()
  83. assert adj.len("¡¡ab") == 6
  84. data = [["あ", "b", "c"], ["dd", "ええ", "ff"], ["ggg", "¡¡ab", "いいい"]]
  85. expected = "あ dd ggg \nb ええ ¡¡ab\nc ff いいい"
  86. adjoined = adj.adjoin(2, *data)
  87. assert adjoined == expected
  88. class TestTableSchemaRepr:
  89. def test_publishes(self, ip):
  90. ipython = ip.instance(config=ip.config)
  91. df = pd.DataFrame({"A": [1, 2]})
  92. objects = [df["A"], df] # dataframe / series
  93. expected_keys = [
  94. {"text/plain", "application/vnd.dataresource+json"},
  95. {"text/plain", "text/html", "application/vnd.dataresource+json"},
  96. ]
  97. opt = pd.option_context("display.html.table_schema", True)
  98. last_obj = None
  99. for obj, expected in zip(objects, expected_keys):
  100. last_obj = obj
  101. with opt:
  102. formatted = ipython.display_formatter.format(obj)
  103. assert set(formatted[0].keys()) == expected
  104. with_latex = pd.option_context("styler.render.repr", "latex")
  105. with opt, with_latex:
  106. formatted = ipython.display_formatter.format(last_obj)
  107. expected = {
  108. "text/plain",
  109. "text/html",
  110. "text/latex",
  111. "application/vnd.dataresource+json",
  112. }
  113. assert set(formatted[0].keys()) == expected
  114. def test_publishes_not_implemented(self, ip):
  115. # column MultiIndex
  116. # GH 15996
  117. midx = pd.MultiIndex.from_product([["A", "B"], ["a", "b", "c"]])
  118. df = pd.DataFrame(np.random.randn(5, len(midx)), columns=midx)
  119. opt = pd.option_context("display.html.table_schema", True)
  120. with opt:
  121. formatted = ip.instance(config=ip.config).display_formatter.format(df)
  122. expected = {"text/plain", "text/html"}
  123. assert set(formatted[0].keys()) == expected
  124. def test_config_on(self):
  125. df = pd.DataFrame({"A": [1, 2]})
  126. with pd.option_context("display.html.table_schema", True):
  127. result = df._repr_data_resource_()
  128. assert result is not None
  129. def test_config_default_off(self):
  130. df = pd.DataFrame({"A": [1, 2]})
  131. with pd.option_context("display.html.table_schema", False):
  132. result = df._repr_data_resource_()
  133. assert result is None
  134. def test_enable_data_resource_formatter(self, ip):
  135. # GH 10491
  136. formatters = ip.instance(config=ip.config).display_formatter.formatters
  137. mimetype = "application/vnd.dataresource+json"
  138. with pd.option_context("display.html.table_schema", True):
  139. assert "application/vnd.dataresource+json" in formatters
  140. assert formatters[mimetype].enabled
  141. # still there, just disabled
  142. assert "application/vnd.dataresource+json" in formatters
  143. assert not formatters[mimetype].enabled
  144. # able to re-set
  145. with pd.option_context("display.html.table_schema", True):
  146. assert "application/vnd.dataresource+json" in formatters
  147. assert formatters[mimetype].enabled
  148. # smoke test that it works
  149. ip.instance(config=ip.config).display_formatter.format(cf)