MCPcopy
hub / github.com/pydata/xarray / concat

Method concat

xarray/core/variable.py:2835–2884  ·  view source on GitHub ↗

Specialized version of Variable.concat for IndexVariable objects. This exists because we want to avoid converting Index objects to NumPy arrays, if possible.

(
        cls,
        variables,
        dim="concat_dim",
        positions=None,
        shortcut=False,
        combine_attrs="override",
    )

Source from the content-addressed store, hash-verified

2833
2834 @classmethod
2835 def concat(
2836 cls,
2837 variables,
2838 dim="concat_dim",
2839 positions=None,
2840 shortcut=False,
2841 combine_attrs="override",
2842 ):
2843 """Specialized version of Variable.concat for IndexVariable objects.
2844
2845 This exists because we want to avoid converting Index objects to NumPy
2846 arrays, if possible.
2847 """
2848 from xarray.structure.merge import merge_attrs
2849
2850 if not isinstance(dim, str):
2851 (dim,) = dim.dims
2852
2853 variables = list(variables)
2854 first_var = variables[0]
2855
2856 if any(not isinstance(v, cls) for v in variables):
2857 raise TypeError(
2858 "IndexVariable.concat requires that all input "
2859 "variables be IndexVariable objects"
2860 )
2861
2862 indexes = [v._data.array for v in variables]
2863
2864 if not indexes:
2865 data = []
2866 else:
2867 data = indexes[0].append(indexes[1:])
2868
2869 if positions is not None:
2870 indices = nputils.inverse_permutation(np.concatenate(positions))
2871 data = data.take(indices)
2872
2873 # keep as str if possible as pandas.Index uses object (converts to numpy array)
2874 data = maybe_coerce_to_str(data, variables)
2875
2876 attrs = merge_attrs(
2877 [var.attrs for var in variables], combine_attrs=combine_attrs
2878 )
2879 if not shortcut:
2880 for var in variables:
2881 if var.dims != first_var.dims:
2882 raise ValueError("inconsistent dimensions")
2883
2884 return cls(first_var.dims, data, attrs)
2885
2886 def copy(
2887 self, deep: bool = True, data: T_DuckArray | np.typing.ArrayLike | None = None

Callers

nothing calls this directly

Calls 2

maybe_coerce_to_strFunction · 0.90
merge_attrsFunction · 0.90

Tested by

no test coverage detected