Skip to content

Commit b46b001

Browse files
committed
ENH: add multi_sparse option to set_printoptions to disable sparsification in MultiIndex.format, close #1538
1 parent 1a21af6 commit b46b001

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pandas 0.8.1
3333
- Add new ``bootstrap_plot`` plot function
3434
- Add new ``parallel_coordinates`` plot function (#1488)
3535
- Add ``radviz`` plot function (#1566)
36+
- Add ``multi_sparse`` option to ``set_printoptions`` to modify display of
37+
hierarchical indexes (#1538)
3638

3739
**Improvements to existing features**
3840

pandas/core/format.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,17 @@ class DataFrameFormatter(object):
149149

150150
def __init__(self, frame, buf=None, columns=None, col_space=None,
151151
header=True, index=True, na_rep='NaN', formatters=None,
152-
justify=None, float_format=None, sparsify=True,
152+
justify=None, float_format=None, sparsify=None,
153153
index_names=True, **kwds):
154154
self.frame = frame
155155
self.buf = buf if buf is not None else StringIO()
156156
self.show_index_names = index_names
157+
158+
if sparsify is None:
159+
sparsify = print_config.multi_sparse
160+
157161
self.sparsify = sparsify
162+
158163
self.float_format = float_format
159164
self.formatters = formatters if formatters is not None else {}
160165
self.na_rep = na_rep
@@ -662,7 +667,8 @@ def _has_names(index):
662667
def set_printoptions(precision=None, column_space=None, max_rows=None,
663668
max_columns=None, colheader_justify=None,
664669
max_colwidth=None, notebook_repr_html=None,
665-
date_dayfirst=None, date_yearfirst=None):
670+
date_dayfirst=None, date_yearfirst=None,
671+
multi_sparse=None):
666672
"""
667673
Alter default behavior of DataFrame.toString
668674
@@ -686,6 +692,9 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
686692
When True, prints and parses dates with the day first, eg 20/01/2005
687693
date_yearfirst : boolean
688694
When True, prints and parses dates with the year first, eg 2005/01/20
695+
multi_sparse : boolean
696+
Default True, "sparsify" MultiIndex display (don't display repeated
697+
elements in outer levels within groups)
689698
"""
690699
if precision is not None:
691700
print_config.precision = precision
@@ -705,6 +714,8 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
705714
print_config.date_dayfirst = date_dayfirst
706715
if date_yearfirst is not None:
707716
print_config.date_yearfirst = date_yearfirst
717+
if multi_sparse is not None:
718+
print_config.multi_sparse = multi_sparse
708719

709720
def reset_printoptions():
710721
print_config.reset()
@@ -834,6 +845,7 @@ def __init__(self):
834845
self.notebook_repr_html = True
835846
self.date_dayfirst = False
836847
self.date_yearfirst = False
848+
self.multi_sparse = True
837849

838850
def reset(self):
839851
self.__init__()

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ def to_excel(self, excel_writer, sheet_name='sheet1', na_rep='',
11991199
@Appender(fmt.docstring_to_string, indents=1)
12001200
def to_string(self, buf=None, columns=None, col_space=None, colSpace=None,
12011201
header=True, index=True, na_rep='NaN', formatters=None,
1202-
float_format=None, sparsify=True, nanRep=None,
1202+
float_format=None, sparsify=None, nanRep=None,
12031203
index_names=True, justify=None, force_unicode=False):
12041204
"""
12051205
Render a DataFrame to a console-friendly tabular output.
@@ -1239,7 +1239,7 @@ def to_string(self, buf=None, columns=None, col_space=None, colSpace=None,
12391239
@Appender(fmt.docstring_to_string, indents=1)
12401240
def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
12411241
header=True, index=True, na_rep='NaN', formatters=None,
1242-
float_format=None, sparsify=True, index_names=True,
1242+
float_format=None, sparsify=None, index_names=True,
12431243
bold_rows=True):
12441244
"""
12451245
to_html-specific options

pandas/core/index.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ def get_level_values(self, level):
14381438
labels = self.labels[num]
14391439
return unique_vals.take(labels)
14401440

1441-
def format(self, space=2, sparsify=True, adjoin=True, names=False):
1441+
def format(self, space=2, sparsify=None, adjoin=True, names=False):
14421442
if len(self) == 0:
14431443
return []
14441444

@@ -1454,6 +1454,10 @@ def format(self, space=2, sparsify=True, adjoin=True, names=False):
14541454
level.extend(ndtake(np.array(lev, dtype=object), lab))
14551455
result_levels.append(level)
14561456

1457+
if sparsify is None:
1458+
import pandas.core.format as fmt
1459+
sparsify = fmt.print_config.multi_sparse
1460+
14571461
if sparsify:
14581462
# little bit of a kludge job for #1217
14591463
result_levels = _sparsify(result_levels,

pandas/tests/test_index.py

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from pandas.tseries.index import _to_m8
2222
import pandas.tseries.offsets as offsets
2323

24+
import pandas as pd
25+
2426
class TestIndex(unittest.TestCase):
2527

2628
def setUp(self):
@@ -1208,6 +1210,15 @@ def test_format_sparse_display(self):
12081210
result = index.format()
12091211
self.assertEqual(result[3], '1 0 0 0')
12101212

1213+
def test_format_sparse_config(self):
1214+
# #1538
1215+
pd.set_printoptions(multi_sparse=False)
1216+
1217+
result = self.index.format()
1218+
self.assertEqual(result[1], 'foo two')
1219+
1220+
pd.reset_printoptions()
1221+
12111222
def test_bounds(self):
12121223
self.index._bounds
12131224

0 commit comments

Comments
 (0)