@@ -69,6 +69,9 @@ class Styler(object):
6969 a unique identifier to avoid CSS collisons; generated automatically
7070 caption: str, default None
7171 caption to attach to the table
72+ index_hidden: bool, default False
73+ hides the index in the html output
74+ .. versionadded:: 0.20.1
7275
7376 Attributes
7477 ----------
@@ -117,7 +120,8 @@ class Styler(object):
117120 template = env .get_template ("html.tpl" )
118121
119122 def __init__ (self , data , precision = None , table_styles = None , uuid = None ,
120- caption = None , table_attributes = None ):
123+ caption = None , table_attributes = None , index_hidden = False ,
124+ hidden_cols = None ):
121125 self .ctx = defaultdict (list )
122126 self ._todo = []
123127
@@ -139,6 +143,11 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None,
139143 precision = get_option ('display.precision' )
140144 self .precision = precision
141145 self .table_attributes = table_attributes
146+ self .index_hidden = index_hidden
147+ if hidden_cols is None :
148+ hidden_cols = []
149+ self .hidden_cols = hidden_cols
150+
142151 # display_funcs maps (row, col) -> formatting function
143152
144153 def default_display_func (x ):
@@ -186,6 +195,8 @@ def _translate(self):
186195 caption = self .caption
187196 ctx = self .ctx
188197 precision = self .precision
198+ index_hidden = self .index_hidden
199+ hidden_cols = self .hidden_cols
189200 uuid = self .uuid or str (uuid1 ()).replace ("-" , "_" )
190201 ROW_HEADING_CLASS = "row_heading"
191202 COL_HEADING_CLASS = "col_heading"
@@ -200,7 +211,7 @@ def format_attr(pair):
200211
201212 # for sparsifying a MultiIndex
202213 idx_lengths = _get_level_lengths (self .index )
203- col_lengths = _get_level_lengths (self .columns )
214+ col_lengths = _get_level_lengths (self .columns , hidden_cols )
204215
205216 cell_context = dict ()
206217
@@ -223,7 +234,7 @@ def format_attr(pair):
223234 row_es = [{"type" : "th" ,
224235 "value" : BLANK_VALUE ,
225236 "display_value" : BLANK_VALUE ,
226- "is_visible" : True ,
237+ "is_visible" : ( not index_hidden ) ,
227238 "class" : " " .join ([BLANK_CLASS ])}] * (n_rlvls - 1 )
228239
229240 # ... except maybe the last for columns.names
@@ -235,7 +246,7 @@ def format_attr(pair):
235246 "value" : name ,
236247 "display_value" : name ,
237248 "class" : " " .join (cs ),
238- "is_visible" : True })
249+ "is_visible" : ( not index_hidden ) })
239250
240251 for c , value in enumerate (clabels [r ]):
241252 cs = [COL_HEADING_CLASS , "level%s" % r , "col%s" % c ]
@@ -256,8 +267,8 @@ def format_attr(pair):
256267 row_es .append (es )
257268 head .append (row_es )
258269
259- if self .data .index .names and not all ( x is None
260- for x in self .data .index .names ):
270+ if self .data .index .names and not index_hidden \
271+ and not all ( x is None for x in self .data .index .names ):
261272 index_header_row = []
262273
263274 for c , name in enumerate (self .data .index .names ):
@@ -271,7 +282,7 @@ def format_attr(pair):
271282 [{"type" : "th" ,
272283 "value" : BLANK_VALUE ,
273284 "class" : " " .join ([BLANK_CLASS ])
274- }] * len (clabels [0 ]))
285+ }] * ( len (clabels [0 ]) - len ( hidden_cols ) ))
275286
276287 head .append (index_header_row )
277288
@@ -281,7 +292,8 @@ def format_attr(pair):
281292 for c , value in enumerate (rlabels [r ]):
282293 es = {
283294 "type" : "th" ,
284- "is_visible" : _is_visible (r , c , idx_lengths ),
295+ "is_visible" : (_is_visible (r , c , idx_lengths ) &
296+ (~ index_hidden )),
285297 "value" : value ,
286298 "display_value" : value ,
287299 "class" : " " .join ([ROW_HEADING_CLASS , "level%s" % c ,
@@ -304,7 +316,8 @@ def format_attr(pair):
304316 "value" : value ,
305317 "class" : " " .join (cs ),
306318 "id" : "_" .join (cs [1 :]),
307- "display_value" : formatter (value )
319+ "display_value" : formatter (value ),
320+ "is_visible" : (c not in hidden_cols )
308321 })
309322 props = []
310323 for x in ctx [r , c ]:
@@ -460,7 +473,9 @@ def _update_ctx(self, attrs):
460473 def _copy (self , deepcopy = False ):
461474 styler = Styler (self .data , precision = self .precision ,
462475 caption = self .caption , uuid = self .uuid ,
463- table_styles = self .table_styles )
476+ table_styles = self .table_styles ,
477+ index_hidden = self .index_hidden ,
478+ hidden_cols = self .hidden_cols )
464479 if deepcopy :
465480 styler .ctx = copy .deepcopy (self .ctx )
466481 styler ._todo = copy .deepcopy (self ._todo )
@@ -716,7 +731,7 @@ def set_uuid(self, uuid):
716731
717732 def set_caption (self , caption ):
718733 """
719- Se the caption on a Styler
734+ Set the caption on a Styler
720735
721736 .. versionadded:: 0.17.1
722737
@@ -762,6 +777,41 @@ def set_table_styles(self, table_styles):
762777 self .table_styles = table_styles
763778 return self
764779
780+ def hide_index (self ):
781+ """
782+ Hide any indices from rendering.
783+
784+ .. versionadded:: 0.20.1
785+
786+ Returns
787+ -------
788+ self : Styler
789+ """
790+ self .index_hidden = True
791+ return self
792+
793+ def hide_columns (self , subset ):
794+ """
795+ Hide columns from rendering.
796+
797+ .. versionadded:: 0.20.1
798+
799+ Parameters
800+ ----------
801+
802+ subset: IndexSlice
803+ An argument to ``DataFrame.loc`` that identifies which columns
804+ are hidden.
805+
806+ Returns
807+ -------
808+ self : Styler
809+ """
810+ subset = _non_reducing_slice (subset )
811+ hidden_df = self .data .loc [subset ]
812+ self .hidden_cols = self .columns .get_indexer_for (hidden_df .columns )
813+ return self
814+
765815 # -----------------------------------------------------------------------
766816 # A collection of "builtin" styles
767817 # -----------------------------------------------------------------------
@@ -1009,31 +1059,48 @@ def _is_visible(idx_row, idx_col, lengths):
10091059 return (idx_col , idx_row ) in lengths
10101060
10111061
1012- def _get_level_lengths (index ):
1062+ def _get_level_lengths (index , hidden_elements = None ):
10131063 """
10141064 Given an index, find the level lenght for each element.
1065+ Optional argument is a list of index positions which
1066+ should not be visible.
10151067
10161068 Result is a dictionary of (level, inital_position): span
10171069 """
10181070 sentinel = com .sentinel_factory ()
10191071 levels = index .format (sparsify = sentinel , adjoin = False , names = False )
10201072
1021- if index . nlevels == 1 :
1022- return {( 0 , i ): 1 for i , value in enumerate ( levels )}
1073+ if hidden_elements is None :
1074+ hidden_elements = []
10231075
10241076 lengths = {}
1077+ if index .nlevels == 1 :
1078+ for i , value in enumerate (levels ):
1079+ if (i not in hidden_elements ):
1080+ lengths [(0 , i )] = 1
1081+ return lengths
10251082
10261083 for i , lvl in enumerate (levels ):
10271084 for j , row in enumerate (lvl ):
10281085 if not get_option ('display.multi_sparse' ):
10291086 lengths [(i , j )] = 1
1030- elif row != sentinel :
1087+ elif ( row != sentinel ) and ( j not in hidden_elements ) :
10311088 last_label = j
10321089 lengths [(i , last_label )] = 1
1033- else :
1090+ elif (row != sentinel ):
1091+ # even if its hidden, keep track of it in case
1092+ # length >1 and later elemens are visible
1093+ last_label = j
1094+ lengths [(i , last_label )] = 0
1095+ elif (j not in hidden_elements ):
10341096 lengths [(i , last_label )] += 1
10351097
1036- return lengths
1098+ non_zero_lengths = {}
1099+ for element , length in lengths .items ():
1100+ if (length >= 1 ):
1101+ non_zero_lengths [element ] = length
1102+
1103+ return non_zero_lengths
10371104
10381105
10391106def _maybe_wrap_formatter (formatter ):
0 commit comments