MCPcopy Index your code
hub / github.com/pydata/xarray / diff_indexes_repr

Function diff_indexes_repr

xarray/core/formatting.py:1002–1053  ·  view source on GitHub ↗

Generate diff representation for indexes.

(a_indexes, b_indexes, col_width: int = 20)

Source from the content-addressed store, hash-verified

1000
1001
1002def diff_indexes_repr(a_indexes, b_indexes, col_width: int = 20) -> str:
1003 """Generate diff representation for indexes."""
1004 a_keys = set(a_indexes.keys())
1005 b_keys = set(b_indexes.keys())
1006
1007 summary = []
1008
1009 if only_a := a_keys - b_keys:
1010 summary.append(f"Indexes only on the left object: {sorted(only_a)}")
1011
1012 if only_b := b_keys - a_keys:
1013 summary.append(f"Indexes only on the right object: {sorted(only_b)}")
1014
1015 # Check for indexes on the same coordinates but with different types or values
1016 common_keys = a_keys & b_keys
1017 diff_items = []
1018
1019 for key in sorted(common_keys):
1020 a_idx = a_indexes[key]
1021 b_idx = b_indexes[key]
1022
1023 # Check if indexes differ
1024 indexes_equal = False
1025 if type(a_idx) is type(b_idx):
1026 try:
1027 indexes_equal = a_idx.equals(b_idx)
1028 except NotImplementedError:
1029 # Fall back to variable comparison
1030 a_var = a_indexes.variables[key]
1031 b_var = b_indexes.variables[key]
1032 indexes_equal = a_var.equals(b_var)
1033
1034 if not indexes_equal:
1035 # Format the index values similar to variable diff
1036 try:
1037 a_repr = inline_index_repr(
1038 a_indexes.to_pandas_indexes()[key], max_width=70
1039 )
1040 b_repr = inline_index_repr(
1041 b_indexes.to_pandas_indexes()[key], max_width=70
1042 )
1043 except TypeError:
1044 # Custom indexes may not support to_pandas_index()
1045 a_repr = repr(a_idx)
1046 b_repr = repr(b_idx)
1047 diff_items.append(f"L {key!s:<{col_width}} {a_repr}")
1048 diff_items.append(f"R {key!s:<{col_width}} {b_repr}")
1049
1050 if diff_items:
1051 summary.append("Differing indexes:\n" + "\n".join(diff_items))
1052
1053 return "\n".join(summary)
1054
1055
1056def diff_array_repr(a, b, compat):

Callers 2

diff_array_reprFunction · 0.85
diff_dataset_reprFunction · 0.85

Calls 6

typeFunction · 0.85
inline_index_reprFunction · 0.85
keysMethod · 0.80
to_pandas_indexesMethod · 0.80
equalsMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…