Skip to content

Commit de94073

Browse files
JimStearns206TomAugspurger
authored andcommitted
BUG: Render empty DataFrame as empty HTML table w/o raising IndexError. (#16441)
* BUG: Render empty DataFrame as empty HTML table w/o raising IndexError. * TST: Test rendering of 2 empty-ish DataFrames (#15953) DataFrame with an index but no column, and one with a column but no index. Add entry to whatsnew. (cherry picked from commit d9a63d0)
1 parent e089122 commit de94073

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ I/O
5757
^^^
5858

5959
- Bug that would force importing of the clipboard routines unnecessarily, potentially causing an import error on startup (:issue:`16288`)
60+
- Bug that raised IndexError HTML-rendering an empty DataFrame (:issue:`15953`)
6061

6162

6263
Plotting

pandas/io/formats/style.py

+19-18
Original file line numberDiff line numberDiff line change
@@ -238,24 +238,25 @@ def format_attr(pair):
238238
"class": " ".join(cs),
239239
"is_visible": True})
240240

241-
for c, value in enumerate(clabels[r]):
242-
cs = [COL_HEADING_CLASS, "level%s" % r, "col%s" % c]
243-
cs.extend(cell_context.get(
244-
"col_headings", {}).get(r, {}).get(c, []))
245-
es = {
246-
"type": "th",
247-
"value": value,
248-
"display_value": value,
249-
"class": " ".join(cs),
250-
"is_visible": _is_visible(c, r, col_lengths),
251-
}
252-
colspan = col_lengths.get((r, c), 0)
253-
if colspan > 1:
254-
es["attributes"] = [
255-
format_attr({"key": "colspan", "value": colspan})
256-
]
257-
row_es.append(es)
258-
head.append(row_es)
241+
if clabels:
242+
for c, value in enumerate(clabels[r]):
243+
cs = [COL_HEADING_CLASS, "level%s" % r, "col%s" % c]
244+
cs.extend(cell_context.get(
245+
"col_headings", {}).get(r, {}).get(c, []))
246+
es = {
247+
"type": "th",
248+
"value": value,
249+
"display_value": value,
250+
"class": " ".join(cs),
251+
"is_visible": _is_visible(c, r, col_lengths),
252+
}
253+
colspan = col_lengths.get((r, c), 0)
254+
if colspan > 1:
255+
es["attributes"] = [
256+
format_attr({"key": "colspan", "value": colspan})
257+
]
258+
row_es.append(es)
259+
head.append(row_es)
259260

260261
if self.data.index.names and not all(x is None
261262
for x in self.data.index.names):

pandas/tests/io/formats/test_style.py

+10
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ def test_render(self):
103103
s.render()
104104
# it worked?
105105

106+
def test_render_empty_dfs(self):
107+
empty_df = DataFrame()
108+
es = Styler(empty_df)
109+
es.render()
110+
# An index but no columns
111+
DataFrame(columns=['a']).style.render()
112+
# A column but no index
113+
DataFrame(index=['a']).style.render()
114+
# No IndexError raised?
115+
106116
def test_render_double(self):
107117
df = pd.DataFrame({"A": [0, 1]})
108118
style = lambda x: pd.Series(["color: red; border: 1px",

0 commit comments

Comments
 (0)