Skip to content

Allow printing of a length-0 coordinate (#6531)#7122

Open
gaoflow wants to merge 1 commit into
SciTools:mainfrom
gaoflow:fix/print-empty-time-coord
Open

Allow printing of a length-0 coordinate (#6531)#7122
gaoflow wants to merge 1 commit into
SciTools:mainfrom
gaoflow:fix/print-empty-time-coord

Conversation

@gaoflow
Copy link
Copy Markdown

@gaoflow gaoflow commented Jun 1, 2026

🚀 Pull Request

Description

Closes #6531.

Printing a length-0 coordinate raised a ValueError instead of producing a summary:

>>> from iris.coords import DimCoord
>>> t_coord = DimCoord([], standard_name="time", units="days since 1999-01-01")
>>> print(t_coord)
ValueError: max() iterable argument is empty

As confirmed by @SciTools/peloton on the issue, a length-0 time coordinate is a valid object — it represents an unlimited dimension that does not yet have any points — so printing it should not crash.

Cause

Coord.summary() formats string- and date-like values by first finding the longest element so it can apply a common width:

length = max(len(str(x)) for x in data.flatten())

For an empty array this generator is empty, so max() raises. (Date-like coordinates reach this branch because num2date converts the points to strings first; the original report's vectorize/otypes error no longer occurs with current cf-units, leaving this max() call as the remaining failure.)

Fix

Pass default=0 to max(), so an empty array yields a zero width and formats as []. Non-empty coordinates are unaffected.

>>> print(t_coord)
DimCoord :  time / (days since 1999-01-01, standard calendar)
    points: []
    shape: (0,)
    dtype: float64
    standard_name: 'time'

Verification

  • New tests Test___str__::test_empty_time_coord and ::test_empty_string_coord (both date-like and plain-string empty paths). They fail on main and pass here.
  • Full tests/unit/coords/ and tests/unit/representation/ suites pass (470 tests).
  • ruff check / ruff format clean.

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 SciTools#6531.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Time coordinate with length 0 cannot be printed

1 participant