Skip to content

Commit c2786eb

Browse files
committed
Simpler: pass to numpy in all cases
1 parent bee7670 commit c2786eb

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

xarray/core/common.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,7 @@ def _repr_html_(self):
156156
return formatting_html.array_repr(self)
157157

158158
def __format__(self: Any, format_spec: str) -> str:
159-
if not format_spec:
160-
# format without specifier falls backs to standard repr
161-
return formatting.array_repr(self)
162-
# else use numpy: scalars will print fine and arrays will raise
159+
# we use numpy: scalars will print fine and arrays will raise
163160
return self.values.__format__(format_spec)
164161

165162
def _iter(self: Any) -> Iterator[Any]:

xarray/tests/test_formatting.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,12 @@ def test_array_repr_variable(self) -> None:
415415

416416
def test_array_scalar_format(self) -> None:
417417
var = xr.DataArray(0)
418-
assert var.__format__("") == "<xarray.DataArray ()>\narray(0)"
418+
assert var.__format__("") == "0"
419419
assert var.__format__("d") == "0"
420420
assert var.__format__(".2f") == "0.00"
421421

422-
var = xr.DataArray([0])
423-
assert var.__format__("") == (
424-
"<xarray.DataArray (dim_0: 1)>\narray([0])"
425-
"\nDimensions without coordinates: dim_0"
426-
)
422+
var = xr.DataArray([0.1, 0.2])
423+
assert var.__format__("") == "[0.1 0.2]"
427424
with pytest.raises(TypeError) as excinfo:
428425
var.__format__(".2f")
429426
assert "unsupported format string passed to" in str(excinfo.value)

0 commit comments

Comments
 (0)