Skip to content

da.xindexes or da.indexes raises an error if there are none (in the repr) #8367

Closed
@mathause

Description

@mathause

What happened?

da.xindexes or da.indexes raises an error when trying to generate the repr if there are no coords (indexes)

What did you expect to happen?

Displaying an empty Mappable?

Minimal Complete Verifiable Example

xr.DataArray([3, 5]).indexes
xr.DataArray([3, 5]).xindexes

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

Out[9]: ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File ~/.conda/envs/xarray_dev/lib/python3.10/site-packages/IPython/core/formatters.py:708, in PlainTextFormatter.__call__(self, obj)
    701 stream = StringIO()
    702 printer = pretty.RepresentationPrinter(stream, self.verbose,
    703     self.max_width, self.newline,
    704     max_seq_length=self.max_seq_length,
    705     singleton_pprinters=self.singleton_printers,
    706     type_pprinters=self.type_printers,
    707     deferred_pprinters=self.deferred_printers)
--> 708 printer.pretty(obj)
    709 printer.flush()
    710 return stream.getvalue()

File ~/.conda/envs/xarray_dev/lib/python3.10/site-packages/IPython/lib/pretty.py:410, in RepresentationPrinter.pretty(self, obj)
    407                         return meth(obj, self, cycle)
    408                 if cls is not object \
    409                         and callable(cls.__dict__.get('__repr__')):
--> 410                     return _repr_pprint(obj, self, cycle)
    412     return _default_pprint(obj, self, cycle)
    413 finally:

File ~/.conda/envs/xarray_dev/lib/python3.10/site-packages/IPython/lib/pretty.py:778, in _repr_pprint(obj, p, cycle)
    776 """A pprint that just redirects to the normal repr function."""
    777 # Find newlines and replace them with p.break_()
--> 778 output = repr(obj)
    779 lines = output.splitlines()
    780 with p.group():

File ~/code/xarray/xarray/core/indexes.py:1659, in Indexes.__repr__(self)
   1657 def __repr__(self):
   1658     indexes = formatting._get_indexes_dict(self)
-> 1659     return formatting.indexes_repr(indexes)

File ~/code/xarray/xarray/core/formatting.py:474, in indexes_repr(indexes, max_rows)
    473 def indexes_repr(indexes, max_rows: int | None = None) -> str:
--> 474     col_width = _calculate_col_width(chain.from_iterable(indexes))
    476     return _mapping_repr(
    477         indexes,
    478         "Indexes",
   (...)
    482         max_rows=max_rows,
    483     )

File ~/code/xarray/xarray/core/formatting.py:341, in _calculate_col_width(col_items)
    340 def _calculate_col_width(col_items):
--> 341     max_name_length = max(len(str(s)) for s in col_items) if col_items else 0
    342     col_width = max(max_name_length, 7) + 6
    343     return col_width

ValueError: max() arg is an empty sequence

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: ccc8f99
python: 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:40:32) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 6.2.0-35-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.2
libnetcdf: 4.9.2

xarray: 2023.9.1.dev8+gf6d69a1f
pandas: 2.1.1
numpy: 1.24.4
scipy: 1.11.3
netCDF4: 1.6.4
pydap: installed
h5netcdf: 1.2.0
h5py: 3.9.0
Nio: None
zarr: 2.16.1
cftime: 1.6.2
nc_time_axis: 1.4.1
PseudoNetCDF: 3.2.2
iris: 3.7.0
bottleneck: 1.3.7
dask: 2023.9.2
distributed: None
matplotlib: 3.8.0
cartopy: 0.22.0
seaborn: 0.12.2
numbagg: 0.2.2
fsspec: 2023.9.2
cupy: None
pint: 0.20.1
sparse: 0.14.0
flox: 0.7.2
numpy_groupies: 0.10.1
setuptools: 68.2.2
pip: 23.2.1
conda: None
pytest: 7.4.2
mypy: 1.5.1
IPython: 8.15.0
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions