Skip to content

html repr fails on non-str Dataset keys #3833

@max-sixty

Description

@max-sixty

MCVE Code Sample

# In a notebook with html repr enabled
xr.Dataset({0: (('a','b'), np.random.rand(2,3))})

# gives:


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/j/office/app/research-python/conda/envs/2019.10/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

~/.local/lib/python3.7/site-packages/xarray/core/dataset.py in _repr_html_(self)
   1632         if OPTIONS["display_style"] == "text":
   1633             return f"<pre>{escape(repr(self))}</pre>"
-> 1634         return formatting_html.dataset_repr(self)
   1635 
   1636     def info(self, buf=None) -> None:

~/.local/lib/python3.7/site-packages/xarray/core/formatting_html.py in dataset_repr(ds)
    268         dim_section(ds),
    269         coord_section(ds.coords),
--> 270         datavar_section(ds.data_vars),
    271         attr_section(ds.attrs),
    272     ]

~/.local/lib/python3.7/site-packages/xarray/core/formatting_html.py in _mapping_section(mapping, name, details_func, max_items_collapse, enabled)
    165     return collapsible_section(
    166         name,
--> 167         details=details_func(mapping),
    168         n_items=n_items,
    169         enabled=enabled,

~/.local/lib/python3.7/site-packages/xarray/core/formatting_html.py in summarize_vars(variables)
    131     vars_li = "".join(
    132         f"<li class='xr-var-item'>{summarize_variable(k, v)}</li>"
--> 133         for k, v in variables.items()
    134     )
    135 

~/.local/lib/python3.7/site-packages/xarray/core/formatting_html.py in <genexpr>(.0)
    131     vars_li = "".join(
    132         f"<li class='xr-var-item'>{summarize_variable(k, v)}</li>"
--> 133         for k, v in variables.items()
    134     )
    135 

~/.local/lib/python3.7/site-packages/xarray/core/formatting_html.py in summarize_variable(name, var, is_index, dtype, preview)
     96     cssclass_idx = " class='xr-has-index'" if is_index else ""
     97     dims_str = f"({', '.join(escape(dim) for dim in var.dims)})"
---> 98     name = escape(name)
     99     dtype = dtype or escape(str(var.dtype))
    100 

/j/office/app/research-python/conda/envs/2019.10/lib/python3.7/html/__init__.py in escape(s, quote)
     17     translated.
     18     """
---> 19     s = s.replace("&", "&amp;") # Must be done first!
     20     s = s.replace("<", "&lt;")
     21     s = s.replace(">", "&gt;")

AttributeError: 'int' object has no attribute 'replace'

<xarray.Dataset>
Dimensions:  (a: 2, b: 3)
Dimensions without coordinates: a, b
Data variables:
    0        (a, b) float64 0.5327 0.927 0.8582 0.8825 0.9478 0.09475

Problem Description

I think this may be an uncomplicated fix: coerce the keys to str

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21) [GCC 7.3.0] python-bits: 64 OS: Linux OS-release: ... machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.utf8 LOCALE: en_US.UTF-8 libhdf5: 1.10.5 libnetcdf: 4.7.1

xarray: 0.15.0
pandas: 1.0.1
numpy: 1.17.3
scipy: 1.3.2
netCDF4: 1.5.3
pydap: None
h5netcdf: 0.7.4
h5py: 2.10.0
Nio: None
zarr: None
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.2.1
dask: 2.7.0
distributed: 2.7.0
matplotlib: 3.1.2
cartopy: None
seaborn: 0.9.0
numbagg: installed
setuptools: 41.6.0.post20191101
pip: 19.3.1
conda: None
pytest: 5.2.2
IPython: 7.9.0
sphinx: 2.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions