Skip to content

Commit

Permalink
speed up the repr for big MultiIndex objects (#4846)
Browse files Browse the repository at this point in the history
* 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 <5635139+max-sixty@users.noreply.github.com>

Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com>
  • Loading branch information
keewis and max-sixty authored Jan 29, 2021
1 parent f4b95cd commit 39048f9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
18 changes: 18 additions & 0 deletions asv_bench/benchmarks/repr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pandas as pd

import xarray as xr


class ReprMultiIndex:
def setup(self, key):
index = pd.MultiIndex.from_product(
[range(10000), range(10000)], names=("level_0", "level_1")
)
series = pd.Series(range(100000000), index=index)
self.da = xr.DataArray(series)

def time_repr(self):
repr(self.da)

def time_repr_html(self):
self.da._repr_html_()
11 changes: 9 additions & 2 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,18 @@ def _summarize_coord_multiindex(coord, col_width, marker):


def _summarize_coord_levels(coord, col_width, marker="-"):
if len(coord) > 100 and col_width < len(coord):
n_values = col_width
indices = list(range(0, n_values)) + list(range(-n_values, 0))
subset = coord[indices]
else:
subset = coord

return "\n".join(
summarize_variable(
lname, coord.get_level_variable(lname), col_width, marker=marker
lname, subset.get_level_variable(lname), col_width, marker=marker
)
for lname in coord.level_names
for lname in subset.level_names
)


Expand Down

0 comments on commit 39048f9

Please sign in to comment.