|
19 | 19 | from pandas.util.py3compat import lzip
|
20 | 20 | import pandas.core.format as fmt
|
21 | 21 | import pandas.util.testing as tm
|
| 22 | +from pandas.util.terminal import get_terminal_size |
22 | 23 | import pandas
|
23 | 24 | import pandas as pd
|
24 | 25 | from pandas.core.config import (set_option, get_option,
|
|
27 | 28 | _frame = DataFrame(tm.getSeriesData())
|
28 | 29 |
|
29 | 30 |
|
| 31 | +def curpath(): |
| 32 | + pth, _ = os.path.split(os.path.abspath(__file__)) |
| 33 | + return pth |
| 34 | + |
| 35 | +def has_info_repr(df): |
| 36 | + r = repr(df) |
| 37 | + return r.split('\n')[0].startswith("<class") |
| 38 | + |
| 39 | +def has_expanded_repr(df): |
| 40 | + r = repr(df) |
| 41 | + for line in r.split('\n'): |
| 42 | + if line.endswith('\\'): |
| 43 | + return True |
| 44 | + return False |
| 45 | + |
| 46 | + |
30 | 47 | class TestDataFrameFormatting(unittest.TestCase):
|
31 | 48 | _multiprocess_can_split_ = True
|
32 | 49 |
|
@@ -139,6 +156,57 @@ def test_repr_no_backslash(self):
|
139 | 156 | df = DataFrame(np.random.randn(10, 4))
|
140 | 157 | self.assertTrue('\\' not in repr(df))
|
141 | 158 |
|
| 159 | + def test_expand_frame_repr(self): |
| 160 | + df_small = DataFrame('hello', [0], [0]) |
| 161 | + df_wide = DataFrame('hello', [0], range(10)) |
| 162 | + |
| 163 | + with option_context('mode.sim_interactive', True): |
| 164 | + with option_context('display.width', 50): |
| 165 | + with option_context('display.expand_frame_repr', True): |
| 166 | + self.assertFalse(has_info_repr(df_small)) |
| 167 | + self.assertFalse(has_expanded_repr(df_small)) |
| 168 | + self.assertFalse(has_info_repr(df_wide)) |
| 169 | + self.assertTrue(has_expanded_repr(df_wide)) |
| 170 | + |
| 171 | + with option_context('display.expand_frame_repr', False): |
| 172 | + self.assertFalse(has_info_repr(df_small)) |
| 173 | + self.assertFalse(has_expanded_repr(df_small)) |
| 174 | + self.assertTrue(has_info_repr(df_wide)) |
| 175 | + self.assertFalse(has_expanded_repr(df_wide)) |
| 176 | + |
| 177 | + def test_repr_max_columns_max_rows(self): |
| 178 | + term_width, term_height = get_terminal_size() |
| 179 | + if term_width < 10 or term_height < 10: |
| 180 | + raise nose.SkipTest |
| 181 | + |
| 182 | + def mkframe(n): |
| 183 | + index = ['%05d' % i for i in range(n)] |
| 184 | + return DataFrame(0, index, index) |
| 185 | + |
| 186 | + with option_context('mode.sim_interactive', True): |
| 187 | + with option_context('display.width', term_width * 2): |
| 188 | + with option_context('display.max_rows', 5, |
| 189 | + 'display.max_columns', 5): |
| 190 | + self.assertFalse(has_expanded_repr(mkframe(4))) |
| 191 | + self.assertFalse(has_expanded_repr(mkframe(5))) |
| 192 | + self.assertFalse(has_expanded_repr(mkframe(6))) |
| 193 | + self.assertTrue(has_info_repr(mkframe(6))) |
| 194 | + |
| 195 | + with option_context('display.max_rows', 20, |
| 196 | + 'display.max_columns', 5): |
| 197 | + # Out off max_columns boundary, but no extending |
| 198 | + # occurs ... can improve? |
| 199 | + self.assertFalse(has_expanded_repr(mkframe(6))) |
| 200 | + self.assertFalse(has_info_repr(mkframe(6))) |
| 201 | + |
| 202 | + with option_context('display.max_columns', 0, |
| 203 | + 'display.max_rows', term_width * 20, |
| 204 | + 'display.width', 0): |
| 205 | + df = mkframe((term_width // 7) - 2) |
| 206 | + self.assertFalse(has_expanded_repr(df)) |
| 207 | + df = mkframe((term_width // 7) + 2) |
| 208 | + self.assertTrue(has_expanded_repr(df)) |
| 209 | + |
142 | 210 | def test_to_string_repr_unicode(self):
|
143 | 211 | buf = StringIO()
|
144 | 212 |
|
@@ -525,19 +593,20 @@ def test_setting(value, nrows=3, ncols=2):
|
525 | 593 | else:
|
526 | 594 | raise ValueError("'value' must be int or None")
|
527 | 595 |
|
528 |
| - pd.set_option('display.max_rows', nrows - 1) |
529 |
| - pd.set_option('display.max_info_rows', value) |
| 596 | + with option_context('mode.sim_interactive', True): |
| 597 | + pd.set_option('display.max_rows', nrows - 1) |
| 598 | + pd.set_option('display.max_info_rows', value) |
530 | 599 |
|
531 |
| - smallx = DataFrame(np.random.rand(nrows, ncols)) |
532 |
| - repr_small = repr(smallx) |
| 600 | + smallx = DataFrame(np.random.rand(nrows, ncols)) |
| 601 | + repr_small = repr(smallx) |
533 | 602 |
|
534 |
| - bigx = DataFrame(np.random.rand(nrows + 1, ncols)) |
535 |
| - repr_big = repr(bigx) |
| 603 | + bigx = DataFrame(np.random.rand(nrows + 1, ncols)) |
| 604 | + repr_big = repr(bigx) |
536 | 605 |
|
537 |
| - diff = len(repr_small.splitlines()) - len(repr_big.splitlines()) |
| 606 | + diff = len(repr_small.splitlines()) - len(repr_big.splitlines()) |
538 | 607 |
|
539 |
| - # the difference in line count is the number of columns |
540 |
| - self.assertEqual(diff, expected_difference) |
| 608 | + # the difference in line count is the number of columns |
| 609 | + self.assertEqual(diff, expected_difference) |
541 | 610 |
|
542 | 611 | test_setting(None)
|
543 | 612 | test_setting(3)
|
@@ -691,7 +760,7 @@ def test_index_with_nan(self):
|
691 | 760 | result = y.to_string()
|
692 | 761 | expected = u' id1 id3 value\nid2 \nNaN 1a3 78d 123\nNaN 9h4 79d 64'
|
693 | 762 | self.assert_(result == expected)
|
694 |
| - |
| 763 | + |
695 | 764 | # partial nan in mi
|
696 | 765 | df2 = df.copy()
|
697 | 766 | df2.ix[:,'id2'] = np.nan
|
@@ -1170,8 +1239,8 @@ def get_ipython():
|
1170 | 1239 | self.assert_(repstr is not None)
|
1171 | 1240 |
|
1172 | 1241 | fmt.set_printoptions(max_rows=5, max_columns=2)
|
1173 |
| - |
1174 |
| - self.assert_(self.frame._repr_html_() is None) |
| 1242 | + repstr = self.frame._repr_html_() |
| 1243 | + self.assert_('class' in repstr) # info fallback |
1175 | 1244 |
|
1176 | 1245 | fmt.reset_printoptions()
|
1177 | 1246 |
|
|
0 commit comments