File tree Expand file tree Collapse file tree 3 files changed +7
-0
lines changed Expand file tree Collapse file tree 3 files changed +7
-0
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,8 @@ Bug fixes
54
54
Documentation
55
55
~~~~~~~~~~~~~
56
56
57
+ - Raise a more informative error when :py:meth: `DataArray.to_dataframe ` is
58
+ is called on a scalar (:issue: `4228 `). By `Pieter Gijsbers <https://github.com/pgijsbers >`_.
57
59
58
60
Internal Changes
59
61
~~~~~~~~~~~~~~~~
Original file line number Diff line number Diff line change @@ -2442,6 +2442,8 @@ def to_dataframe(
2442
2442
"cannot convert an unnamed DataArray to a "
2443
2443
"DataFrame: use the ``name`` parameter"
2444
2444
)
2445
+ if self .ndim == 0 :
2446
+ raise ValueError ("cannot convert a scalar to a DataFrame" )
2445
2447
2446
2448
# By using a unique name, we can convert a DataArray into a DataFrame
2447
2449
# even if it shares a name with one of its coordinates.
Original file line number Diff line number Diff line change @@ -3538,6 +3538,9 @@ def test_to_dataframe(self):
3538
3538
with pytest .raises (ValueError , match = "does not match the set of dimensions" ):
3539
3539
arr .to_dataframe (dim_order = ["B" , "A" , "C" ])
3540
3540
3541
+ with pytest .raises (ValueError , match = r"cannot convert a scalar" ):
3542
+ arr .sel (A = "c" , B = 2 ).to_dataframe ()
3543
+
3541
3544
arr .name = None # unnamed
3542
3545
with raises_regex (ValueError , "unnamed" ):
3543
3546
arr .to_dataframe ()
You can’t perform that action at this time.
0 commit comments