Skip to content

Variable objects not displayable in notebook #3972

Closed
@arabidopsis

Description

@arabidopsis

class Variable subclasses AbstractArray which has a _repr_html_ method so variable objects
should display in a notebook.

But this fails with a
AttributeError: 'Variable' object has no attribute 'variable':

from os.path import join,dirname
import xarray as xr
ds=join(dirname(xr.__file__), 'tests/data/bears.nc')
xr.open_dataset(ds).variables['cross']

Fix:

change line 186 in xarray.core.formatting_html.py:: array_section
from

preview = escape(inline_variable_array_repr(obj.variable, max_width=70))

to (because obj is a variable):

preview = escape(inline_variable_array_repr(obj.variable if hasattr(obj, "variable") else obj, max_width=70))

I can make a PR if you like.

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.6 | packaged by conda-forge | (default, Mar 23 2020, 23:03:20) 
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 5.3.0-46-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_AU.UTF-8
LOCALE: en_AU.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.7.4

xarray: 0.15.1
pandas: 1.0.3
numpy: 1.18.1
scipy: 1.4.1
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.1.1.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.14.0
distributed: 2.6.0
matplotlib: 3.2.1
cartopy: None
seaborn: 0.10.0
numbagg: None
setuptools: 46.1.3.post20200325
pip: 20.0.2
conda: 4.8.3
pytest: 5.4.1
IPython: 7.13.0
sphinx: 2.2.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions