Open In App

Convert Numpy Array To Xarray

Xarray is a powerful Python library for working with labeled multi-dimensional arrays. In Python, NumPy provides basic data structures and APIs for working with raw ND arrays, but, in the real world, the data is more complex, in some cases, which are encoded. The data array maps to positions in space, time, etc.

The process of converting a NumPy array to an xarray (short for “extended array”) involves creating an xarray DataArray from the existing NumPy array. Xarray, extends the capabilities of NumPy arrays by adding labels to dimensions, enabling easier handling of multidimensional labeled data.



In this post, I will show, how you can convert a NumPy array to an Xarray.

Converting NumPy to Xarray: A Step-by-Step Guide

The foundational step involves creating an Xarray DataArray from an existing NumPy array. This straightforward conversion lays the groundwork for leveraging Xarray’s extended functionalities.






import numpy as np
import xarray as xr
 
numpy_array = np.array([[1, 2, 3], [4, 5, 6]])
 
# Converting NumPy array to Xarray DataArray
xarray_dataarray = xr.DataArray(numpy_array)
 
# Printing the numpy array and its shape
print(numpy_array)
print(numpy_array.shape)
 
 
# Printing the resulting Xarray DataArray and its shape
print(xarray_dataarray)
print(xarray_dataarray.shape)

Output:

[[1 2 3]
[4 5 6]]
(2, 3)
<xarray.DataArray (dim_0: 2, dim_1: 3)>
array([[1, 2, 3],
[4, 5, 6]])
Dimensions without coordinates: dim_0, dim_1
(2, 3)

Xarray provides a versatile set of operations that empower users to manipulate and analyze multidimensional labeled data efficiently. Let’s brief descriptions of some key operations:

Adding coordinates

Xarray allows users to enrich their data arrays by adding coordinates. Coordinates are labels associated with each dimension, providing valuable context and meaning to the data. This facilitates enhanced interpretability and enables more intuitive indexing and slicing of arrays.




import numpy as np
import xarray as xr
 
numpy_array = np.array([[1, 2, 3], [4, 5, 6]])
coords= {
    'latitude':[0,1],
    'longitude':[10,20,30]
}
xarray_dataarray= xr.DataArray(numpy_array,coords)
print(numpy_array)
print(numpy_array.shape)
print(xarray_dataarray)

Output:

[[1 2 3]
[4 5 6]]
(2, 3)
<xarray.DataArray (latitude: 2, longitude: 3)>
array([[1, 2, 3],
[4, 5, 6]])
Coordinates:
* latitude (latitude) int64 0 1
* longitude (longitude) int64 10 20 30

The output represents a 2×3 array converted to an Xarray with latitude and longitude coordinates, enhancing spatial context for data interpretation.

Adding Dimension




import numpy as np
import xarray as xr
 
numpy_array = np.array([[1, 2, 3], [4, 5, 6]])
dimension = ['row', 'column']
xarray_dataarray= xr.DataArray(numpy_array,dims=dimension)
print(numpy_array)
print(xarray_dataarray)

[[1 2 3]
[4 5 6]]
<xarray.DataArray (row: 2, column: 3)>
array([[1, 2, 3],
[4, 5, 6]])
Dimensions without coordinates: row, column

Adding Attributes




import numpy as np
import xarray as xr
 
numpy_array = np.array([[1, 2, 3], [4, 5, 6]])
attrs = {'description': ' DataArray attributes Example',
         'units': 'meters'}
 
xarray_dataarray = xr.DataArray(numpy_array, attrs=attrs)
print(xarray_dataarray)

Output:

xarray.DataArray (dim_0: 2, dim_1: 3)>
array([[1, 2, 3],
[4, 5, 6]])
Dimensions without coordinates: dim_0, dim_1
Attributes:
description: DataArray attributes Example
units: meters

Conclusion

To sum up, here are some methods of converting a NumPy array to Xarray in Python. Choose the best-fit approach such as adding coordinates or adding dimensions, anything depending on whatever you want.


Article Tags :