test_repr.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. from pandas.compat import PY3, u
  4. from pandas import (
  5. Categorical, CategoricalIndex, Series, date_range, period_range,
  6. timedelta_range)
  7. from pandas.core.config import option_context
  8. from pandas.tests.arrays.categorical.common import TestCategorical
  9. class TestCategoricalReprWithFactor(TestCategorical):
  10. def test_print(self):
  11. expected = ["[a, b, b, a, a, c, c, c]",
  12. "Categories (3, object): [a < b < c]"]
  13. expected = "\n".join(expected)
  14. actual = repr(self.factor)
  15. assert actual == expected
  16. class TestCategoricalRepr(object):
  17. def test_big_print(self):
  18. factor = Categorical([0, 1, 2, 0, 1, 2] * 100, ['a', 'b', 'c'],
  19. fastpath=True)
  20. expected = ["[a, b, c, a, b, ..., b, c, a, b, c]", "Length: 600",
  21. "Categories (3, object): [a, b, c]"]
  22. expected = "\n".join(expected)
  23. actual = repr(factor)
  24. assert actual == expected
  25. def test_empty_print(self):
  26. factor = Categorical([], ["a", "b", "c"])
  27. expected = ("[], Categories (3, object): [a, b, c]")
  28. actual = repr(factor)
  29. assert actual == expected
  30. assert expected == actual
  31. factor = Categorical([], ["a", "b", "c"], ordered=True)
  32. expected = ("[], Categories (3, object): [a < b < c]")
  33. actual = repr(factor)
  34. assert expected == actual
  35. factor = Categorical([], [])
  36. expected = ("[], Categories (0, object): []")
  37. assert expected == repr(factor)
  38. def test_print_none_width(self):
  39. # GH10087
  40. a = Series(Categorical([1, 2, 3, 4]))
  41. exp = u("0 1\n1 2\n2 3\n3 4\n" +
  42. "dtype: category\nCategories (4, int64): [1, 2, 3, 4]")
  43. with option_context("display.width", None):
  44. assert exp == repr(a)
  45. def test_unicode_print(self):
  46. if PY3:
  47. _rep = repr
  48. else:
  49. _rep = unicode # noqa
  50. c = Categorical(['aaaaa', 'bb', 'cccc'] * 20)
  51. expected = u"""\
  52. [aaaaa, bb, cccc, aaaaa, bb, ..., bb, cccc, aaaaa, bb, cccc]
  53. Length: 60
  54. Categories (3, object): [aaaaa, bb, cccc]"""
  55. assert _rep(c) == expected
  56. c = Categorical([u'ああああ', u'いいいいい', u'ううううううう'] * 20)
  57. expected = u"""\
  58. [ああああ, いいいいい, ううううううう, ああああ, いいいいい, ..., いいいいい, ううううううう, ああああ, いいいいい, ううううううう]
  59. Length: 60
  60. Categories (3, object): [ああああ, いいいいい, ううううううう]""" # noqa
  61. assert _rep(c) == expected
  62. # unicode option should not affect to Categorical, as it doesn't care
  63. # the repr width
  64. with option_context('display.unicode.east_asian_width', True):
  65. c = Categorical([u'ああああ', u'いいいいい', u'ううううううう'] * 20)
  66. expected = u"""[ああああ, いいいいい, ううううううう, ああああ, いいいいい, ..., いいいいい, ううううううう, ああああ, いいいいい, ううううううう]
  67. Length: 60
  68. Categories (3, object): [ああああ, いいいいい, ううううううう]""" # noqa
  69. assert _rep(c) == expected
  70. def test_categorical_repr(self):
  71. c = Categorical([1, 2, 3])
  72. exp = """[1, 2, 3]
  73. Categories (3, int64): [1, 2, 3]"""
  74. assert repr(c) == exp
  75. c = Categorical([1, 2, 3, 1, 2, 3], categories=[1, 2, 3])
  76. exp = """[1, 2, 3, 1, 2, 3]
  77. Categories (3, int64): [1, 2, 3]"""
  78. assert repr(c) == exp
  79. c = Categorical([1, 2, 3, 4, 5] * 10)
  80. exp = """[1, 2, 3, 4, 5, ..., 1, 2, 3, 4, 5]
  81. Length: 50
  82. Categories (5, int64): [1, 2, 3, 4, 5]"""
  83. assert repr(c) == exp
  84. c = Categorical(np.arange(20))
  85. exp = """[0, 1, 2, 3, 4, ..., 15, 16, 17, 18, 19]
  86. Length: 20
  87. Categories (20, int64): [0, 1, 2, 3, ..., 16, 17, 18, 19]"""
  88. assert repr(c) == exp
  89. def test_categorical_repr_ordered(self):
  90. c = Categorical([1, 2, 3], ordered=True)
  91. exp = """[1, 2, 3]
  92. Categories (3, int64): [1 < 2 < 3]"""
  93. assert repr(c) == exp
  94. c = Categorical([1, 2, 3, 1, 2, 3], categories=[1, 2, 3], ordered=True)
  95. exp = """[1, 2, 3, 1, 2, 3]
  96. Categories (3, int64): [1 < 2 < 3]"""
  97. assert repr(c) == exp
  98. c = Categorical([1, 2, 3, 4, 5] * 10, ordered=True)
  99. exp = """[1, 2, 3, 4, 5, ..., 1, 2, 3, 4, 5]
  100. Length: 50
  101. Categories (5, int64): [1 < 2 < 3 < 4 < 5]"""
  102. assert repr(c) == exp
  103. c = Categorical(np.arange(20), ordered=True)
  104. exp = """[0, 1, 2, 3, 4, ..., 15, 16, 17, 18, 19]
  105. Length: 20
  106. Categories (20, int64): [0 < 1 < 2 < 3 ... 16 < 17 < 18 < 19]"""
  107. assert repr(c) == exp
  108. def test_categorical_repr_datetime(self):
  109. idx = date_range('2011-01-01 09:00', freq='H', periods=5)
  110. c = Categorical(idx)
  111. # TODO(wesm): exceeding 80 characters in the console is not good
  112. # behavior
  113. exp = (
  114. "[2011-01-01 09:00:00, 2011-01-01 10:00:00, 2011-01-01 11:00:00, "
  115. "2011-01-01 12:00:00, 2011-01-01 13:00:00]\n"
  116. "Categories (5, datetime64[ns]): [2011-01-01 09:00:00, "
  117. "2011-01-01 10:00:00, 2011-01-01 11:00:00,\n"
  118. " 2011-01-01 12:00:00, "
  119. "2011-01-01 13:00:00]""")
  120. assert repr(c) == exp
  121. c = Categorical(idx.append(idx), categories=idx)
  122. exp = (
  123. "[2011-01-01 09:00:00, 2011-01-01 10:00:00, 2011-01-01 11:00:00, "
  124. "2011-01-01 12:00:00, 2011-01-01 13:00:00, 2011-01-01 09:00:00, "
  125. "2011-01-01 10:00:00, 2011-01-01 11:00:00, 2011-01-01 12:00:00, "
  126. "2011-01-01 13:00:00]\n"
  127. "Categories (5, datetime64[ns]): [2011-01-01 09:00:00, "
  128. "2011-01-01 10:00:00, 2011-01-01 11:00:00,\n"
  129. " 2011-01-01 12:00:00, "
  130. "2011-01-01 13:00:00]")
  131. assert repr(c) == exp
  132. idx = date_range('2011-01-01 09:00', freq='H', periods=5,
  133. tz='US/Eastern')
  134. c = Categorical(idx)
  135. exp = (
  136. "[2011-01-01 09:00:00-05:00, 2011-01-01 10:00:00-05:00, "
  137. "2011-01-01 11:00:00-05:00, 2011-01-01 12:00:00-05:00, "
  138. "2011-01-01 13:00:00-05:00]\n"
  139. "Categories (5, datetime64[ns, US/Eastern]): "
  140. "[2011-01-01 09:00:00-05:00, 2011-01-01 10:00:00-05:00,\n"
  141. " "
  142. "2011-01-01 11:00:00-05:00, 2011-01-01 12:00:00-05:00,\n"
  143. " "
  144. "2011-01-01 13:00:00-05:00]")
  145. assert repr(c) == exp
  146. c = Categorical(idx.append(idx), categories=idx)
  147. exp = (
  148. "[2011-01-01 09:00:00-05:00, 2011-01-01 10:00:00-05:00, "
  149. "2011-01-01 11:00:00-05:00, 2011-01-01 12:00:00-05:00, "
  150. "2011-01-01 13:00:00-05:00, 2011-01-01 09:00:00-05:00, "
  151. "2011-01-01 10:00:00-05:00, 2011-01-01 11:00:00-05:00, "
  152. "2011-01-01 12:00:00-05:00, 2011-01-01 13:00:00-05:00]\n"
  153. "Categories (5, datetime64[ns, US/Eastern]): "
  154. "[2011-01-01 09:00:00-05:00, 2011-01-01 10:00:00-05:00,\n"
  155. " "
  156. "2011-01-01 11:00:00-05:00, 2011-01-01 12:00:00-05:00,\n"
  157. " "
  158. "2011-01-01 13:00:00-05:00]")
  159. assert repr(c) == exp
  160. def test_categorical_repr_datetime_ordered(self):
  161. idx = date_range('2011-01-01 09:00', freq='H', periods=5)
  162. c = Categorical(idx, ordered=True)
  163. exp = """[2011-01-01 09:00:00, 2011-01-01 10:00:00, 2011-01-01 11:00:00, 2011-01-01 12:00:00, 2011-01-01 13:00:00]
  164. Categories (5, datetime64[ns]): [2011-01-01 09:00:00 < 2011-01-01 10:00:00 < 2011-01-01 11:00:00 <
  165. 2011-01-01 12:00:00 < 2011-01-01 13:00:00]""" # noqa
  166. assert repr(c) == exp
  167. c = Categorical(idx.append(idx), categories=idx, ordered=True)
  168. exp = """[2011-01-01 09:00:00, 2011-01-01 10:00:00, 2011-01-01 11:00:00, 2011-01-01 12:00:00, 2011-01-01 13:00:00, 2011-01-01 09:00:00, 2011-01-01 10:00:00, 2011-01-01 11:00:00, 2011-01-01 12:00:00, 2011-01-01 13:00:00]
  169. Categories (5, datetime64[ns]): [2011-01-01 09:00:00 < 2011-01-01 10:00:00 < 2011-01-01 11:00:00 <
  170. 2011-01-01 12:00:00 < 2011-01-01 13:00:00]""" # noqa
  171. assert repr(c) == exp
  172. idx = date_range('2011-01-01 09:00', freq='H', periods=5,
  173. tz='US/Eastern')
  174. c = Categorical(idx, ordered=True)
  175. exp = """[2011-01-01 09:00:00-05:00, 2011-01-01 10:00:00-05:00, 2011-01-01 11:00:00-05:00, 2011-01-01 12:00:00-05:00, 2011-01-01 13:00:00-05:00]
  176. Categories (5, datetime64[ns, US/Eastern]): [2011-01-01 09:00:00-05:00 < 2011-01-01 10:00:00-05:00 <
  177. 2011-01-01 11:00:00-05:00 < 2011-01-01 12:00:00-05:00 <
  178. 2011-01-01 13:00:00-05:00]""" # noqa
  179. assert repr(c) == exp
  180. c = Categorical(idx.append(idx), categories=idx, ordered=True)
  181. exp = """[2011-01-01 09:00:00-05:00, 2011-01-01 10:00:00-05:00, 2011-01-01 11:00:00-05:00, 2011-01-01 12:00:00-05:00, 2011-01-01 13:00:00-05:00, 2011-01-01 09:00:00-05:00, 2011-01-01 10:00:00-05:00, 2011-01-01 11:00:00-05:00, 2011-01-01 12:00:00-05:00, 2011-01-01 13:00:00-05:00]
  182. Categories (5, datetime64[ns, US/Eastern]): [2011-01-01 09:00:00-05:00 < 2011-01-01 10:00:00-05:00 <
  183. 2011-01-01 11:00:00-05:00 < 2011-01-01 12:00:00-05:00 <
  184. 2011-01-01 13:00:00-05:00]""" # noqa
  185. assert repr(c) == exp
  186. def test_categorical_repr_int_with_nan(self):
  187. c = Categorical([1, 2, np.nan])
  188. c_exp = """[1, 2, NaN]\nCategories (2, int64): [1, 2]"""
  189. assert repr(c) == c_exp
  190. s = Series([1, 2, np.nan], dtype="object").astype("category")
  191. s_exp = """0 1\n1 2\n2 NaN
  192. dtype: category
  193. Categories (2, int64): [1, 2]"""
  194. assert repr(s) == s_exp
  195. def test_categorical_repr_period(self):
  196. idx = period_range('2011-01-01 09:00', freq='H', periods=5)
  197. c = Categorical(idx)
  198. exp = """[2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00]
  199. Categories (5, period[H]): [2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00,
  200. 2011-01-01 13:00]""" # noqa
  201. assert repr(c) == exp
  202. c = Categorical(idx.append(idx), categories=idx)
  203. exp = """[2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00, 2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00]
  204. Categories (5, period[H]): [2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00,
  205. 2011-01-01 13:00]""" # noqa
  206. assert repr(c) == exp
  207. idx = period_range('2011-01', freq='M', periods=5)
  208. c = Categorical(idx)
  209. exp = """[2011-01, 2011-02, 2011-03, 2011-04, 2011-05]
  210. Categories (5, period[M]): [2011-01, 2011-02, 2011-03, 2011-04, 2011-05]"""
  211. assert repr(c) == exp
  212. c = Categorical(idx.append(idx), categories=idx)
  213. exp = """[2011-01, 2011-02, 2011-03, 2011-04, 2011-05, 2011-01, 2011-02, 2011-03, 2011-04, 2011-05]
  214. Categories (5, period[M]): [2011-01, 2011-02, 2011-03, 2011-04, 2011-05]""" # noqa
  215. assert repr(c) == exp
  216. def test_categorical_repr_period_ordered(self):
  217. idx = period_range('2011-01-01 09:00', freq='H', periods=5)
  218. c = Categorical(idx, ordered=True)
  219. exp = """[2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00]
  220. Categories (5, period[H]): [2011-01-01 09:00 < 2011-01-01 10:00 < 2011-01-01 11:00 < 2011-01-01 12:00 <
  221. 2011-01-01 13:00]""" # noqa
  222. assert repr(c) == exp
  223. c = Categorical(idx.append(idx), categories=idx, ordered=True)
  224. exp = """[2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00, 2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00]
  225. Categories (5, period[H]): [2011-01-01 09:00 < 2011-01-01 10:00 < 2011-01-01 11:00 < 2011-01-01 12:00 <
  226. 2011-01-01 13:00]""" # noqa
  227. assert repr(c) == exp
  228. idx = period_range('2011-01', freq='M', periods=5)
  229. c = Categorical(idx, ordered=True)
  230. exp = """[2011-01, 2011-02, 2011-03, 2011-04, 2011-05]
  231. Categories (5, period[M]): [2011-01 < 2011-02 < 2011-03 < 2011-04 < 2011-05]"""
  232. assert repr(c) == exp
  233. c = Categorical(idx.append(idx), categories=idx, ordered=True)
  234. exp = """[2011-01, 2011-02, 2011-03, 2011-04, 2011-05, 2011-01, 2011-02, 2011-03, 2011-04, 2011-05]
  235. Categories (5, period[M]): [2011-01 < 2011-02 < 2011-03 < 2011-04 < 2011-05]""" # noqa
  236. assert repr(c) == exp
  237. def test_categorical_repr_timedelta(self):
  238. idx = timedelta_range('1 days', periods=5)
  239. c = Categorical(idx)
  240. exp = """[1 days, 2 days, 3 days, 4 days, 5 days]
  241. Categories (5, timedelta64[ns]): [1 days, 2 days, 3 days, 4 days, 5 days]"""
  242. assert repr(c) == exp
  243. c = Categorical(idx.append(idx), categories=idx)
  244. exp = """[1 days, 2 days, 3 days, 4 days, 5 days, 1 days, 2 days, 3 days, 4 days, 5 days]
  245. Categories (5, timedelta64[ns]): [1 days, 2 days, 3 days, 4 days, 5 days]""" # noqa
  246. assert repr(c) == exp
  247. idx = timedelta_range('1 hours', periods=20)
  248. c = Categorical(idx)
  249. exp = """[0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00, 3 days 01:00:00, 4 days 01:00:00, ..., 15 days 01:00:00, 16 days 01:00:00, 17 days 01:00:00, 18 days 01:00:00, 19 days 01:00:00]
  250. Length: 20
  251. Categories (20, timedelta64[ns]): [0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00,
  252. 3 days 01:00:00, ..., 16 days 01:00:00, 17 days 01:00:00,
  253. 18 days 01:00:00, 19 days 01:00:00]""" # noqa
  254. assert repr(c) == exp
  255. c = Categorical(idx.append(idx), categories=idx)
  256. exp = """[0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00, 3 days 01:00:00, 4 days 01:00:00, ..., 15 days 01:00:00, 16 days 01:00:00, 17 days 01:00:00, 18 days 01:00:00, 19 days 01:00:00]
  257. Length: 40
  258. Categories (20, timedelta64[ns]): [0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00,
  259. 3 days 01:00:00, ..., 16 days 01:00:00, 17 days 01:00:00,
  260. 18 days 01:00:00, 19 days 01:00:00]""" # noqa
  261. assert repr(c) == exp
  262. def test_categorical_repr_timedelta_ordered(self):
  263. idx = timedelta_range('1 days', periods=5)
  264. c = Categorical(idx, ordered=True)
  265. exp = """[1 days, 2 days, 3 days, 4 days, 5 days]
  266. Categories (5, timedelta64[ns]): [1 days < 2 days < 3 days < 4 days < 5 days]""" # noqa
  267. assert repr(c) == exp
  268. c = Categorical(idx.append(idx), categories=idx, ordered=True)
  269. exp = """[1 days, 2 days, 3 days, 4 days, 5 days, 1 days, 2 days, 3 days, 4 days, 5 days]
  270. Categories (5, timedelta64[ns]): [1 days < 2 days < 3 days < 4 days < 5 days]""" # noqa
  271. assert repr(c) == exp
  272. idx = timedelta_range('1 hours', periods=20)
  273. c = Categorical(idx, ordered=True)
  274. exp = """[0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00, 3 days 01:00:00, 4 days 01:00:00, ..., 15 days 01:00:00, 16 days 01:00:00, 17 days 01:00:00, 18 days 01:00:00, 19 days 01:00:00]
  275. Length: 20
  276. Categories (20, timedelta64[ns]): [0 days 01:00:00 < 1 days 01:00:00 < 2 days 01:00:00 <
  277. 3 days 01:00:00 ... 16 days 01:00:00 < 17 days 01:00:00 <
  278. 18 days 01:00:00 < 19 days 01:00:00]""" # noqa
  279. assert repr(c) == exp
  280. c = Categorical(idx.append(idx), categories=idx, ordered=True)
  281. exp = """[0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00, 3 days 01:00:00, 4 days 01:00:00, ..., 15 days 01:00:00, 16 days 01:00:00, 17 days 01:00:00, 18 days 01:00:00, 19 days 01:00:00]
  282. Length: 40
  283. Categories (20, timedelta64[ns]): [0 days 01:00:00 < 1 days 01:00:00 < 2 days 01:00:00 <
  284. 3 days 01:00:00 ... 16 days 01:00:00 < 17 days 01:00:00 <
  285. 18 days 01:00:00 < 19 days 01:00:00]""" # noqa
  286. assert repr(c) == exp
  287. def test_categorical_index_repr(self):
  288. idx = CategoricalIndex(Categorical([1, 2, 3]))
  289. exp = """CategoricalIndex([1, 2, 3], categories=[1, 2, 3], ordered=False, dtype='category')""" # noqa
  290. assert repr(idx) == exp
  291. i = CategoricalIndex(Categorical(np.arange(10)))
  292. exp = """CategoricalIndex([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], categories=[0, 1, 2, 3, 4, 5, 6, 7, ...], ordered=False, dtype='category')""" # noqa
  293. assert repr(i) == exp
  294. def test_categorical_index_repr_ordered(self):
  295. i = CategoricalIndex(Categorical([1, 2, 3], ordered=True))
  296. exp = """CategoricalIndex([1, 2, 3], categories=[1, 2, 3], ordered=True, dtype='category')""" # noqa
  297. assert repr(i) == exp
  298. i = CategoricalIndex(Categorical(np.arange(10), ordered=True))
  299. exp = """CategoricalIndex([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], categories=[0, 1, 2, 3, 4, 5, 6, 7, ...], ordered=True, dtype='category')""" # noqa
  300. assert repr(i) == exp
  301. def test_categorical_index_repr_datetime(self):
  302. idx = date_range('2011-01-01 09:00', freq='H', periods=5)
  303. i = CategoricalIndex(Categorical(idx))
  304. exp = """CategoricalIndex(['2011-01-01 09:00:00', '2011-01-01 10:00:00',
  305. '2011-01-01 11:00:00', '2011-01-01 12:00:00',
  306. '2011-01-01 13:00:00'],
  307. categories=[2011-01-01 09:00:00, 2011-01-01 10:00:00, 2011-01-01 11:00:00, 2011-01-01 12:00:00, 2011-01-01 13:00:00], ordered=False, dtype='category')""" # noqa
  308. assert repr(i) == exp
  309. idx = date_range('2011-01-01 09:00', freq='H', periods=5,
  310. tz='US/Eastern')
  311. i = CategoricalIndex(Categorical(idx))
  312. exp = """CategoricalIndex(['2011-01-01 09:00:00-05:00', '2011-01-01 10:00:00-05:00',
  313. '2011-01-01 11:00:00-05:00', '2011-01-01 12:00:00-05:00',
  314. '2011-01-01 13:00:00-05:00'],
  315. categories=[2011-01-01 09:00:00-05:00, 2011-01-01 10:00:00-05:00, 2011-01-01 11:00:00-05:00, 2011-01-01 12:00:00-05:00, 2011-01-01 13:00:00-05:00], ordered=False, dtype='category')""" # noqa
  316. assert repr(i) == exp
  317. def test_categorical_index_repr_datetime_ordered(self):
  318. idx = date_range('2011-01-01 09:00', freq='H', periods=5)
  319. i = CategoricalIndex(Categorical(idx, ordered=True))
  320. exp = """CategoricalIndex(['2011-01-01 09:00:00', '2011-01-01 10:00:00',
  321. '2011-01-01 11:00:00', '2011-01-01 12:00:00',
  322. '2011-01-01 13:00:00'],
  323. categories=[2011-01-01 09:00:00, 2011-01-01 10:00:00, 2011-01-01 11:00:00, 2011-01-01 12:00:00, 2011-01-01 13:00:00], ordered=True, dtype='category')""" # noqa
  324. assert repr(i) == exp
  325. idx = date_range('2011-01-01 09:00', freq='H', periods=5,
  326. tz='US/Eastern')
  327. i = CategoricalIndex(Categorical(idx, ordered=True))
  328. exp = """CategoricalIndex(['2011-01-01 09:00:00-05:00', '2011-01-01 10:00:00-05:00',
  329. '2011-01-01 11:00:00-05:00', '2011-01-01 12:00:00-05:00',
  330. '2011-01-01 13:00:00-05:00'],
  331. categories=[2011-01-01 09:00:00-05:00, 2011-01-01 10:00:00-05:00, 2011-01-01 11:00:00-05:00, 2011-01-01 12:00:00-05:00, 2011-01-01 13:00:00-05:00], ordered=True, dtype='category')""" # noqa
  332. assert repr(i) == exp
  333. i = CategoricalIndex(Categorical(idx.append(idx), ordered=True))
  334. exp = """CategoricalIndex(['2011-01-01 09:00:00-05:00', '2011-01-01 10:00:00-05:00',
  335. '2011-01-01 11:00:00-05:00', '2011-01-01 12:00:00-05:00',
  336. '2011-01-01 13:00:00-05:00', '2011-01-01 09:00:00-05:00',
  337. '2011-01-01 10:00:00-05:00', '2011-01-01 11:00:00-05:00',
  338. '2011-01-01 12:00:00-05:00', '2011-01-01 13:00:00-05:00'],
  339. categories=[2011-01-01 09:00:00-05:00, 2011-01-01 10:00:00-05:00, 2011-01-01 11:00:00-05:00, 2011-01-01 12:00:00-05:00, 2011-01-01 13:00:00-05:00], ordered=True, dtype='category')""" # noqa
  340. assert repr(i) == exp
  341. def test_categorical_index_repr_period(self):
  342. # test all length
  343. idx = period_range('2011-01-01 09:00', freq='H', periods=1)
  344. i = CategoricalIndex(Categorical(idx))
  345. exp = """CategoricalIndex(['2011-01-01 09:00'], categories=[2011-01-01 09:00], ordered=False, dtype='category')""" # noqa
  346. assert repr(i) == exp
  347. idx = period_range('2011-01-01 09:00', freq='H', periods=2)
  348. i = CategoricalIndex(Categorical(idx))
  349. exp = """CategoricalIndex(['2011-01-01 09:00', '2011-01-01 10:00'], categories=[2011-01-01 09:00, 2011-01-01 10:00], ordered=False, dtype='category')""" # noqa
  350. assert repr(i) == exp
  351. idx = period_range('2011-01-01 09:00', freq='H', periods=3)
  352. i = CategoricalIndex(Categorical(idx))
  353. exp = """CategoricalIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'], categories=[2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00], ordered=False, dtype='category')""" # noqa
  354. assert repr(i) == exp
  355. idx = period_range('2011-01-01 09:00', freq='H', periods=5)
  356. i = CategoricalIndex(Categorical(idx))
  357. exp = """CategoricalIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00',
  358. '2011-01-01 12:00', '2011-01-01 13:00'],
  359. categories=[2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00], ordered=False, dtype='category')""" # noqa
  360. assert repr(i) == exp
  361. i = CategoricalIndex(Categorical(idx.append(idx)))
  362. exp = """CategoricalIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00',
  363. '2011-01-01 12:00', '2011-01-01 13:00', '2011-01-01 09:00',
  364. '2011-01-01 10:00', '2011-01-01 11:00', '2011-01-01 12:00',
  365. '2011-01-01 13:00'],
  366. categories=[2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00], ordered=False, dtype='category')""" # noqa
  367. assert repr(i) == exp
  368. idx = period_range('2011-01', freq='M', periods=5)
  369. i = CategoricalIndex(Categorical(idx))
  370. exp = """CategoricalIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05'], categories=[2011-01, 2011-02, 2011-03, 2011-04, 2011-05], ordered=False, dtype='category')""" # noqa
  371. assert repr(i) == exp
  372. def test_categorical_index_repr_period_ordered(self):
  373. idx = period_range('2011-01-01 09:00', freq='H', periods=5)
  374. i = CategoricalIndex(Categorical(idx, ordered=True))
  375. exp = """CategoricalIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00',
  376. '2011-01-01 12:00', '2011-01-01 13:00'],
  377. categories=[2011-01-01 09:00, 2011-01-01 10:00, 2011-01-01 11:00, 2011-01-01 12:00, 2011-01-01 13:00], ordered=True, dtype='category')""" # noqa
  378. assert repr(i) == exp
  379. idx = period_range('2011-01', freq='M', periods=5)
  380. i = CategoricalIndex(Categorical(idx, ordered=True))
  381. exp = """CategoricalIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05'], categories=[2011-01, 2011-02, 2011-03, 2011-04, 2011-05], ordered=True, dtype='category')""" # noqa
  382. assert repr(i) == exp
  383. def test_categorical_index_repr_timedelta(self):
  384. idx = timedelta_range('1 days', periods=5)
  385. i = CategoricalIndex(Categorical(idx))
  386. exp = """CategoricalIndex(['1 days', '2 days', '3 days', '4 days', '5 days'], categories=[1 days 00:00:00, 2 days 00:00:00, 3 days 00:00:00, 4 days 00:00:00, 5 days 00:00:00], ordered=False, dtype='category')""" # noqa
  387. assert repr(i) == exp
  388. idx = timedelta_range('1 hours', periods=10)
  389. i = CategoricalIndex(Categorical(idx))
  390. exp = """CategoricalIndex(['0 days 01:00:00', '1 days 01:00:00', '2 days 01:00:00',
  391. '3 days 01:00:00', '4 days 01:00:00', '5 days 01:00:00',
  392. '6 days 01:00:00', '7 days 01:00:00', '8 days 01:00:00',
  393. '9 days 01:00:00'],
  394. categories=[0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00, 3 days 01:00:00, 4 days 01:00:00, 5 days 01:00:00, 6 days 01:00:00, 7 days 01:00:00, ...], ordered=False, dtype='category')""" # noqa
  395. assert repr(i) == exp
  396. def test_categorical_index_repr_timedelta_ordered(self):
  397. idx = timedelta_range('1 days', periods=5)
  398. i = CategoricalIndex(Categorical(idx, ordered=True))
  399. exp = """CategoricalIndex(['1 days', '2 days', '3 days', '4 days', '5 days'], categories=[1 days 00:00:00, 2 days 00:00:00, 3 days 00:00:00, 4 days 00:00:00, 5 days 00:00:00], ordered=True, dtype='category')""" # noqa
  400. assert repr(i) == exp
  401. idx = timedelta_range('1 hours', periods=10)
  402. i = CategoricalIndex(Categorical(idx, ordered=True))
  403. exp = """CategoricalIndex(['0 days 01:00:00', '1 days 01:00:00', '2 days 01:00:00',
  404. '3 days 01:00:00', '4 days 01:00:00', '5 days 01:00:00',
  405. '6 days 01:00:00', '7 days 01:00:00', '8 days 01:00:00',
  406. '9 days 01:00:00'],
  407. categories=[0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00, 3 days 01:00:00, 4 days 01:00:00, 5 days 01:00:00, 6 days 01:00:00, 7 days 01:00:00, ...], ordered=True, dtype='category')""" # noqa
  408. assert repr(i) == exp