Skip to content

Commit 0068e5b

Browse files
committed
Add group display truncation for too many groups
1 parent 46f5353 commit 0068e5b

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

pandas/core/groupby/groupby.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,22 @@ def __repr__(self) -> str:
544544
return object.__repr__(self)
545545

546546
def _repr_html_(self) -> str:
547+
max_groups = get_option("display.max_groups")
548+
ngroups = self.ngroups
549+
n_start = (max_groups + 1) // 2
550+
n_end = max_groups - n_start
547551
repr_html = ""
548-
for group_name, group in self:
549-
if not hasattr(group, "to_html"):
550-
group = group.to_frame()
551-
repr_html += f"<H3>Group Key: {group_name}<H3/>"
552-
repr_html += group.to_html(
553-
max_rows=get_option("display.max_rows") // self.ngroups
554-
)
552+
truncated = ngroups > max_groups
553+
for k, (group_name, group) in enumerate(self):
554+
if not truncated or (n_start > k or k >= ngroups - n_end):
555+
if not hasattr(group, "to_html"):
556+
group = group.to_frame()
557+
repr_html += f"<H3>Group Key: {group_name}<H3/>"
558+
repr_html += group.to_html(
559+
max_rows=max(1, get_option("display.max_rows") // self.ngroups)
560+
)
561+
elif k == max_groups // 2:
562+
repr_html += "<H3>...<H3/>"
555563
return repr_html
556564

557565
def _assure_grouper(self):

0 commit comments

Comments
 (0)