def to_pandas(self) -> Self | pd.Series | pd.DataFrame:
        """Convert this array into a pandas object with the same shape.

        The type of the returned object depends on the number of DataArray
        dimensions:

        * 0D -> `xarray.DataArray`
        * 1D -> `pandas.Series`
        * 2D -> `pandas.DataFrame`

        Only works for arrays with 2 or fewer dimensions.

        The DataArray constructor performs the inverse transformation.

        Returns
        -------
        result : DataArray | Series | DataFrame
            DataArray, pandas Series or pandas DataFrame.
        """
        # TODO: consolidate the info about pandas constructors and the
        # attributes that correspond to their indexes into a separate module?
        constructors: dict[int, Callable] = {
            0: lambda x: x,
            1: pd.Series,
            2: pd.DataFrame,
        }
        try:
            constructor = constructors[self.ndim]
        except KeyError as err:
            raise ValueError(
                f"Cannot convert arrays with {self.ndim} dimensions into "
                "pandas objects. Requires 2 or fewer dimensions."
            ) from err
        indexes = [self.get_index(dim) for dim in self.dims]
        if isinstance(self._variable._data, PandasExtensionArray):
            values = self._variable._data.array
        else:
            values = self.values
        pandas_object = constructor(values, *indexes)
        if isinstance(pandas_object, pd.Series):
            pandas_object.name = self.name
        return pandas_object
Convert this array into a pandas object with the same shape.

        The type of the returned object depends on the number of DataArray
        dimensions:

        * 0D -> `xarray.DataArray`
        * 1D -> `pandas.Series`
        * 2D -> `pandas.DataFrame`

        Only works for arrays with 2 or fewer dimensions.

        The DataArray constructor performs the inverse transformation.

        Returns
        -------
        result : DataArray | Series | DataFrame
            DataArray, pandas Series or pandas DataFrame.
        
xarray/core/dataarray.py