Skip to content

Commit

Permalink
ENH: add multi_sparse option to set_printoptions to disable sparsific…
Browse files Browse the repository at this point in the history
…ation in MultiIndex.format, close #1538
  • Loading branch information
wesm committed Jul 12, 2012
1 parent 1a21af6 commit b46b001
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pandas 0.8.1
- Add new ``bootstrap_plot`` plot function
- Add new ``parallel_coordinates`` plot function (#1488)
- Add ``radviz`` plot function (#1566)
- Add ``multi_sparse`` option to ``set_printoptions`` to modify display of
hierarchical indexes (#1538)

**Improvements to existing features**

Expand Down
16 changes: 14 additions & 2 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,17 @@ class DataFrameFormatter(object):

def __init__(self, frame, buf=None, columns=None, col_space=None,
header=True, index=True, na_rep='NaN', formatters=None,
justify=None, float_format=None, sparsify=True,
justify=None, float_format=None, sparsify=None,
index_names=True, **kwds):
self.frame = frame
self.buf = buf if buf is not None else StringIO()
self.show_index_names = index_names

if sparsify is None:
sparsify = print_config.multi_sparse

self.sparsify = sparsify

self.float_format = float_format
self.formatters = formatters if formatters is not None else {}
self.na_rep = na_rep
Expand Down Expand Up @@ -662,7 +667,8 @@ def _has_names(index):
def set_printoptions(precision=None, column_space=None, max_rows=None,
max_columns=None, colheader_justify=None,
max_colwidth=None, notebook_repr_html=None,
date_dayfirst=None, date_yearfirst=None):
date_dayfirst=None, date_yearfirst=None,
multi_sparse=None):
"""
Alter default behavior of DataFrame.toString
Expand All @@ -686,6 +692,9 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
When True, prints and parses dates with the day first, eg 20/01/2005
date_yearfirst : boolean
When True, prints and parses dates with the year first, eg 2005/01/20
multi_sparse : boolean
Default True, "sparsify" MultiIndex display (don't display repeated
elements in outer levels within groups)
"""
if precision is not None:
print_config.precision = precision
Expand All @@ -705,6 +714,8 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
print_config.date_dayfirst = date_dayfirst
if date_yearfirst is not None:
print_config.date_yearfirst = date_yearfirst
if multi_sparse is not None:
print_config.multi_sparse = multi_sparse

def reset_printoptions():
print_config.reset()
Expand Down Expand Up @@ -834,6 +845,7 @@ def __init__(self):
self.notebook_repr_html = True
self.date_dayfirst = False
self.date_yearfirst = False
self.multi_sparse = True

def reset(self):
self.__init__()
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ def to_excel(self, excel_writer, sheet_name='sheet1', na_rep='',
@Appender(fmt.docstring_to_string, indents=1)
def to_string(self, buf=None, columns=None, col_space=None, colSpace=None,
header=True, index=True, na_rep='NaN', formatters=None,
float_format=None, sparsify=True, nanRep=None,
float_format=None, sparsify=None, nanRep=None,
index_names=True, justify=None, force_unicode=False):
"""
Render a DataFrame to a console-friendly tabular output.
Expand Down Expand Up @@ -1239,7 +1239,7 @@ def to_string(self, buf=None, columns=None, col_space=None, colSpace=None,
@Appender(fmt.docstring_to_string, indents=1)
def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
header=True, index=True, na_rep='NaN', formatters=None,
float_format=None, sparsify=True, index_names=True,
float_format=None, sparsify=None, index_names=True,
bold_rows=True):
"""
to_html-specific options
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ def get_level_values(self, level):
labels = self.labels[num]
return unique_vals.take(labels)

def format(self, space=2, sparsify=True, adjoin=True, names=False):
def format(self, space=2, sparsify=None, adjoin=True, names=False):
if len(self) == 0:
return []

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

if sparsify is None:
import pandas.core.format as fmt
sparsify = fmt.print_config.multi_sparse

if sparsify:
# little bit of a kludge job for #1217
result_levels = _sparsify(result_levels,
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from pandas.tseries.index import _to_m8
import pandas.tseries.offsets as offsets

import pandas as pd

class TestIndex(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -1208,6 +1210,15 @@ def test_format_sparse_display(self):
result = index.format()
self.assertEqual(result[3], '1 0 0 0')

def test_format_sparse_config(self):
# #1538
pd.set_printoptions(multi_sparse=False)

result = self.index.format()
self.assertEqual(result[1], 'foo two')

pd.reset_printoptions()

def test_bounds(self):
self.index._bounds

Expand Down

0 comments on commit b46b001

Please sign in to comment.