-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Add HTML repr for groupby dataframe and series #34926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5e1cb8c
9c3df8a
4a3911a
46f5353
c840c29
139bdc6
1020be9
2e4a6ee
ea2f151
2443b80
913afb0
7efc505
d85fc63
778d90d
9736007
7f1937c
388f35d
228e659
dee1220
57b8bf3
2c5c394
669c047
8a75299
b36177d
edff21d
580d09b
0c948e1
e41ff00
ae8721d
1c92ed8
b92d61f
579998a
8d8b260
5865cfb
7a11be8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2086,3 +2086,42 @@ def buffer_put_lines(buf: IO[str], lines: list[str]) -> None: | |
if any(isinstance(x, str) for x in lines): | ||
lines = [str(x) for x in lines] | ||
buf.write("\n".join(lines)) | ||
|
||
|
||
def repr_html_groupby(group_obj) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should use the same machines as DataFrameFormatter/DataFrameRenderer (subclass as appropriate), which was recently changed). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, for the long inactivity. I don't get how I would use the DataFrameFormatter? Is there documentation on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can find examples in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, for coming back to this again, but I really dont get what code I should change or how? Could you tell me which line in my code I have to rewrite? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still relevant? I still would need some guidance :) |
||
""" | ||
Create an HTML representation for a grouped DataFrame or Series. | ||
|
||
Parameters | ||
---------- | ||
group_obj : [DataFrameGroupBy, SeriesGroupBy] | ||
Object to make HTML representation of. | ||
Returns | ||
------- | ||
str : | ||
HTML representation of the input object. | ||
""" | ||
max_groups = get_option("display.max_groups") | ||
max_rows = max( | ||
1, get_option("display.max_rows") // min(max_groups, group_obj.ngroups) | ||
) | ||
group_names = list(group_obj.groups.keys()) | ||
truncated = max_groups < group_obj.ngroups | ||
if truncated: | ||
n_start = (max_groups + 1) // 2 | ||
n_end = max_groups - n_start | ||
group_names = group_names[:n_start] + group_names[-n_end:] | ||
repr_html_list = list() | ||
for group_name in group_names: | ||
if not isinstance(group_name, tuple): | ||
group = group_obj.get_group((group_name, )) | ||
else: | ||
group = group_obj.get_group(group_name) | ||
if not hasattr(group, "to_html"): | ||
group = group.to_frame() | ||
repr_html_list.append( | ||
f"<H3>Group Key: {group_name}<H3/>\n{group.to_html(max_rows=max_rows)}" | ||
) | ||
if truncated: | ||
repr_html_list.insert(max_groups // 2, "<H3>...<H3/>") | ||
return "\n".join(repr_html_list) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to reorg this to use a GroupbyFormatter located in pandas/io/formats/groupby.py (it can do pretty much this but just locate the code there) as this is where we keep all of the formatting code.
could also add a
.to_string()
method but not sure that's actually worth it (maybe open an issue for that).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank for the review! Do you mean pandas/io/formats/html.py? Should I add a new function and then just call that function from the above location?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, i mean pandas/io/formats/format.py (ok to just shove in there is fine, we should split that file up but that's for later).