-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Multi-index repr #879
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
Closed
Closed
Multi-index repr #879
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f5bd116
repr for multi-index showing level names/dtypes
7c88d41
display the actual used levels
b624381
much more efficient display of actual used levels
dc35362
fixed col width calculation error with attrs
6673138
clean up
73a828b
Don't need to set default multi-index level names
4e7793a
refactor
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
|
|
||
| from .options import OPTIONS | ||
| from .pycompat import iteritems, unicode_type, bytes_type, dask_array_type | ||
| from .indexing import PandasIndexAdapter | ||
|
|
||
|
|
||
| def pretty_print(x, numchars): | ||
|
|
@@ -122,13 +123,17 @@ def format_items(x): | |
| return formatted | ||
|
|
||
|
|
||
| def _get_max_items(max_width): | ||
| # every item will take up at least two characters, but we always want to | ||
| # print at least one item | ||
| return max(int(np.ceil(max_width / 2.0)), 1) | ||
|
|
||
|
|
||
| def format_array_flat(items_ndarray, max_width): | ||
| """Return a formatted string for as many items in the flattened version of | ||
| items_ndarray that will fit within max_width characters | ||
| """ | ||
| # every item will take up at least two characters, but we always want to | ||
| # print at least one item | ||
| max_possibly_relevant = max(int(np.ceil(max_width / 2.0)), 1) | ||
| max_possibly_relevant = _get_max_items(max_width) | ||
| relevant_items = first_n_items(items_ndarray, max_possibly_relevant) | ||
| pprint_items = format_items(relevant_items) | ||
|
|
||
|
|
@@ -145,6 +150,38 @@ def format_array_flat(items_ndarray, max_width): | |
| return pprint_str | ||
|
|
||
|
|
||
| def _format_multiindex_levels(index, col_width, max_width): | ||
| """Return a formatted string for multi-index levels with | ||
| their name, dtype and (first) actual used values. | ||
| """ | ||
| level_str_lines = [] | ||
| for i in range(index.nlevels): | ||
| idx = index.get_level_values(i) | ||
| first_col = pretty_print('- %s ' % idx.name, col_width) | ||
| front_str = first_col + ('%s ' % idx.dtype) | ||
| values_str = format_array_flat(idx, max_width - len(front_str)) | ||
| level_str_lines.append(front_str + values_str) | ||
|
|
||
| return '\n'.join(level_str_lines) | ||
|
|
||
|
|
||
| def _maybe_summarize_multiindex(var, col_width, max_width): | ||
| if isinstance(var.variable._data, PandasIndexAdapter): | ||
| indent = 4 | ||
| max_width -= indent | ||
|
|
||
| # get first relevant items before convert to a pandas.Index | ||
| # so that we avoid loading the whole data | ||
| index = var[:_get_max_items(max_width)].to_index() | ||
|
|
||
| if isinstance(index, pd.MultiIndex): | ||
| index_str = _format_multiindex_levels(index, col_width, max_width) | ||
| return wrap_indent(index_str, length=indent, | ||
| start='MultiIndex\n' + ' ' * indent) | ||
|
|
||
| return '' | ||
|
|
||
|
|
||
| def _summarize_var_or_coord(name, var, col_width, show_values=True, | ||
| marker=' ', max_width=None): | ||
| if max_width is None: | ||
|
|
@@ -153,7 +190,9 @@ def _summarize_var_or_coord(name, var, col_width, show_values=True, | |
| dims_str = '(%s) ' % ', '.join(map(str, var.dims)) if var.dims else '' | ||
| front_str = first_col + dims_str + ('%s ' % var.dtype) | ||
| if show_values: | ||
| values_str = format_array_flat(var, max_width - len(front_str)) | ||
| values_str = _maybe_summarize_multiindex(var, col_width, max_width) | ||
| if not values_str: | ||
| values_str = format_array_flat(var, max_width - len(front_str)) | ||
| else: | ||
| values_str = '...' | ||
| return front_str + values_str | ||
|
|
@@ -197,7 +236,15 @@ def summarize_attr(key, value, col_width=None): | |
|
|
||
|
|
||
| def _calculate_col_width(mapping): | ||
| max_name_length = max(len(str(k)) for k in mapping) if mapping else 0 | ||
| names = [] | ||
| for k, v in mapping.items(): | ||
| names.append(k) | ||
| if (hasattr(v, 'variable') | ||
| and isinstance(v.variable._data, PandasIndexAdapter)): | ||
|
Member
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. again, this seems a little fragile. |
||
| index = v.variable.to_index() | ||
| if isinstance(index, pd.MultiIndex): | ||
| names += ['- %s' % n for n in index.names] | ||
| max_name_length = max(len(n) for n in names) if names else 0 | ||
| col_width = max(max_name_length, 7) + 6 | ||
| return col_width | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think
isinstance(var, Coordinate)would be a safer check.