test_format.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. import warnings
  3. import pytest
  4. from pandas.compat import PY3, range, u
  5. import pandas as pd
  6. from pandas import MultiIndex, compat
  7. import pandas.util.testing as tm
  8. def test_dtype_str(indices):
  9. dtype = indices.dtype_str
  10. assert isinstance(dtype, compat.string_types)
  11. assert dtype == str(indices.dtype)
  12. def test_format(idx):
  13. idx.format()
  14. idx[:0].format()
  15. def test_format_integer_names():
  16. index = MultiIndex(levels=[[0, 1], [0, 1]],
  17. codes=[[0, 0, 1, 1], [0, 1, 0, 1]], names=[0, 1])
  18. index.format(names=True)
  19. def test_format_sparse_config(idx):
  20. warn_filters = warnings.filters
  21. warnings.filterwarnings('ignore', category=FutureWarning,
  22. module=".*format")
  23. # GH1538
  24. pd.set_option('display.multi_sparse', False)
  25. result = idx.format()
  26. assert result[1] == 'foo two'
  27. tm.reset_display_options()
  28. warnings.filters = warn_filters
  29. def test_format_sparse_display():
  30. index = MultiIndex(levels=[[0, 1], [0, 1], [0, 1], [0]],
  31. codes=[[0, 0, 0, 1, 1, 1], [0, 0, 1, 0, 0, 1],
  32. [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0]])
  33. result = index.format()
  34. assert result[3] == '1 0 0 0'
  35. def test_repr_with_unicode_data():
  36. with pd.core.config.option_context("display.encoding", 'UTF-8'):
  37. d = {"a": [u("\u05d0"), 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}
  38. index = pd.DataFrame(d).set_index(["a", "b"]).index
  39. assert "\\u" not in repr(index) # we don't want unicode-escaped
  40. @pytest.mark.skip(reason="#22511 will remove this test")
  41. def test_repr_roundtrip():
  42. mi = MultiIndex.from_product([list('ab'), range(3)],
  43. names=['first', 'second'])
  44. str(mi)
  45. if PY3:
  46. tm.assert_index_equal(eval(repr(mi)), mi, exact=True)
  47. else:
  48. result = eval(repr(mi))
  49. # string coerces to unicode
  50. tm.assert_index_equal(result, mi, exact=False)
  51. assert mi.get_level_values('first').inferred_type == 'string'
  52. assert result.get_level_values('first').inferred_type == 'unicode'
  53. mi_u = MultiIndex.from_product(
  54. [list(u'ab'), range(3)], names=['first', 'second'])
  55. result = eval(repr(mi_u))
  56. tm.assert_index_equal(result, mi_u, exact=True)
  57. # formatting
  58. if PY3:
  59. str(mi)
  60. else:
  61. compat.text_type(mi)
  62. # long format
  63. mi = MultiIndex.from_product([list('abcdefg'), range(10)],
  64. names=['first', 'second'])
  65. if PY3:
  66. tm.assert_index_equal(eval(repr(mi)), mi, exact=True)
  67. else:
  68. result = eval(repr(mi))
  69. # string coerces to unicode
  70. tm.assert_index_equal(result, mi, exact=False)
  71. assert mi.get_level_values('first').inferred_type == 'string'
  72. assert result.get_level_values('first').inferred_type == 'unicode'
  73. result = eval(repr(mi_u))
  74. tm.assert_index_equal(result, mi_u, exact=True)
  75. def test_unicode_string_with_unicode():
  76. d = {"a": [u("\u05d0"), 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}
  77. idx = pd.DataFrame(d).set_index(["a", "b"]).index
  78. if PY3:
  79. str(idx)
  80. else:
  81. compat.text_type(idx)
  82. def test_bytestring_with_unicode():
  83. d = {"a": [u("\u05d0"), 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}
  84. idx = pd.DataFrame(d).set_index(["a", "b"]).index
  85. if PY3:
  86. bytes(idx)
  87. else:
  88. str(idx)
  89. def test_repr_max_seq_item_setting(idx):
  90. # GH10182
  91. idx = idx.repeat(50)
  92. with pd.option_context("display.max_seq_items", None):
  93. repr(idx)
  94. assert '...' not in str(idx)