w3resource

Pandas Series: to_xarray() function

Series - to_xarray() function

The to_xarray() function is used to get an xarray object from the pandas object.

Syntax:

Series.to_xarray(self)

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.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([('eagle', 'bird',  320.0, 2),
                   ('sparrow', 'bird', 30.0, 2),
                   ('tiger',   'mammal', 90.5, 4),
                   ('fox', 'mammal', np.nan, 4)],
                   columns=['name', 'class', 'max_speed',
                            'num_legs'])
df

Output:

   name   class	 max_speed	num_legs
0	eagle	bird   	320.0	         2
1	sparrow	bird	30.0	         2
2	tiger	mammal	90.5	         4
3	fox	    mammal   NaN	         4

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([('eagle', 'bird',  320.0, 2),
                   ('sparrow', 'bird', 30.0, 2),
                   ('tiger',   'mammal', 90.5, 4),
                   ('fox', 'mammal', np.nan, 4)],
                   columns=['name', 'class', 'max_speed',
                            'num_legs'])
df.to_xarray()

Output:

<xarray.Dataset>
Dimensions:    (index: 4)
Coordinates:
  * index      (index) int64 0 1 2 3
Data variables:
    name       (index) object 'eagle' 'sparrow' 'tiger' 'fox'
    class      (index) object 'bird' 'bird' 'mammal' 'mammal'
    max_speed  (index) float64 320.0 30.0 90.5 nan
    num_legs   (index) int64 2 2 4 4

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([('eagle', 'bird',  320.0, 2),
                   ('sparrow', 'bird', 30.0, 2),
                   ('tiger',   'mammal', 90.5, 4),
                   ('fox', 'mammal', np.nan, 4)],
                   columns=['name', 'class', 'max_speed',
                            'num_legs'])
df['max_speed'].to_xarray()

Output:

<xarray.DataArray 'max_speed' (index: 4)>
array([320. ,  30. ,  90.5,   nan])
Coordinates:
  * index    (index) int64 0 1 2 3

Python-Pandas Code:

import numpy as np
import pandas as pd
dates = pd.to_datetime(['2019-01-01', '2019-01-01',
                        '2019-01-02', '2019-01-02'])
df_multiindex = pd.DataFrame({'date': dates,
                  'animal': ['eagle', 'sparrow', 'eagle',
                             'sparrow'],
                  'speed': [320, 30, 330, 25]}).set_index(['date',
                                                  'animal'])
dates = pd.to_datetime(['2019-01-01', '2019-01-01',
                        '2019-01-02', '2019-01-02'])
df_multiindex =  pd.DataFrame({'date': dates,
                  'animal': ['eagle', 'sparrow', 'eagle',
                             'sparrow'],
                  'speed': [320, 30, 330, 25]}).set_index(['date',
                                                  'animal'])
df_multiindex

Output:

                      speed
      date	animal	
2019-01-01	eagle	 320
            sparrow	 30
2019-01-02	eagle	 330
            sparrow	 25

Python-Pandas Code:

import numpy as np
import pandas as pd
dates = pd.to_datetime(['2019-01-01', '2019-01-01',
                        '2019-01-02', '2019-01-02'])
df_multiindex = pd.DataFrame({'date': dates,
                  'animal': ['eagle', 'sparrow', 'eagle',
                             'sparrow'],
                  'speed': [320, 30, 330, 25]}).set_index(['date',
                                                  'animal'])
dates = pd.to_datetime(['2019-01-01', '2019-01-01',
                        '2019-01-02', '2019-01-02'])
df_multiindex =  pd.DataFrame({'date': dates,
                  'animal': ['eagle', 'sparrow', 'eagle',
                             'sparrow'],
                  'speed': [320, 30, 330, 25]}).set_index(['date',
                                                  'animal'])
df_multiindex.to_xarray()

Output:

<xarray.Dataset>
Dimensions:  (animal: 2, date: 2)
Coordinates:
  * date     (date) datetime64[ns] 2019-01-01 2019-01-02
  * animal   (animal) object 'eagle' 'sparrow'
Data variables:
    speed    (date, animal) int64 320 30 330 25

Previous: Series-to_frame() function
Next: Series-to_hdf() function



Follow us on Facebook and Twitter for latest update.