From 3f3d31217cdf21ab990c1ea7e05fb77fdb0bdceb Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Mon, 1 Jun 2026 14:05:53 +0200 Subject: [PATCH] Allow printing of a length-0 coordinate Coord.summary() formats string and date-like values by first finding the longest string with max(...), which raised ValueError on an empty array. A length-0 coordinate is a valid object (e.g. a time coordinate for an unfilled unlimited dimension), so its summary should not crash. Pass default=0 to max() so an empty array formats as '[]'. Fixes #6531. --- docs/src/whatsnew/latest.rst | 4 +++ lib/iris/coords.py | 5 ++-- lib/iris/tests/unit/coords/test_Coord.py | 31 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 12720cecae..eda397fcf2 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -56,6 +56,10 @@ This document explains the changes made to Iris for this release always promoting the result to ``float64``. Integer inputs are still returned as ``float64``. (:issue:`4119`) +#. :user:`gaoflow` fixed printing of a length-0 coordinate (e.g. a time + coordinate for an as-yet-unfilled unlimited dimension), which previously + raised a ``ValueError`` instead of producing a summary. (:issue:`6531`) + 💣 Incompatible Changes ======================= diff --git a/lib/iris/coords.py b/lib/iris/coords.py index da712d6e2a..5f35fc0a5f 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -406,8 +406,9 @@ def array_summary(data, n_max, n_edge, linewidth, precision): if data.dtype.kind == "U": # Strings : N.B. includes all missing data - # find the longest. - length = max(len(str(x)) for x in data.flatten()) + # find the longest (an empty array, e.g. a length-0 time + # coordinate, has no elements, so fall back to 0). See #6531. + length = max((len(str(x)) for x in data.flatten()), default=0) # Pre-apply a common formatting width. formatter = {"all": lambda x: str(x).ljust(length)} diff --git a/lib/iris/tests/unit/coords/test_Coord.py b/lib/iris/tests/unit/coords/test_Coord.py index 697a6f86d4..28d0689288 100644 --- a/lib/iris/tests/unit/coords/test_Coord.py +++ b/lib/iris/tests/unit/coords/test_Coord.py @@ -1482,6 +1482,37 @@ def test_long_time_interval__bounded(self): result = coord.__str__() assert expected == result + def test_empty_time_coord(self): + # A length-0 time coordinate (an as-yet-unfilled unlimited dimension) + # should print rather than raising (#6531). + coord = DimCoord([], standard_name="time", units="days since 1970-01-01") + expected = "\n".join( + [ + "DimCoord : time / (days since 1970-01-01, standard calendar)", + " points: []", + " shape: (0,)", + " dtype: float64", + " standard_name: 'time'", + ] + ) + result = coord.__str__() + assert expected == result + + def test_empty_string_coord(self): + # A length-0 string coordinate also exercises the empty-array path. + coord = AuxCoord(np.array([], dtype="