Skip to content

Commit bdfbce4

Browse files
authored
Merge pull request #265 from lincc-frameworks/fix_slow_repr
fix slow html_repr at scale
2 parents e854bb5 + 2885a1b commit bdfbce4

File tree

1 file changed

+13
-4
lines changed
  • src/nested_pandas/nestedframe

1 file changed

+13
-4
lines changed

src/nested_pandas/nestedframe/core.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,16 @@ def repack_row(chunk):
131131
return None
132132
return chunk.to_html(max_rows=1, max_cols=5, show_dimensions=True, index=False, header=False)
133133

134+
# Handle sizing, trim html dataframe if output will be truncated
135+
df_shape = self.shape # grab original shape information for later
136+
if pd.get_option("display.max_rows") is None or df_shape[0] > pd.get_option("display.max_rows"):
137+
html_df = self.head(pd.get_option("display.min_rows") + 1)
138+
else:
139+
html_df = self.copy()
140+
134141
# replace index to ensure proper behavior for duplicate index values
135-
index_values = self.index
136-
html_df = self.reset_index(drop=True)
142+
index_values = html_df.index
143+
html_df = html_df.reset_index(drop=True)
137144

138145
# Apply repacking to all nested columns
139146
repr = html_df.style.format(
@@ -152,15 +159,17 @@ def map_true_index(index):
152159

153160
# Recover some truncation formatting, limited to head truncation
154161
if pd.get_option("display.max_rows") is None:
162+
# Just display header
155163
return repr.to_html(max_rows=0)
156-
elif repr.data.shape[0] > pd.get_option("display.max_rows"):
164+
elif df_shape[0] > pd.get_option("display.max_rows"):
165+
# when over the max_rows threshold, display with truncation ("..." row at the end)
157166
html_repr = repr.to_html(max_rows=pd.get_option("display.min_rows"))
158167
else:
159168
# when under the max_rows threshold, display all rows (behavior of 0 here)
160169
html_repr = repr.to_html(max_rows=0)
161170

162171
# Manually append dimensionality to a styler output
163-
html_repr += f"{repr.data.shape[0]} rows x {repr.data.shape[1]} columns"
172+
html_repr += f"{df_shape[0]} rows x {df_shape[1]} columns"
164173

165174
return html_repr
166175

0 commit comments

Comments
 (0)