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 all 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: bool) -> None:
# ensure repr for empty xindexes can be displayed #8367

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

obj: xr.DataArray | xr.Dataset = xr.DataArray()
obj = obj._to_temp_dataset() if as_dataset else obj

actual = repr(obj.xindexes)
assert actual == expected


@pytest.mark.parametrize("as_dataset", (False, True))
def test_format_xindexes(as_dataset: bool) -> None:
expected = """\
Indexes:
x PandasIndex"""
expected = dedent(expected)

obj: xr.DataArray | xr.Dataset = xr.DataArray([1], coords={"x": [1]})
obj = obj._to_temp_dataset() if as_dataset else obj

actual = repr(obj.xindexes)
assert actual == expected
Loading