Skip to content
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

test and fix empty xindexes repr #8521

Merged
merged 6 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def summarize_attr(key, value, col_width=None):


def _calculate_col_width(col_items):
max_name_length = max(len(str(s)) for s in col_items) if col_items else 0
max_name_length = max((len(str(s)) for s in col_items), default=0)
col_width = max(max_name_length, 7) + 6
return col_width

Expand Down
30 changes: 30 additions & 0 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,33 @@ def __array__(self, dtype=None):
# These will crash if var.data are converted to numpy arrays:
var.__repr__()
var._repr_html_()


@pytest.mark.parametrize("as_dataset", (False, True))
def test_format_xindexes_none(as_dataset):
mathause marked this conversation as resolved.
Show resolved Hide resolved
# ensure repr for empty xindexes can be displayed #8367

expected = """\
Indexes:
*empty*"""
expected = dedent(expected)

obj = xr.DataArray()
mathause marked this conversation as resolved.
Show resolved Hide resolved
obj = obj._to_temp_dataset() if as_dataset else obj # type: ignore[assignment]
mathause marked this conversation as resolved.
Show resolved Hide resolved

actual = obj.xindexes.__repr__()
mathause marked this conversation as resolved.
Show resolved Hide resolved
assert actual == expected


@pytest.mark.parametrize("as_dataset", (False, True))
def test_format_xindexes(as_dataset):
mathause marked this conversation as resolved.
Show resolved Hide resolved
expected = """\
Indexes:
x PandasIndex"""
expected = dedent(expected)

obj = xr.DataArray([1], coords={"x": [1]})
mathause marked this conversation as resolved.
Show resolved Hide resolved
obj = obj._to_temp_dataset() if as_dataset else obj # type: ignore[assignment]
mathause marked this conversation as resolved.
Show resolved Hide resolved

actual = obj.xindexes.__repr__()
mathause marked this conversation as resolved.
Show resolved Hide resolved
assert actual == expected
Loading