Skip to content
Open
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
4 changes: 4 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======================
Expand Down
5 changes: 3 additions & 2 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}

Expand Down
31 changes: 31 additions & 0 deletions lib/iris/tests/unit/coords/test_Coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<U1"), long_name="label")
expected = "\n".join(
[
"AuxCoord : label / (unknown)",
" points: []",
" shape: (0,)",
" dtype: <U1",
" long_name: 'label'",
]
)
result = coord.__str__()
assert expected == result

def test_non_time_unit(self):
coord = DimCoord([1.0])
expected = "\n".join(
Expand Down
Loading