test_printing.py 6.8 KB

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