tml)
  * [ Resampling ](../resampling.html)
  * [ Style ](../style.html)
  * [ Plotting ](../plotting.html)
  * [ Options and settings ](../options.html)
  * [ Extensions ](../extensions.html)
  * [ Testing ](../testing.html)
  * [ Missing values ](../missing_value.html)

  * [ __ ](../../index.html)
  * [ API reference ](../index.html)
  * [ DataFrame ](../frame.html)
  * pandas.DataF... 

#  pandas.DataFrame.to_xarray  #

DataFrame.  to_xarray  (  )  [ [source]  ](https://github.com/pandas-
dev/pandas/blob/v2.2.2/pandas/core/generic.py#L3249-L3331) #

    

Return an xarray object from the pandas object.

Returns  :

    

xarray.DataArray or xarray.Dataset

    

Data in the pandas structure converted to Dataset if the object is a
DataFrame, or a DataArray if the object is a Series.

See also

[ ` DataFrame.to_hdf  ` ](pandas.DataFrame.to_hdf.html#pandas.DataFrame.to_hdf
"pandas.DataFrame.to_hdf")

    

Write DataFrame to an HDF5 file.

[ ` DataFrame.to_parquet  `
](pandas.DataFrame.to_parquet.html#pandas.DataFrame.to_parquet
"pandas.DataFrame.to_parquet")

    

Write a DataFrame to the binary parquet format.

Notes

See the [ xarray docs ](https://xarray.pydata.org/en/stable/)

Examples

    
    
    >>> df = pd.DataFrame([('falcon', 'bird', 389.0, 2),
    ...                    ('parrot', 'bird', 24.0, 2),
    ...                    ('lion', 'mammal', 80.5, 4),
    ...                    ('monkey', 'mammal', np.nan, 4)],
    ...                   columns=['name', 'class', 'max_speed',
    ...                            'num_legs'])
    >>> df
         name   class  max_speed  num_legs
    0  falcon    bird      389.0         2
    1  parrot    bird       24.0         2
    2    lion  mammal       80.5         4
    3  monkey  mammal        NaN         4
    
    
    
    >>> df.to_xarray()  
    <xarray.Dataset>
    Dimensions:    (index: 4)
    Coordinates:
      * index      (index) int64 32B 0 1 2 3
    Data variables:
        name       (index) object 32B 'falcon' 'parrot' 'lion' 'monkey'
        class      (index) object 32B 'bird' 'bird' 'mammal' 'mammal'
        max_speed  (index) float64 32B 389.0 24.0 80.5 nan
        num_legs   (index) int64 32B 2 2 4 4
    
    
    
    >>> df['max_speed'].to_xarray()  
    <xarray.DataArray 'max_speed' (index: 4)>
    array([389. ,  24. ,  80.5,   nan])
    Coordinates:
      * index    (index) int64 0 1 2 3
    
    
    
    >>> dates = pd.to_datetime(['2018-01-01', '2018-01-01',
    ...                         '2018-01-02', '2018-01-02'])
    >>> df_multiindex = pd.DataFrame({'date': dates,
    ...                               'animal': ['falcon', 'parrot',
    ...                                          'falcon', 'parrot'],
    ...                               'speed': [350, 18, 361, 15]})
    >>> df_multiindex = df_multiindex.set_index(['date', 'animal'])
    
    
    
    >>> df_multiindex
                       speed
    date       animal
    2018-01-01 falcon    350
               parrot     18
    2018-01-02 falcon    361
               parrot     15
    
    
    
    >>> df_multiindex.to_xarray()  
    <xarray.Dataset>
    Dimensions:  (date: 2, animal: 2)
    Coordinates:
      * date     (date) datetime64[ns] 2018-01-01 2018-01-02
      * animal   (animal) object 'falcon' 'parrot'
    Data variables:
        speed    (date, animal) int64 350 18 361 15
    

[ __ previous  pandas.DataFrame.squeeze  ](pandas.DataFrame.squeeze.html
"previous page") [ next  pandas.DataFrame.T  __ ](pandas.DataFrame.T.html
"next page")

__ On this page

  * ` DataFrame.to_xarray()  `

[ __ Show Source
](../../_sources/reference/api/pandas.DataFrame.to_xarray.rst.txt)

© 2024, pandas via [ NumFOCUS, Inc. ](https://numfocus.org) Hosted by [
OVHcloud ](https://www.ovhcloud.com) .

Created using [ Sphinx ](https://www.sphinx-doc.org/) 7.2.6.  

Built with the [ PyData Sphinx Theme ](https://pydata-sphinx-
theme.readthedocs.io/en/stable/index.html) 0.14.4.