Repository: pydata/xarray

Issue:
to_dataframe: no valid index for a 0-dimensional object
**What happened**: 
`xr.DataArray([1], coords=[('onecoord', [2])]).sel(onecoord=2).to_dataframe(name='name')` raise an exception `ValueError: no valid index for a 0-dimensional object`

**What you expected to happen**:

the same behavior as: `xr.DataArray([1], coords=[('onecoord', [2])]).to_dataframe(name='name')`

**Anything else we need to know?**:

I see that the array after the selection has no "dims" anymore, and this is what cause the error.  but it still has one "coords", this is confusing.  Is there any documentation about this difference ?

**Environment**:

<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.7.6 | packaged by conda-forge | (default, Jun  1 2020, 18:57:50) 
[GCC 7.5.0]
python-bits: 64
OS: Linux
OS-release: 4.19.0-9-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.7.4

xarray: 0.15.1
pandas: 1.0.4
numpy: 1.18.5
scipy: 1.4.1
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: 2.4.0
cftime: 1.1.3
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2.18.1
distributed: 2.18.0
matplotlib: 3.2.1
cartopy: None
seaborn: 0.10.1
numbagg: None
setuptools: 47.3.1.post20200616
pip: 20.1.1
conda: 4.8.3
pytest: 5.4.3
IPython: 7.15.0
sphinx: 3.1.1

</details>


Discussion:
max-sixty: Thanks @PGijsbers and welcome to xarray!

>  I was not sure if this was "notable". Please let me know if I should add.

Please feel free to!
PGijsbers: Which header should it be under? I would assume documentation or bug fix?
max-sixty: Good question — I would say documentation

Patch:
diff --git a/doc/whats-new.rst b/doc/whats-new.rst
index f6f087cc..2ba75a44 100644
--- a/doc/whats-new.rst
+++ b/doc/whats-new.rst
@@ -54,6 +54,8 @@ Bug fixes
 Documentation
 ~~~~~~~~~~~~~
 
+- Raise a more informative error when :py:meth:`DataArray.to_dataframe` is
+  is called on a scalar (:issue:`4228`). By `Pieter Gijsbers <https://github.com/pgijsbers>`_.
 
 Internal Changes
 ~~~~~~~~~~~~~~~~
diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py
index a2acb858..f2a42074 100644
--- a/xarray/core/dataarray.py
+++ b/xarray/core/dataarray.py
@@ -2442,6 +2442,8 @@ class DataArray(AbstractArray, DataWithCoords):
                 "cannot convert an unnamed DataArray to a "
                 "DataFrame: use the ``name`` parameter"
             )
+        if self.ndim == 0:
+            raise ValueError("cannot convert a scalar to a DataFrame")
 
         # By using a unique name, we can convert a DataArray into a DataFrame
         # even if it shares a name with one of its coordinates.


Test patch:
diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py
index ea142bb9..78b12ddd 100644
--- a/xarray/tests/test_dataarray.py
+++ b/xarray/tests/test_dataarray.py
@@ -3538,6 +3538,9 @@ class TestDataArray:
         with pytest.raises(ValueError, match="does not match the set of dimensions"):
             arr.to_dataframe(dim_order=["B", "A", "C"])
 
+        with pytest.raises(ValueError, match=r"cannot convert a scalar"):
+            arr.sel(A="c", B=2).to_dataframe()
+
         arr.name = None  # unnamed
         with raises_regex(ValueError, "unnamed"):
             arr.to_dataframe()