Python | Pandas Series.to_xarray()
Last Updated :
05 Feb, 2019
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.to_xarray()
function Return an xarray object from the pandas object.
Note : You need to have the xarray
library installed in your computer.
Syntax: Series.to_xarray()
Parameter : None
Returns : xarray.DataArray or xarray.Dataset
Example #1: Use Series.to_xarray()
function to convert the given Series object into an xarray object.
import pandas as pd
sr = pd.Series([ 'New York' , 'Chicago' , 'Toronto' , 'Lisbon' , 'Rio' , 'Moscow' ])
didx = pd.DatetimeIndex(start = '2014-08-01 10:00' , freq = 'W' ,
periods = 6 , tz = 'Europe/Berlin' )
sr.index = didx
print (sr)
|
Output :
Now we will use Series.to_xarray()
function to convert the given series object to xarray object.
Output :
As we can see in the output, the Series.to_xarray()
function has successfully converted the given series object into an xarray object.
Example #2: Use Series.to_xarray()
function to convert the given Series object into an xarray object.
import pandas as pd
sr = pd.Series([ 19.5 , 16.8 , 22.78 , 20.124 , 18.1002 ])
print (sr)
|
Output :
Now we will use Series.to_xarray()
function to convert the given series object to xarray object.
Output :
As we can see in the output, the Series.to_xarray()
function has successfully converted the given series object into an xarray object.
Share your thoughts in the comments
Please Login to comment...