ENH 14194: add style option for hiding index and columns (#16141) · pandas-dev/pandas@b00e62c (original) (raw)
`@@ -133,6 +133,9 @@ def init(self, data, precision=None, table_styles=None, uuid=None,
`
133
133
`precision = get_option('display.precision')
`
134
134
`self.precision = precision
`
135
135
`self.table_attributes = table_attributes
`
``
136
`+
self.hidden_index = False
`
``
137
`+
self.hidden_columns = []
`
``
138
+
136
139
`# display_funcs maps (row, col) -> formatting function
`
137
140
``
138
141
`def default_display_func(x):
`
`@@ -180,6 +183,8 @@ def _translate(self):
`
180
183
`caption = self.caption
`
181
184
`ctx = self.ctx
`
182
185
`precision = self.precision
`
``
186
`+
hidden_index = self.hidden_index
`
``
187
`+
hidden_columns = self.hidden_columns
`
183
188
`uuid = self.uuid or str(uuid1()).replace("-", "_")
`
184
189
`ROW_HEADING_CLASS = "row_heading"
`
185
190
`COL_HEADING_CLASS = "col_heading"
`
`@@ -194,7 +199,7 @@ def format_attr(pair):
`
194
199
``
195
200
`# for sparsifying a MultiIndex
`
196
201
`idx_lengths = _get_level_lengths(self.index)
`
197
``
`-
col_lengths = _get_level_lengths(self.columns)
`
``
202
`+
col_lengths = _get_level_lengths(self.columns, hidden_columns)
`
198
203
``
199
204
`cell_context = dict()
`
200
205
``
`@@ -217,7 +222,7 @@ def format_attr(pair):
`
217
222
`row_es = [{"type": "th",
`
218
223
`"value": BLANK_VALUE,
`
219
224
`"display_value": BLANK_VALUE,
`
220
``
`-
"is_visible": True,
`
``
225
`+
"is_visible": not hidden_index,
`
221
226
`"class": " ".join([BLANK_CLASS])}] * (n_rlvls - 1)
`
222
227
``
223
228
`# ... except maybe the last for columns.names
`
`@@ -229,7 +234,7 @@ def format_attr(pair):
`
229
234
`"value": name,
`
230
235
`"display_value": name,
`
231
236
`"class": " ".join(cs),
`
232
``
`-
"is_visible": True})
`
``
237
`+
"is_visible": not hidden_index})
`
233
238
``
234
239
`if clabels:
`
235
240
`for c, value in enumerate(clabels[r]):
`
`@@ -252,7 +257,8 @@ def format_attr(pair):
`
252
257
`row_es.append(es)
`
253
258
`head.append(row_es)
`
254
259
``
255
``
`-
if self.data.index.names and _any_not_none(*self.data.index.names):
`
``
260
`+
if (self.data.index.names and _any_not_none(*self.data.index.names) and
`
``
261
`+
not hidden_index):
`
256
262
`index_header_row = []
`
257
263
``
258
264
`for c, name in enumerate(self.data.index.names):
`
`@@ -266,7 +272,7 @@ def format_attr(pair):
`
266
272
` [{"type": "th",
`
267
273
`"value": BLANK_VALUE,
`
268
274
`"class": " ".join([BLANK_CLASS])
`
269
``
`-
}] * len(clabels[0]))
`
``
275
`+
}] * (len(clabels[0]) - len(hidden_columns)))
`
270
276
``
271
277
`head.append(index_header_row)
`
272
278
``
`@@ -278,7 +284,8 @@ def format_attr(pair):
`
278
284
`"row{row}".format(row=r)]
`
279
285
`es = {
`
280
286
`"type": "th",
`
281
``
`-
"is_visible": _is_visible(r, c, idx_lengths),
`
``
287
`+
"is_visible": (_is_visible(r, c, idx_lengths) and
`
``
288
`+
not hidden_index),
`
282
289
`"value": value,
`
283
290
`"display_value": value,
`
284
291
`"id": "_".join(rid[1:]),
`
`@@ -302,7 +309,8 @@ def format_attr(pair):
`
302
309
`"value": value,
`
303
310
`"class": " ".join(cs),
`
304
311
`"id": "_".join(cs[1:]),
`
305
``
`-
"display_value": formatter(value)
`
``
312
`+
"display_value": formatter(value),
`
``
313
`+
"is_visible": (c not in hidden_columns)
`
306
314
` })
`
307
315
`props = []
`
308
316
`for x in ctx[r, c]:
`
`@@ -741,7 +749,7 @@ def set_uuid(self, uuid):
`
741
749
``
742
750
`def set_caption(self, caption):
`
743
751
`"""
`
744
``
`-
Se the caption on a Styler
`
``
752
`+
Set the caption on a Styler
`
745
753
``
746
754
` Parameters
`
747
755
` ----------
`
`@@ -783,6 +791,40 @@ def set_table_styles(self, table_styles):
`
783
791
`self.table_styles = table_styles
`
784
792
`return self
`
785
793
``
``
794
`+
def hide_index(self):
`
``
795
`+
"""
`
``
796
`+
Hide any indices from rendering.
`
``
797
+
``
798
`+
.. versionadded:: 0.22.0
`
``
799
+
``
800
`+
Returns
`
``
801
`+
`
``
802
`+
self : Styler
`
``
803
`+
"""
`
``
804
`+
self.hidden_index = True
`
``
805
`+
return self
`
``
806
+
``
807
`+
def hide_columns(self, subset):
`
``
808
`+
"""
`
``
809
`+
Hide columns from rendering.
`
``
810
+
``
811
`+
.. versionadded:: 0.22.0
`
``
812
+
``
813
`+
Parameters
`
``
814
`+
`
``
815
`+
subset: IndexSlice
`
``
816
An argument to ``DataFrame.loc`` that identifies which columns
``
817
`+
are hidden.
`
``
818
+
``
819
`+
Returns
`
``
820
`+
`
``
821
`+
self : Styler
`
``
822
`+
"""
`
``
823
`+
subset = _non_reducing_slice(subset)
`
``
824
`+
hidden_df = self.data.loc[subset]
`
``
825
`+
self.hidden_columns = self.columns.get_indexer_for(hidden_df.columns)
`
``
826
`+
return self
`
``
827
+
786
828
`# -----------------------------------------------------------------------
`
787
829
`# A collection of "builtin" styles
`
788
830
`# -----------------------------------------------------------------------
`
`@@ -1157,31 +1199,48 @@ def _is_visible(idx_row, idx_col, lengths):
`
1157
1199
`return (idx_col, idx_row) in lengths
`
1158
1200
``
1159
1201
``
1160
``
`-
def _get_level_lengths(index):
`
``
1202
`+
def _get_level_lengths(index, hidden_elements=None):
`
1161
1203
`"""
`
1162
1204
` Given an index, find the level lenght for each element.
`
``
1205
`+
Optional argument is a list of index positions which
`
``
1206
`+
should not be visible.
`
1163
1207
``
1164
1208
` Result is a dictionary of (level, inital_position): span
`
1165
1209
` """
`
1166
1210
`sentinel = sentinel_factory()
`
1167
1211
`levels = index.format(sparsify=sentinel, adjoin=False, names=False)
`
1168
1212
``
1169
``
`-
if index.nlevels == 1:
`
1170
``
`-
return {(0, i): 1 for i, value in enumerate(levels)}
`
``
1213
`+
if hidden_elements is None:
`
``
1214
`+
hidden_elements = []
`
1171
1215
``
1172
1216
`lengths = {}
`
``
1217
`+
if index.nlevels == 1:
`
``
1218
`+
for i, value in enumerate(levels):
`
``
1219
`+
if(i not in hidden_elements):
`
``
1220
`+
lengths[(0, i)] = 1
`
``
1221
`+
return lengths
`
1173
1222
``
1174
1223
`for i, lvl in enumerate(levels):
`
1175
1224
`for j, row in enumerate(lvl):
`
1176
1225
`if not get_option('display.multi_sparse'):
`
1177
1226
`lengths[(i, j)] = 1
`
1178
``
`-
elif row != sentinel:
`
``
1227
`+
elif (row != sentinel) and (j not in hidden_elements):
`
1179
1228
`last_label = j
`
1180
1229
`lengths[(i, last_label)] = 1
`
1181
``
`-
else:
`
``
1230
`+
elif (row != sentinel):
`
``
1231
`+
even if its hidden, keep track of it in case
`
``
1232
`+
length >1 and later elemens are visible
`
``
1233
`+
last_label = j
`
``
1234
`+
lengths[(i, last_label)] = 0
`
``
1235
`+
elif(j not in hidden_elements):
`
1182
1236
`lengths[(i, last_label)] += 1
`
1183
1237
``
1184
``
`-
return lengths
`
``
1238
`+
non_zero_lengths = {}
`
``
1239
`+
for element, length in lengths.items():
`
``
1240
`+
if(length >= 1):
`
``
1241
`+
non_zero_lengths[element] = length
`
``
1242
+
``
1243
`+
return non_zero_lengths
`
1185
1244
``
1186
1245
``
1187
1246
`def _maybe_wrap_formatter(formatter):
`