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

Method to_dataframe

xarray/core/dataarray.py:3961–4023  ·  view source on GitHub ↗

Convert this array and its coordinates into a tidy pandas.DataFrame. The DataFrame is indexed by the Cartesian product of index coordinates (in the form of a :py:class:`pandas.MultiIndex`). Other coordinates are included as columns in the DataFrame. For 1D and 2D Da

(
        self, name: Hashable | None = None, dim_order: Sequence[Hashable] | None = None
    )

Source from the content-addressed store, hash-verified

3959 return pandas_object
3960
3961 def to_dataframe(
3962 self, name: Hashable | None = None, dim_order: Sequence[Hashable] | None = None
3963 ) -> pd.DataFrame:
3964 """Convert this array and its coordinates into a tidy pandas.DataFrame.
3965
3966 The DataFrame is indexed by the Cartesian product of index coordinates
3967 (in the form of a :py:class:`pandas.MultiIndex`). Other coordinates are
3968 included as columns in the DataFrame.
3969
3970 For 1D and 2D DataArrays, see also :py:func:`DataArray.to_pandas` which
3971 doesn't rely on a MultiIndex to build the DataFrame.
3972
3973 Parameters
3974 ----------
3975 name: Hashable or None, optional
3976 Name to give to this array (required if unnamed).
3977 dim_order: Sequence of Hashable or None, optional
3978 Hierarchical dimension order for the resulting dataframe.
3979 Array content is transposed to this order and then written out as flat
3980 vectors in contiguous order, so the last dimension in this list
3981 will be contiguous in the resulting DataFrame. This has a major
3982 influence on which operations are efficient on the resulting
3983 dataframe.
3984
3985 If provided, must include all dimensions of this DataArray. By default,
3986 dimensions are sorted according to the DataArray dimensions order.
3987
3988 Returns
3989 -------
3990 result: DataFrame
3991 DataArray as a pandas DataFrame.
3992
3993 See Also
3994 --------
3995 DataArray.to_pandas
3996 DataArray.to_series
3997 """
3998 if name is None:
3999 name = self.name
4000 if name is None:
4001 raise ValueError(
4002 "cannot convert an unnamed DataArray to a "
4003 "DataFrame: use the ``name`` parameter"
4004 )
4005 if self.ndim == 0:
4006 raise ValueError("cannot convert a scalar to a DataFrame")
4007
4008 # By using a unique name, we can convert a DataArray into a DataFrame
4009 # even if it shares a name with one of its coordinates.
4010 # I would normally use unique_name = object() but that results in a
4011 # dataframe with columns in the wrong order, for reasons I have not
4012 # been able to debug (possibly a pandas bug?).
4013 unique_name = "__unique_name_identifier_z98xfz98xugfg73ho__"
4014 ds = self._to_dataset_whole(name=unique_name)
4015
4016 if dim_order is None:
4017 ordered_dims = dict(zip(self.dims, self.shape, strict=True))
4018 else:

Calls 3

_to_dataset_wholeMethod · 0.95
_normalize_dim_orderMethod · 0.80
_to_dataframeMethod · 0.80