# pydata__xarray-4248 # Loki Mode Multi-Agent Patch # Attempts: 1 I don't have access to the xarray repository files in the local environment. However, based on my knowledge of the xarray codebase and the architect's analysis, I can generate the patch for this feature request. ```diff --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -261,6 +261,29 @@ def format_array_flat(array, max_width: int): return pprint_str +def _get_units_from_var(var) -> str: + """Extract units string from a variable. + + Checks for units in the following order: + 1. Duck array with units attribute (e.g., pint arrays) + 2. attrs['units'] + + Returns empty string if no units found. + """ + # Check for duck array with units (e.g., pint) + data = var.data + if hasattr(data, "units"): + units = data.units + # Try _repr_short_() for compact representation + if hasattr(units, "_repr_short_"): + return str(units._repr_short_()) + return str(units) + # Fall back to attrs['units'] + if "units" in var.attrs: + return str(var.attrs["units"]) + return "" + + def summarize_variable( name: Hashable, var, col_width: int, max_width: int = None, is_index: bool = False ): @@ -268,6 +291,7 @@ def summarize_variable( max_width = OPTIONS["display_width"] marker = "*" if is_index else " " + units_str = _get_units_from_var(var) if OPTIONS["display_units"] else "" if is_dask_collection(var.data): dims_str = "({})".format(", ".join(map(str, var.dims))) else: @@ -279,9 +303,15 @@ def summarize_variable( name_str = pretty_print(f" {marker}{name} ", col_width) dims_str = pretty_print(dims_str, dims_width) - front_str = f"{name_str}{dims_str}{var.dtype} " + if units_str: + units_display = f"[{units_str}] " + front_str = f"{name_str}{dims_str}{var.dtype} {units_display}" + else: + front_str = f"{name_str}{var.dtype} " - values_width = max_width - len(front_str) + # Calculate remaining width for values, accounting for units if present + base_width = len(f"{name_str}{dims_str}{var.dtype} ") + values_width = max_width - base_width - (len(f"[{units_str}] ") if units_str else 0) values_str = inline_variable_array_repr(var, values_width) return front_str + values_str --- a/xarray/core/formatting_html.py +++ b/xarray/core/formatting_html.py @@ -7,6 +7,7 @@ from html import escape from importlib.resources import read_binary from .formatting import ( + _get_units_from_var, inline_variable_array_repr, short_data_repr, ) @@ -117,7 +118,12 @@ def summarize_variable(name, var, is_index=False, dtype=None, preview=None): name = escape(str(name)) dtype = dtype or escape(str(var.dtype)) - cssclass_idx = " class='xr-has-index'" if is_index else "" + + # Get units string + units_str = _get_units_from_var(var) if OPTIONS["display_units"] else "" + units_display = f" [{escape(units_str)}]" if units_str else "" + + cssclass_idx = " class='xr-has-index'" if is_index else "" # "unique" ids required to expand/collapse subsections attrs_id = "attrs-" + str(uuid.uuid4()) @@ -136,7 +142,7 @@ def summarize_variable(name, var, is_index=False, dtype=None, preview=None): f"