Skip to content

Commit 3f3d312

Browse files
committed
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.
1 parent 3f0dd8d commit 3f3d312

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

docs/src/whatsnew/latest.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ This document explains the changes made to Iris for this release
5656
always promoting the result to ``float64``. Integer inputs are still returned
5757
as ``float64``. (:issue:`4119`)
5858

59+
#. :user:`gaoflow` fixed printing of a length-0 coordinate (e.g. a time
60+
coordinate for an as-yet-unfilled unlimited dimension), which previously
61+
raised a ``ValueError`` instead of producing a summary. (:issue:`6531`)
62+
5963

6064
💣 Incompatible Changes
6165
=======================

lib/iris/coords.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,9 @@ def array_summary(data, n_max, n_edge, linewidth, precision):
406406

407407
if data.dtype.kind == "U":
408408
# Strings : N.B. includes all missing data
409-
# find the longest.
410-
length = max(len(str(x)) for x in data.flatten())
409+
# find the longest (an empty array, e.g. a length-0 time
410+
# coordinate, has no elements, so fall back to 0). See #6531.
411+
length = max((len(str(x)) for x in data.flatten()), default=0)
411412
# Pre-apply a common formatting width.
412413
formatter = {"all": lambda x: str(x).ljust(length)}
413414

lib/iris/tests/unit/coords/test_Coord.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,37 @@ def test_long_time_interval__bounded(self):
14821482
result = coord.__str__()
14831483
assert expected == result
14841484

1485+
def test_empty_time_coord(self):
1486+
# A length-0 time coordinate (an as-yet-unfilled unlimited dimension)
1487+
# should print rather than raising (#6531).
1488+
coord = DimCoord([], standard_name="time", units="days since 1970-01-01")
1489+
expected = "\n".join(
1490+
[
1491+
"DimCoord : time / (days since 1970-01-01, standard calendar)",
1492+
" points: []",
1493+
" shape: (0,)",
1494+
" dtype: float64",
1495+
" standard_name: 'time'",
1496+
]
1497+
)
1498+
result = coord.__str__()
1499+
assert expected == result
1500+
1501+
def test_empty_string_coord(self):
1502+
# A length-0 string coordinate also exercises the empty-array path.
1503+
coord = AuxCoord(np.array([], dtype="<U1"), long_name="label")
1504+
expected = "\n".join(
1505+
[
1506+
"AuxCoord : label / (unknown)",
1507+
" points: []",
1508+
" shape: (0,)",
1509+
" dtype: <U1",
1510+
" long_name: 'label'",
1511+
]
1512+
)
1513+
result = coord.__str__()
1514+
assert expected == result
1515+
14851516
def test_non_time_unit(self):
14861517
coord = DimCoord([1.0])
14871518
expected = "\n".join(

0 commit comments

Comments
 (0)