Skip to content

Commit bfe145b

Browse files
committed
Fix a crash in DesignMatrix.__repr__ when shape[0] == 0
Discovered by @rsgmon in gh-82.
1 parent f9ad38b commit bfe145b

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

patsy/design_info.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,8 +1085,13 @@ def _repr_pretty_(self, p, cycle):
10851085
formatted_cols = [_format_float_column(PRECISION,
10861086
printable_part[:, i])
10871087
for i in range(self.shape[1])]
1088-
column_num_widths = [max([len(s) for s in col])
1089-
for col in formatted_cols]
1088+
def max_width(col):
1089+
assert col.ndim == 1
1090+
if not col.shape[0]:
1091+
return 0
1092+
else:
1093+
return max([len(s) for s in col])
1094+
column_num_widths = [max_width(col) for col in formatted_cols]
10901095
column_widths = [max(name_width, num_width)
10911096
for (name_width, num_width)
10921097
in zip(column_name_widths, column_num_widths)]
@@ -1188,3 +1193,7 @@ def test_design_matrix():
11881193
repr(DesignMatrix(np.arange(100).reshape((1, 100))))
11891194
repr(DesignMatrix([np.nan, np.inf]))
11901195
repr(DesignMatrix([np.nan, 0, 1e20, 20.5]))
1196+
# handling of zero-size matrices
1197+
repr(DesignMatrix(np.zeros((1, 0))))
1198+
repr(DesignMatrix(np.zeros((0, 1))))
1199+
repr(DesignMatrix(np.zeros((0, 0))))

0 commit comments

Comments
 (0)