Welcome to the Xarray Cheat Code! This quick guide includes essential functions and operations with concise explanations and examples. Copy these snippets to learn or enhance your data manipulation skills with Xarray.
1. Import Xarray
import xarray as xr
2. Open a NetCDF File
ds = xr.open_dataset('file.nc')
print(ds)
3. Create a DataArray
import numpy as np
arr = xr.DataArray(np.random.rand(3, 3), dims=['x', 'y'], coords={'x': [1, 2, 3], 'y': [10, 20, 30]})
print(arr)
4. Create a Dataset
data = xr.Dataset(
{'var1': (('x', 'y'), np.random.rand(3, 3))},
coords={'x': [1, 2, 3], 'y': [10, 20, 30]}
)
print(data)
5. Select Data by Coordinates
# Select single value
value = ds.sel(lat=10, lon=20)
# Select range
subset = ds.sel(lat=slice(5, 15), lon=slice(10, 30))
6. Access Variables
var = ds['variable_name']
print(var)
7. Groupby Operations
# Group by time and calculate the mean
monthly_mean = ds.groupby('time.month').mean()
8. Resample Data
# Resample to monthly means
monthly = ds.resample(time='1M').mean()
9. Interpolate Missing Data
# Linear interpolation
filled = ds.interpolate_na(dim='time')
10. Calculate Statistics
# Calculate mean and standard deviation
mean = ds.mean(dim='time')
std = ds.std(dim='time')
11. Apply Mathematical Operations
# Multiply all values by 2
scaled = ds * 2
12. Plot Data
# Plot a single variable
ds['var1'].plot()
13. Save Dataset to NetCDF
ds.to_netcdf('output.nc')
14. Combine Datasets
# Merge two datasets
combined = xr.merge([ds1, ds2])
15. Convert to Pandas
# Convert to DataFrame
df = ds.to_dataframe()
16. Expand Dimensions
# Add a new dimension
expanded = ds.expand_dims('new_dim')
17. Drop Variables
# Drop a variable
ds = ds.drop_vars('var_to_drop')
18. Mask Data
# Mask values greater than 0.5
masked = ds.where(ds < 0.5)
19. Compute Derived Variables
# Calculate a new variable
ds['new_var'] = ds['var1'] * 2
20. Identify NaNs
# Check for NaN values
nan_mask = ds.isnull()
Let me know if you need more help and leave comment if you like this cheatcode.