xp1632 / VPE_IP

0 stars 0 forks source link

Differences between `numpy ndarray` and `xarray` #63

Open xp1632 opened 4 months ago

xp1632 commented 4 months ago


The differences between numpy ndarray and xarray are:

# NumPy ndarray
import numpy as np

# Creating a 2D NumPy array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Accessing elements using positional indices
print(arr[0, 1])  # Output: 2

# Xarray DataArray
import xarray as xr

# Creating a 2D DataArray with labeled dimensions and coordinates
data_arr = xr.DataArray(arr, dims=("x", "y"), coords={"x": [0, 1], "y": [0, 1, 2]})

# Accessing elements using dimension names and coordinates
print(data_arr.sel(x=0, y=1).values)  # Output: 2