Pandas: How to convert a DataFrame to an xarray (4 examples)

Updated: February 24, 2024 By: Guest Contributor Post a comment


Working with large data sets often involves leveraging the strengths of different Python libraries. Pandas is renowned for its ease of handling tabular data, while xarray extends these capabilities towards multi-dimensional arrays, making it invaluable for scientific computing. In this tutorial, we’ll explore how to transition seamlessly from Pandas DataFrames to xarray DataArrays and Datasets through four progressive examples. Whether you’re dealing with numerical simulations, statistical models, or complex data analysis, understanding this conversion can greatly enhance your data processing workflow.

Prerequisite: This guide assumes a basic familiarity with Python, Pandas, and xarray. Make sure you have both Pandas and xarray installed in your Python environment. You can install them using pip:

pip install pandas xarray

Example 1: Basic Conversion

Let’s start with the most straightforward example – converting a simple DataFrame to an xarray DataArray. Here, we’ll create a basic DataFrame and then convert it:

import pandas as pd
import xarray as xr

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Convert to xarray DataArray
da = xr.DataArray(df)


   A  B
0  1  4
1  2  5
2  3  6

[[1 4]
 [2 5]
 [3 6]]
Dimensions without coordinates: dim_0, dim_1

This example demonstrates the basic conversion process, effectively translating a DataFrame into a DataArray, though it does not retain column names in the transition.

Example 2: Adding Dimension Names and Coordinates

In our second example, we’re going to add names to the dimensions and coordinates to enrich the DataArray. Modifying our initial example:

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=pd.RangeIndex(start=1, stop=4, name='Id'))
da = xr.DataArray(df, dims=['Id', 'Variables'])
da['Variables'] = ['A', 'B']


[[1 4]
 [2 5]
 [3 6]]
Id: [1, 2, 3]
Variables: ['A', 'B']

This conversion retains more information from the original DataFrame, including the indexes and column names, now referred to as ‘dimensions’ and ‘coordinates’ in the xarray DataArray context. This is especially useful for data with inherent dimensional metadata.

Example 3: Converting to xarray Dataset

While DataArrays are great for handling single dimensional data, xarray’s Dataset can contain multiple variables, which is more akin to a collection of DataArrays. Here’s how to convert a DataFrame to a Dataset:

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}, index=pd.RangeIndex(start=1, stop=4, name='Id'))
ds = xr.Dataset.from_dataframe(df)


Dimensions:  (Id: 3)
  * Id       (Id) int64 1 2 3
Data variables:
    A        (Id) int64 1 2 3
    B        (Id) int64 4 5 6
    C        (Id) int64 7 8 9

This method converts a DataFrame into an xarray Dataset, wherein each column in the DataFrame becomes a separate data variable within the Dataset. This structure is beneficial when working with datasets that contain multiple related variables.

Example 4: Advanced Multi-index DataFrames

For more complex data structures, such as multi-index DataFrames, xarray’s handling of multi-dimensional data comes into its own. Here’s how to convert a DataFrame with a multi-level index:

import numpy as np

df = pd.DataFrame({
  ('A', 'a'): np.random.rand(5),
  ('A', 'b'): np.random.rand(5),
  ('B', 'a'): np.random.rand(5),
  ('B', 'b'): np.random.rand(5)},
  index=pd.MultiIndex.from_product([[1, 2, 3, 4, 5], ['x', 'y']], names=['Number', 'Letter']))
ds = xr.Dataset.from_dataframe(df)


Dimensions:  (Letter: 2, Number: 5)
  * Letter   (Letter) object 'x' 'y'
  * Number   (Number) int64 1 2 3 4 5
Data variables:
    (A, a)   (Number, Letter) float64 ...
    (A, b)   (Number, Letter) float64 ...
    (B, a)   (Number, Letter) float64 ...
    (B, b)   (Number, Letter) float64 ...

This advanced example shows how to handle more complex, hierarchical data within DataFrames. Once converted to an xarray Dataset, it becomes easier to operate on multi-dimensional data, apply group-wise operations, and perform high-level analysis.


Converting Pandas DataFrames to xarray DataArrays or Datasets provides a powerful pathway to working with multi-dimensional data. Throughout these examples, we’ve seen how simple conversions can be, as well as how to handle more complex structures. Whether you’re dealing with basic tables or intricate multi-index DataFrames, understanding these transitions will enhance your data analysis capabilities and open up new avenues for scientific computing.