Skip to content

Commit

Permalink
BUG: to_html regards empty string labels as repeated labels GH3547
Browse files Browse the repository at this point in the history
  • Loading branch information
y-p committed May 10, 2013
1 parent fff30a3 commit 261839e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
5 changes: 5 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,11 @@ def _where_compat(mask, arr1, arr2):

return np.where(mask, arr1, arr2)

def sentinal_factory():
class Sentinal(object):
pass

return Sentinal()

def in_interactive_session():
""" check if we're running in an interactive shell
Expand Down
19 changes: 11 additions & 8 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,11 @@ def _column_header():
if isinstance(self.columns, MultiIndex):
template = 'colspan="%d" halign="left"'

levels = self.columns.format(sparsify=True, adjoin=False,
# GH3547
sentinal = com.sentinal_factory()
levels = self.columns.format(sparsify=sentinal, adjoin=False,
names=False)
level_lengths = _get_level_lengths(levels)
level_lengths = _get_level_lengths(levels,sentinal)

row_levels = self.frame.index.nlevels

Expand Down Expand Up @@ -703,9 +705,11 @@ def _write_hierarchical_rows(self, fmt_values, indent):
idx_values = zip(*idx_values)

if self.fmt.sparsify:
levels = frame.index.format(sparsify=True, adjoin=False,
names=False)
level_lengths = _get_level_lengths(levels)

# GH3547
sentinal = com.sentinal_factory()
levels = frame.index.format(sparsify=sentinal, adjoin=False, names=False)
level_lengths = _get_level_lengths(levels,sentinal)

for i in range(len(frame)):
row = []
Expand Down Expand Up @@ -738,15 +742,14 @@ def _write_hierarchical_rows(self, fmt_values, indent):
self.write_tr(row, indent, self.indent_delta, tags=None,
nindex_levels=frame.index.nlevels)


def _get_level_lengths(levels):
def _get_level_lengths(levels,sentinal=''):
from itertools import groupby

def _make_grouper():
record = {'count': 0}

def grouper(x):
if x != '':
if x != sentinal:
record['count'] += 1
return record['count']
return grouper
Expand Down
12 changes: 9 additions & 3 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1700,9 +1700,15 @@ def format(self, space=2, sparsify=None, adjoin=True, names=False,
sparsify = get_option("display.multi_sparse")

if sparsify:
sentinal = ''
# GH3547
# use value of sparsify as sentinal, unless it's an obvious "Truthey" value
if sparsify not in [True,1]:
sentinal = sparsify
# little bit of a kludge job for #1217
result_levels = _sparsify(result_levels,
start=int(names))
start=int(names),
sentinal=sentinal)

if adjoin:
return com.adjoin(space, *result_levels).split('\n')
Expand Down Expand Up @@ -2631,7 +2637,7 @@ def _wrap_joined_index(self, joined, other):

# For utility purposes

def _sparsify(label_list, start=0):
def _sparsify(label_list, start=0,sentinal=''):
pivoted = zip(*label_list)
k = len(label_list)

Expand All @@ -2648,7 +2654,7 @@ def _sparsify(label_list, start=0):
break

if p == t:
sparse_cur.append('')
sparse_cur.append(sentinal)
else:
sparse_cur.extend(cur[i:])
result.append(sparse_cur)
Expand Down

0 comments on commit 261839e

Please sign in to comment.