Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ This document explains the changes made to Iris for this release
#. `@stephenworsley`_ fixed a bug which caused :meth:`~iris.cube.CubeList.concatenate_cube`
to fail when concatenating over multiple axes. (:pull:`6533`)

#. `@bjlittle`_ fixed :func:`~iris.pandas.as_data_frame` to correctly convert a
scalar cube to a :class:`pandas.DataFrame`. (:issue:`6419`, :pull:`6567`)


💣 Incompatible Changes
=======================

#. N/A


🚀 Performance Enhancements
===========================

Expand Down Expand Up @@ -130,6 +134,7 @@ This document explains the changes made to Iris for this release
#. `@tkknight`_ updated image to ensure it renders correctly using various web browsers
on Windows and Linux. (:pull:`6560`)


💼 Internal
===========

Expand Down
13 changes: 13 additions & 0 deletions lib/iris/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from iris._deprecation import warn_deprecated
from iris.coords import AncillaryVariable, AuxCoord, CellMeasure, DimCoord
from iris.cube import Cube, CubeList
from iris.util import new_axis
from iris.warnings import IrisIgnoringWarning


Expand Down Expand Up @@ -827,6 +828,10 @@ def merge_metadata(meta_var_list):
)
return data_frame

if getattr(cube, "ndim", None) is not None and (is_scalar := cube.ndim == 0):
# promote the scalar cube to a 1D cube, and convert in the same way as a 1D cube
cube = new_axis(cube)

if iris.FUTURE.pandas_ndim:
# Checks
if not isinstance(cube, iris.cube.Cube):
Expand Down Expand Up @@ -893,4 +898,12 @@ def merge_metadata(meta_var_list):

result = data_frame

if is_scalar:
# clear the promoted dimension name associated with any
# indices of the dataframe
if isinstance(result.index, pd.MultiIndex):
result.index.names = [None] * result.index.nlevels
else:
result.index.name = None

return result
19 changes: 19 additions & 0 deletions lib/iris/tests/unit/pandas/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ def test_copy_masked_false(self):
class TestAsDataFrame(tests.IrisTest):
"""Test conversion of 2D cubes to Pandas using as_data_frame()."""

def test_scalar_cube(self):
cube = Cube(1.0)
data_frame = iris.pandas.as_data_frame(cube)
expected_index = [0]
expected_columns = [0]
self.assertArrayEqual(data_frame, cube.data)
self.assertArrayEqual(data_frame.index, expected_index)
self.assertArrayEqual(data_frame.columns, expected_columns)
self.assertIsNone(data_frame.index.name)

def test_no_dim_coords(self):
cube = Cube(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]), long_name="foo")
expected_index = [0, 1]
Expand Down Expand Up @@ -323,6 +333,15 @@ class TestAsDataFrameNDim(tests.IrisTest):
def _activate_pandas_ndim(self, activate_pandas_ndim):
pass

def test_scalar_cube(self):
cube = Cube(1.0, long_name="foo")
data_frame = iris.pandas.as_data_frame(cube)
expected_index = [0]
self.assertArrayEqual(data_frame.foo.values, cube.data)
self.assertEqual(data_frame.index.nlevels, 1)
self.assertIsNone(data_frame.index.names[0])
self.assertArrayEqual(data_frame.index.levels[0], expected_index)

def test_no_dim_coords(self):
cube = Cube(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]), long_name="foo")
expected_dim0 = np.repeat([0, 1], 5)
Expand Down
Loading