Skip to content

Commit 39048f9

Browse files
keewismax-sixty
andauthored
speed up the repr for big MultiIndex objects (#4846)
* print the repr of a multiindex using only a subset of the coordinate values * don't index if we have less items than available width * don't try to shorten arrays which are way too short * col_width seems to be the maximum number of elements, not characters * add a asv benchmark * Apply suggestions from code review Co-authored-by: Maximilian Roos <[email protected]> Co-authored-by: Maximilian Roos <[email protected]>
1 parent f4b95cd commit 39048f9

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

asv_bench/benchmarks/repr.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pandas as pd
2+
3+
import xarray as xr
4+
5+
6+
class ReprMultiIndex:
7+
def setup(self, key):
8+
index = pd.MultiIndex.from_product(
9+
[range(10000), range(10000)], names=("level_0", "level_1")
10+
)
11+
series = pd.Series(range(100000000), index=index)
12+
self.da = xr.DataArray(series)
13+
14+
def time_repr(self):
15+
repr(self.da)
16+
17+
def time_repr_html(self):
18+
self.da._repr_html_()

xarray/core/formatting.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,18 @@ def _summarize_coord_multiindex(coord, col_width, marker):
300300

301301

302302
def _summarize_coord_levels(coord, col_width, marker="-"):
303+
if len(coord) > 100 and col_width < len(coord):
304+
n_values = col_width
305+
indices = list(range(0, n_values)) + list(range(-n_values, 0))
306+
subset = coord[indices]
307+
else:
308+
subset = coord
309+
303310
return "\n".join(
304311
summarize_variable(
305-
lname, coord.get_level_variable(lname), col_width, marker=marker
312+
lname, subset.get_level_variable(lname), col_width, marker=marker
306313
)
307-
for lname in coord.level_names
314+
for lname in subset.level_names
308315
)
309316

310317

0 commit comments

Comments
 (0)