diff --git a/pandas/core/generic.py b/pandas/core/generic.py index accb7d0db1d2c..a674c3ca28ae8 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1501,7 +1501,17 @@ def to_xarray(self): ----- See the `xarray docs `__ """ - import xarray + + try: + import xarray + except ImportError: + # Give a nice error message + raise ImportError("the xarray library is not installed\n" + "you can install via conda\n" + "conda install xarray\n" + "or via pip\n" + "pip install xarray\n") + if self.ndim == 1: return xarray.DataArray.from_series(self) elif self.ndim == 2: