Updated On : Dec-02,2021 Tags xarray-dataset
xarray (Dataset) : Multi-Dimensional Labelled Arrays

xarray: Multi-Dimensional Labelled Arrays (Dataset)

Xarray is an open-source Python library that works almost like numpy but let us name dimensions of the array. Unlike numpy, where the majority of methods require us to specify axis argument to perform operations on a particular axis, xarray lets us specify string dimension names which can be more intuitive. This generally helps a lot when you are looking at your old codebase as operations based on axis won't give a better idea but operations based on some string dimension name will easily remind you of your coding decisions. Apart from dimension names, xarray also let us specify coordinates and attributes for arrays. The coordinates are just like pandas index but present for all dimensions of our array. The attributes are overall details about an array and are not associated with any dimension or coordinates.

Xarray provides two main data structures as a part of its API.

  1. DataArray - This is numpy-like N-Dimensional Labelled array.
  2. Dataset - This is a set of DataArray objects which can be worked together. All the DataArray in Dataset will have same dimensions and coordinates. When we perform any operation based on dimensions or coordinates then that operation will be performed on all DataArrays present in Dataset. This will become clear when we explain with examples.

As a part of this tutorial, we'll be primarily concentrating on Dataset data structure of xarray library. We have already covered DataArray data structure in detail in a separate tutorial. Please feel free to check it from the below link if you want to know about it in detail.

Below we have highlighted important sections of our tutorial to give an overview of the topics that we'll be covering.

Important Sections of Tutorial

  1. Dataset Creation
  2. Dataset Attributes
  3. Dataset Indexing/Slicing
    • Indexing By Accessing Individual Arrays
    • Indexing using isel() Function
    • Indexing using sel() Function
  4. Normal Operations on Dataset
  5. Simple Statistics

Below we have imported the necessary libraries that we'll be using as a part of our tutorial.

In [1]:
import xarray as xr

print("Xarray Version : {}".format(xr.__version__))
Xarray Version : 0.20.1
In [2]:
import numpy as np
import pandas as pd

1. Dataset Creation

In this section, we'll explain various ways of creating xarray Dataset using different methods available from the API. We'll be using numpy for data creation purposes. We'll be using datasets created in this section in all of our upcoming sections to explain indexing and other methods of xarray API.

Below we have first set seed for numpy so that all random numbers generated after this code will be the same on different computers for reproducibility purposes.

In [3]:
np.random.seed(123)

The simplest way to create xarray Dataset is by using Dataset constructor available from the library.

Dataset()


  • Dataset(data_vars={}, coords={}, attrs={}) - This constructor accepts data give as dictionary to data_vars parameter, coordinates of dataset given as dictionary to coords parameter and attributes given as dictionary to attrs parameter. It then creates an instance of Dataset which is a multi-dimensional labeled array consisting of many DataArray instances that can be worked in parallel. Below, we have described how to provide values for parameters to this constructor.
    • The data_vars parameter accepts dictionary where keys of this dictionary are the name of the individual DataArray and values are tuples of the form (coordinates,data[,attributes]).
      • The coordinates can be a single string if there is only one dimension of input DataArray else it can be a tuple of strings specifying the dimension of N-dimensional DataArray.
      • The data can be numpy array, python lists, etc.
      • The attribute is an optional dictionary that can hold information about an individual DataArray.
    • The coords parameter accepts dictionary, where keys of the dictionary are dimension names and values are indexing values of those dimensions that we'll use to index Dataset. This dictionary can also create coordinates by combining a few dimensions of the dataset. We'll explain this through our examples below to make it more clear.
    • The attrs parameter accepts a dictionary which specifies attributes of Dataset holding some information about it.

Below we have created our first xarray Dataset. We have first created two numpy arrays of random numbers and the same shape. We have then given these arrays to Dataset constructor through the dictionary to parameter data_vars. We have given names of the arrays as dictionary keys and dictionary values are a combination of coordinates and data. As we have one-dimensional arrays, we can provide coordinates names as a single string. We have then specified coordinates by giving a dictionary to coords parameter. We have given a simple range that goes from 0-4 as the value of a single dimension of data. These values 0-4 will be coordinates to index Dataset in x dimension.

In [4]:
arr1 = np.random.randn(5)
arr2 = np.random.randn(5)

dataset1 = xr.Dataset(data_vars={"Array1": ('x', arr1),
                                 "Array2": ('x', arr2)},
                      coords={"x": np.arange(5)})

dataset1
Out[4]:
<xarray.Dataset>
Dimensions:  (x: 5)
Coordinates:
  * x        (x) int64 0 1 2 3 4
Data variables:
    Array1   (x) float64 -1.086 0.9973 0.283 -1.506 -0.5786
    Array2   (x) float64 1.651 -2.427 -0.4289 1.266 -0.8667
xarray.Dataset
    • x: 5
    • x
      (x)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x)
      float64
      -1.086 0.9973 0.283 -1.506 -0.5786
      array([-1.0856306 ,  0.99734545,  0.2829785 , -1.50629471, -0.57860025])
    • Array2
      (x)
      float64
      1.651 -2.427 -0.4289 1.266 -0.8667
      array([ 1.65143654, -2.42667924, -0.42891263,  1.26593626, -0.8667404 ])

Below we have created another xarray Dataset using Dataset() constructor. This time we have provided two-dimensional arrays as data of our dataset. We have created both arrays using numpy. One of the arrays is an array of integers and another is an array of random floats in the range 0-1. This example shows that we can combine different kinds of data using Dataset.

As our arrays are two-dimensional, we'll have two dimensions in our data. We have declared dimensions as tuple (('x','y')) in dictionary provided to data_vars parameter of the constructor. The coords parameter is provided with a dictionary where we have simply used a range of integers to represent coordinates.

In [5]:
arr1 = np.random.randint(1,100,size=(3, 5))
arr2 = np.random.randn(3, 5)

dataset2 = xr.Dataset(data_vars={"Array1": (("x","y"), arr1),
                                 "Array2": (("x","y"), arr2)},
                      coords={"x": np.arange(3),
                              "y": np.arange(5)})

dataset2
Out[5]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (x, y) int64 84 79 37 97 81 69 50 56 68 3 85 40 67 85 48
    Array2   (x, y) float64 1.004 0.3862 0.7374 1.491 ... -0.2556 -2.799 -1.772
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x, y)
      int64
      84 79 37 97 81 ... 85 40 67 85 48
      array([[84, 79, 37, 97, 81],
             [69, 50, 56, 68,  3],
             [85, 40, 67, 85, 48]])
    • Array2
      (x, y)
      float64
      1.004 0.3862 ... -2.799 -1.772
      array([[ 1.0040539 ,  0.3861864 ,  0.73736858,  1.49073203, -0.93583387],
             [ 1.17582904, -1.25388067, -0.6377515 ,  0.9071052 , -1.4286807 ],
             [-0.14006872, -0.8617549 , -0.25561937, -2.79858911, -1.7715331 ]])

Below we have created another xaray Dataset which has almost the same code as our previous dataset with only a change in the coordinate values for both dimensions. We have provided a list of strings as coordinate values.

In [6]:
arr1 = np.random.randint(1,100,size=(3, 5))
arr2 = np.random.randn(3, 5)

dataset3 = xr.Dataset(data_vars={"Array1": (("x","y"), arr1),
                                 "Array2": (("x","y"), arr2)},
                      coords={"x": ["x1","x2","x3"],
                              "y": ["y1","y2","y3","y4","y5"]})

dataset3
Out[6]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) <U2 'x1' 'x2' 'x3'
  * y        (y) <U2 'y1' 'y2' 'y3' 'y4' 'y5'
Data variables:
    Array1   (x, y) int64 28 31 53 71 27 81 7 15 76 55 72 2 44 59 56
    Array2   (x, y) float64 -0.6999 2.392 0.4129 0.9787 ... 1.755 1.496 1.069
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      <U2
      'x1' 'x2' 'x3'
      array(['x1', 'x2', 'x3'], dtype='<U2')
    • y
      (y)
      <U2
      'y1' 'y2' 'y3' 'y4' 'y5'
      array(['y1', 'y2', 'y3', 'y4', 'y5'], dtype='<U2')
    • Array1
      (x, y)
      int64
      28 31 53 71 27 81 ... 72 2 44 59 56
      array([[28, 31, 53, 71, 27],
             [81,  7, 15, 76, 55],
             [72,  2, 44, 59, 56]])
    • Array2
      (x, y)
      float64
      -0.6999 2.392 ... 1.496 1.069
      array([[-0.69987723,  2.39236527,  0.41291216,  0.97873601,  2.23814334],
             [-1.29408532, -1.03878821,  1.74371223, -0.79806274,  0.02968323],
             [ 1.06931597,  0.89070639,  1.75488618,  1.49564414,  1.06939267]])

In the below cell, we have created another Dataset in which we have provided 3-dimensional arrays. We have specified three-dimension names as tuple (('x','y','z')). The coordinates values are simply a range of integers.

In [7]:
arr1 = np.random.randint(1,100,size=(3, 5,7))
arr2 = np.random.randn(3, 5, 7)

dataset4 = xr.Dataset(data_vars={"Array1": (("x","y","z"), arr1),
                                 "Array2": (("x","y","z"), arr2)},
                      coords={"x": np.arange(3),
                              "y": np.arange(5),
                              "z": np.arange(7)})

dataset4
Out[7]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5, z: 7)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
  * z        (z) int64 0 1 2 3 4 5 6
Data variables:
    Array1   (x, y, z) int64 7 10 88 15 84 71 13 55 ... 27 93 76 11 4 46 59 60
    Array2   (x, y, z) float64 1.013 0.2787 -1.371 ... 0.8961 0.3696 -0.7613
xarray.Dataset
    • x: 3
    • y: 5
    • z: 7
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • z
      (z)
      int64
      0 1 2 3 4 5 6
      array([0, 1, 2, 3, 4, 5, 6])
    • Array1
      (x, y, z)
      int64
      7 10 88 15 84 71 ... 11 4 46 59 60
      array([[[ 7, 10, 88, 15, 84, 71, 13],
              [55, 28, 39, 18, 62, 75, 66],
              [48, 17,  6, 87, 47, 16, 60],
              [41, 26, 46, 50,  1, 36, 30],
              [ 2, 84, 69, 31,  8, 94, 61]],
      
             [[66, 77, 68, 45, 52,  8, 89],
              [71, 14, 29, 64, 85, 37, 97],
              [41, 89, 64, 59, 78,  9, 79],
              [ 7, 66, 95, 71, 41, 75, 77],
              [77, 26,  8, 14, 45,  2, 42]],
      
             [[79, 57, 88, 64, 98,  4, 18],
              [89, 88, 70, 98, 51,  3, 19],
              [47, 46, 58, 36, 19, 92, 47],
              [55, 74, 82, 63, 68, 12, 27],
              [93, 76, 11,  4, 46, 59, 60]]])
    • Array2
      (x, y, z)
      float64
      1.013 0.2787 ... 0.3696 -0.7613
      array([[[ 1.01273905,  0.27874086, -1.37094847, -0.33247528,
                1.95941134, -2.02504576, -0.27578601],
              [-0.55210807,  0.12074736,  0.74821562,  1.60869097,
               -0.27023239,  0.81234133,  0.49974014],
              [ 0.4743473 , -0.56392393, -0.99732147, -1.10004311,
               -0.75643721,  0.32168658,  0.76094939],
              [ 0.32346885, -0.5489551 ,  1.80597011,  1.51886562,
               -0.35400011, -0.82343141,  0.13021495],
              [ 1.26729865,  0.33276498,  0.5565487 , -0.21208012,
                0.4562709 ,  1.54454445, -0.23966878]],
      
             [[ 0.14330773,  0.25381648,  0.28372536, -1.41188888,
               -1.87686866, -1.01965507,  0.1679423 ],
              [ 0.55385617, -0.53067456,  1.37725748, -0.14317597,
                0.020316  , -0.19396387,  0.13402679],
              [ 0.70447407,  0.66565344, -0.89842294,  1.52366378,
               -1.09502646,  0.07922701, -0.27439657],
              [-1.04899168, -0.07512059, -0.74081377,  0.07290724,
                0.40308596,  1.47192937,  0.30738422],
              [-0.61122534, -0.39161981,  0.13997811,  0.09346083,
                1.45958927,  1.39535293, -0.35893593]],
      
             [[-0.54864213, -2.5570546 , -0.54892041, -0.97805771,
               -0.35482446,  0.39158424,  0.17719233],
              [-0.02996801,  0.19958211, -0.12611777,  0.19701893,
               -3.23105501, -0.26929349, -0.11085072],
              [-0.34126172, -0.21794626,  0.70331012, -0.59810533,
                2.2007021 ,  0.68829693, -0.00630725],
              [-0.2066623 , -0.08652229, -0.91530707, -0.09520254,
                0.27868352,  0.57954162,  0.57968978],
              [-0.27487755, -1.41608225, -0.66910263,  1.61219304,
                0.89605831,  0.36961959, -0.76129424]]])

In the next cell, we have created a Dataset where we have created coordinate by combining two dimensions 'x' and 'y'.

Please make a NOTE of how we have provided value to 'index1' coordinate. The value of the dictionary is a tuple with two elements. The first element is again a tuple of two strings that specifies which dimensions it combines. The second value is an array that has the same shape as the combined shape of dimensions 'x' and 'y'. We have created an array of integers in the range 0-14 and reshaped them as (3,5) array to be used as a coordinate value. When we'll perform indexing on this dataset, value from 'index1' coordinates will be selected based on 'x' and 'y' dimension values used for indexing (E.g - x=0,y=0, index1=0, x=0:2, y=0:2, index1=0,1,3,4). This example explains how we can store some extra information inside of coordinates which can be useful to link more related data. This will become more clear when we explain our next example which is taken from real-life datasets.

In order to perform indexing on this Dataset, we'll still need to provide all three 'x,y, and z' dimensions. But we are storing extra details as 'index1' coordinate which can be a requirement in some situations. When we'll explain indexing/slicing datasets, it'll become more clear how coordinates with values different than normal integer indexing can be used to store more information.

In [8]:
arr1 = np.random.randint(1,100,size=(3, 5,7))
arr2 = np.random.randn(3, 5, 7)

dataset5 = xr.Dataset(data_vars={"Array1": (("x","y","z"), arr1),
                                 "Array2": (("x","y","z"), arr2)},
                      coords={"index1": (("x","y"), np.arange(15).reshape(3,5)),
                              "z": np.arange(7)
                            })

dataset5
Out[8]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5, z: 7)
Coordinates:
    index1   (x, y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  * z        (z) int64 0 1 2 3 4 5 6
Dimensions without coordinates: x, y
Data variables:
    Array1   (x, y, z) int64 8 78 62 89 96 28 66 17 ... 75 25 33 63 54 51 85 52
    Array2   (x, y, z) float64 0.003645 2.932 1.811 ... -0.3679 0.3493 -0.1587
xarray.Dataset
    • x: 3
    • y: 5
    • z: 7
    • index1
      (x, y)
      int64
      0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
      array([[ 0,  1,  2,  3,  4],
             [ 5,  6,  7,  8,  9],
             [10, 11, 12, 13, 14]])
    • z
      (z)
      int64
      0 1 2 3 4 5 6
      array([0, 1, 2, 3, 4, 5, 6])
    • Array1
      (x, y, z)
      int64
      8 78 62 89 96 28 ... 63 54 51 85 52
      array([[[ 8, 78, 62, 89, 96, 28, 66],
              [17, 91,  2,  8, 38, 20, 64],
              [21, 90, 31, 84, 70, 15, 57],
              [ 1, 90, 29, 66, 22, 61, 29],
              [ 9, 41, 21, 55, 96, 96, 40]],
      
             [[26, 48, 77,  3, 96, 33, 97],
              [42, 77, 43, 70,  4, 64, 70],
              [51, 67, 91, 84, 18, 20, 30],
              [74, 52, 23, 47,  3, 50,  9],
              [19, 36, 40, 27, 92, 72, 72]],
      
             [[82, 21, 13, 27, 62, 40, 53],
              [80, 40,  9, 27, 81,  3, 56],
              [53,  4, 40, 52, 56, 65, 78],
              [72, 71, 30, 25, 39, 80, 75],
              [25, 33, 63, 54, 51, 85, 52]]])
    • Array2
      (x, y, z)
      float64
      0.003645 2.932 ... 0.3493 -0.1587
      array([[[ 0.00364515,  2.93214489,  1.81095662,  1.17887773,
                0.27788677,  0.68631345, -0.24897077],
              [-1.63503755, -1.4766429 , -0.87617199, -0.21624005,
                2.12717406, -0.48948679,  0.62745511],
              [-0.32354138, -1.80998831, -0.1782155 , -1.73091981,
               -1.67651805,  0.75305941,  0.85862496],
              [ 0.24178725, -3.41179617,  0.5139972 ,  0.78466251,
                1.68875543,  0.07315088,  0.50879807],
              [-2.00866257, -0.73358057,  0.13734376,  1.16575469,
                0.94927628,  0.2824198 ,  2.9864872 ]],
      
             [[ 0.15292715, -0.79166947,  1.20638357,  1.79443859,
               -0.4269306 ,  0.22876715,  0.01500988],
              [-1.00453921, -1.30618849, -0.22633063,  1.03712951,
               -0.22339343, -0.5606785 ,  0.39423   ],
              [ 0.20793461, -1.29625818, -1.92114749,  1.7474859 ,
                0.29875713, -2.23232578, -0.40950062],
              [-0.07688118, -1.06633545,  1.9420471 , -0.32185626,
               -0.92997999,  0.74065281,  0.83731422],
              [ 0.17709448, -0.37029048, -0.40899663, -0.47497236,
               -2.74551865, -2.01596017,  0.04230286]],
      
             [[-0.75786233, -0.29423165,  1.24229782,  1.51684324,
                0.48815011, -0.76526493, -2.33976321],
              [ 0.59891353,  0.57124371,  0.49303368, -0.6082252 ,
                0.79528583, -0.05729214, -1.22808647],
              [-1.06533714,  0.80455327, -0.02987441,  0.43236661,
               -0.85176539,  0.32477452,  0.27070759],
              [ 0.26346911, -0.61674068,  1.58592864,  1.1723271 ,
               -0.15007176, -0.58147393, -0.6417031 ],
              [ 0.41099584, -0.38411898,  0.05403731, -0.02076909,
               -0.36790608,  0.34926977, -0.15870316]]])

Our next example explains the kind of dataset that we can face in real-life situations. It shows how we can combine a different kind of data with Dataset object.

Our dataset consists of 6 different arrays of shape (3,5,7). They all represent measurements of different attributes which are used in weather forecasting.

The dataset has 3 dimensions which are named 'x,y and time'. All dimension names are specified in the dictionary given to data_vars parameter.

The dictionary is given to coords parameter creates two new coordinates named lon and lat which combines dimensions 'x and y'. The value of 'lon' coordinate is a tuple of two values where the first value is a tuple of two strings representing dimensions and the second value is an array of shape (3,5) representing coordinate values. The value of 'lat' coordinate follows the same structure. The 'time' dimension is used as it is to represent coordinates in that dimension. We have specified a list of seven dates as the value of time coordinate using pandas.date_range() function.

When we'll index our dataset by specifying values for 'x,y, and z' dimensions, we'll get unique measurements of temperature, humidity, pressure, wind speed, precipitation, and PM25 measured at a particular time and particular location (longitude, latitude). The location is represented using longitude and latitude which are specified as coordinates and not as part of the data dictionary provided to data_vars parameter.

Apart from data and coordinates, we have also specified attributes of the dataset first time. We have given a dictionary of strings to attrs parameter where we have specified more information explaining what the dataset holds and how to interpret coordinates and dimensions.

This example is inspired by the example present on xarray document hence below image taken from there can be helpful to understand how to look at Dataset to better understand it.

xarray : Multi-Dimensional Labelled Arrays (Dataset)

In [9]:
temperature = np.random.randint(1,100, size=(3,5,7))
humidity = np.random.randn(3, 5, 7)
pressure = np.random.randn(3, 5, 7)
windspeed = np.random.randn(3, 5, 7)
precitipation = np.random.randn(3, 5, 7)
pm25 = np.random.randn(3, 5, 7)

dataset6 = xr.Dataset(data_vars={"Temperature": (("x","y","time"), temperature),
                                 "Humidity": (("x","y","time"), humidity),
                                 "Pressure": (("x","y","time"), pressure),
                                 "WindSpeed": (("x","y","time"), windspeed),
                                 "Precipitation": (("x","y","time"), precitipation),
                                 "PM25": (("x","y","time"), pm25),
                                },
                      coords={"lon": (("x","y"), np.linspace(1,15,15).reshape(3,5)),
                              "lat": (("x","y"), np.linspace(15,30,15).reshape(3,5)),
                              "time": pd.date_range(start="2021-01-01", periods=7)
                                },
                      attrs={"Summary": "Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.",
                             "lon": "Longitude",
                             "lat": "Latitude",
                             "time": "Date of Record"
                            }
                     )

dataset6
Out[9]:
<xarray.Dataset>
Dimensions:        (x: 3, y: 5, time: 7)
Coordinates:
    lon            (x, y) float64 1.0 2.0 3.0 4.0 5.0 ... 12.0 13.0 14.0 15.0
    lat            (x, y) float64 15.0 16.07 17.14 18.21 ... 27.86 28.93 30.0
  * time           (time) datetime64[ns] 2021-01-01 2021-01-02 ... 2021-01-07
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 82 3 36 70 38 65 83 ... 15 12 71 24 70 41
    Humidity       (x, y, time) float64 1.093 -0.8485 0.1826 ... -1.53 1.676
    Pressure       (x, y, time) float64 1.176 -1.544 -0.6974 ... -0.9302 -0.5022
    WindSpeed      (x, y, time) float64 0.07895 -0.1061 ... 0.2155 -1.026
    Precipitation  (x, y, time) float64 -0.5524 0.5605 0.3806 ... -0.7978 -1.678
    PM25           (x, y, time) float64 1.149 -1.045 ... 0.01034 -0.07389
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 3
    • y: 5
    • time: 7
    • lon
      (x, y)
      float64
      1.0 2.0 3.0 4.0 ... 13.0 14.0 15.0
      array([[ 1.,  2.,  3.,  4.,  5.],
             [ 6.,  7.,  8.,  9., 10.],
             [11., 12., 13., 14., 15.]])
    • lat
      (x, y)
      float64
      15.0 16.07 17.14 ... 28.93 30.0
      array([[15.        , 16.07142857, 17.14285714, 18.21428571, 19.28571429],
             [20.35714286, 21.42857143, 22.5       , 23.57142857, 24.64285714],
             [25.71428571, 26.78571429, 27.85714286, 28.92857143, 30.        ]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 ... 2021-01-07
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000',
             '2021-01-03T00:00:00.000000000', '2021-01-04T00:00:00.000000000',
             '2021-01-05T00:00:00.000000000', '2021-01-06T00:00:00.000000000',
             '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      82 3 36 70 38 65 ... 12 71 24 70 41
      array([[[82,  3, 36, 70, 38, 65, 83],
              [38, 28, 47, 73, 47, 10, 88],
              [90, 72,  5, 13, 86, 34, 25],
              [23, 85,  3,  8, 46, 62, 39],
              [51, 22, 45, 75, 23, 49, 80]],
      
             [[51, 28, 99, 29, 59, 70, 78],
              [93, 28, 74, 47,  4, 88,  2],
              [64, 29, 89, 95, 47, 15, 38],
              [97, 49, 72, 40, 16, 34,  6],
              [87, 53, 21, 99, 29, 99,  2]],
      
             [[97, 26, 44, 96, 18, 98, 30],
              [23, 48, 55, 84, 23, 40, 64],
              [72, 14, 87, 78,  2,  4, 88],
              [ 9, 16, 96, 32, 27, 15, 68],
              [25, 15, 12, 71, 24, 70, 41]]])
    • Humidity
      (x, y, time)
      float64
      1.093 -0.8485 ... -1.53 1.676
      array([[[ 1.09285914, -0.84854387,  0.1826349 , -1.11396086,
                0.78225378, -0.78785743, -0.87511885],
              [ 2.09665566,  0.15985731,  0.25153841,  0.74573533,
               -0.28411693, -0.23392604,  0.7636526 ],
              [ 0.96068395, -1.1125588 ,  0.46637362,  0.29439909,
                0.82885291, -0.04324119,  0.6410817 ],
              [-1.00679045,  0.28511252, -1.29079086,  0.82044935,
               -0.44715373, -1.61174393, -0.85558611],
              [-0.27158379,  0.76722785,  1.16378164, -0.52366829,
               -0.30989558,  0.83373781, -0.78712166]],
      
             [[-0.37605972, -0.54091566,  0.82583929, -0.71282054,
                0.88832528,  1.11421149,  0.02472263],
              [ 0.92056561, -0.32548426,  1.42181688,  0.47777962,
               -0.85564121, -0.38539221, -1.25830263],
              [ 0.57649183, -0.20337122,  1.36695728,  0.87814834,
                0.90984742,  0.55089382,  0.21001439],
              [ 0.5065872 ,  0.74714323,  1.69817763,  1.08498814,
                0.95241091,  1.30515827, -2.78357597],
              [-0.77912696,  0.11587679, -0.96905842, -1.06861872,
               -0.0615628 ,  1.24060027, -0.70328684]],
      
             [[ 0.76795915,  1.94095997, -1.19133151,  0.35401862,
                0.50684127, -0.66506239, -0.34307771],
              [ 1.40531069, -0.34715154, -0.50778276,  1.90611924,
                1.97774463, -0.33045863, -0.08441006],
              [-0.94689976,  0.46821417,  2.79138734,  0.97905794,
               -1.11122656,  0.95154709,  1.58695372],
              [-1.9940226 , -1.3042813 , -1.39580034, -0.44978336,
               -0.54981007, -0.78062656, -0.28168974],
              [-0.15129096,  0.94958385, -0.23475781,  1.62449484,
               -0.44738014, -1.52953396,  1.67588006]]])
    • Pressure
      (x, y, time)
      float64
      1.176 -1.544 ... -0.9302 -0.5022
      array([[[ 1.17620022, -1.54431399, -0.69740179,  0.89795695,
                1.34225084,  1.29864939, -0.01770615],
              [ 0.36674592,  0.670313  ,  0.79318369, -1.48557322,
                0.24876417, -0.02809683,  0.70325669],
              [ 0.43966429, -1.02530554, -0.10341202,  0.69840893,
               -0.68841273,  0.32941774,  1.38918332],
              [ 0.77113172, -1.99468532,  0.51690718, -0.82647174,
               -0.59509579, -0.95100739,  1.21306141],
              [-0.28554035, -0.39255704, -1.1556553 ,  0.15677776,
                1.4294567 , -1.39324158, -2.35228298]],
      
             [[ 1.63744119, -0.3252367 , -0.7357182 ,  0.22199913,
                0.18874436,  1.68638837,  1.38039618],
              [ 2.00222948, -0.28522905,  0.45157815,  1.17247914,
               -1.22189294, -0.39771502,  0.32021353],
              [ 1.46652224, -1.08574032,  1.46696652,  1.15184296,
                0.42686459,  0.75804519,  1.81746665],
              [-2.28452632, -1.5172906 ,  0.36539539,  1.22952385,
                0.18460093, -0.68319853,  0.4164532 ],
              [-1.38847933, -0.7328258 ,  1.07662905, -0.58588456,
               -1.70815234, -0.296188  ,  1.61925102]],
      
             [[ 0.2511776 , -0.22207721,  0.52296919, -2.07489649,
                1.17133082, -1.97176127, -0.6889194 ],
              [ 1.07380698,  1.44372828, -0.41462581,  0.45539046,
                0.18607358, -2.10662521, -0.45100886],
              [-0.69981506,  1.13017584, -0.55982602,  0.56826116,
               -1.36354003, -0.76608813,  0.15614456],
              [-1.26867245,  1.50967789,  1.46400708,  0.09948321,
                0.03405099, -0.55550723, -0.05456826],
              [-0.4456235 , -0.33282213,  0.31132147,  0.069857  ,
                0.47862362, -0.93024318, -0.50223001]]])
    • WindSpeed
      (x, y, time)
      float64
      0.07895 -0.1061 ... 0.2155 -1.026
      array([[[ 0.07894539, -0.10608959, -0.15073608, -0.34567601,
                1.54519634,  0.38600075,  0.64598828],
              [-1.05765309, -1.71632891,  1.12384288,  0.60882024,
               -0.07093692,  0.72749619,  2.3643872 ],
              [ 0.46617626,  0.82395283, -2.52790159,  1.54609636,
               -0.44263358,  0.55701387, -0.75322983],
              [-1.2630226 , -0.06455496,  0.04505647, -0.0518783 ,
               -0.13284939, -0.21291942,  0.79013423],
              [-0.38895218,  1.51207925, -1.90532691,  1.07004393,
                0.297842  , -0.41960468, -2.02513535]],
      
             [[-1.20795325, -1.27012772,  1.3664836 ,  1.63163257,
               -0.36656294, -1.46336765, -0.82472209],
              [-0.43844783,  0.07389177,  0.58102792,  1.77656698,
                1.5170325 ,  1.25235069, -1.64758168],
              [-1.49854381,  0.83048688, -0.96246745,  0.23664752,
               -0.11588952,  1.63056501, -0.34286339],
              [ 0.49633755, -0.35311826,  0.74824454, -2.27923857,
                2.11301479,  1.10075378, -0.63347715],
              [ 0.88666826, -2.51101704, -0.31195891,  0.83481415,
               -1.54296428, -0.50920692, -1.02948406]],
      
             [[ 0.55171479,  0.51425579,  0.78569017,  0.30599779,
                0.18657313,  0.82820387, -0.26162739],
              [ 0.58689156, -0.42296983, -1.1292345 ,  1.94049605,
                1.36913959,  1.05035477, -0.26093031],
              [-0.4132902 , -0.88769071,  0.04975729,  0.2306687 ,
                0.85900227,  0.2660529 ,  0.20363354],
              [ 0.94501406,  0.22654847,  0.50482622,  0.41408703,
                0.34409454,  0.99606496,  1.6020004 ],
              [-1.08944353, -0.36530911, -1.33081251, -0.02537123,
               -1.99927196,  0.21548673, -1.02648887]]])
    • Precipitation
      (x, y, time)
      float64
      -0.5524 0.5605 ... -0.7978 -1.678
      array([[[-0.55236716,  0.56045232,  0.38064352, -1.93996829,
               -0.86920922, -1.18397041, -0.40251371],
              [-1.19625733, -0.55042156,  1.10772148, -0.93566846,
               -0.27924748,  0.47137331,  0.62897312],
              [ 0.20955818,  0.47893734,  1.11841985, -0.45516288,
                1.95094827, -0.61284131,  1.66222385],
              [ 0.70743435,  0.03414732, -1.98738552, -0.64425647,
                0.59936148, -0.69318562,  1.23651906],
              [ 0.13764513,  0.84321747,  0.34240088, -0.1092026 ,
               -0.80374936,  1.12265484, -0.17310687]],
      
             [[ 0.73722263, -0.28084061, -1.59074815,  0.16298992,
                0.78558901, -0.57622863, -0.14254492],
              [ 2.24012964,  2.15538612,  0.02794398,  0.55651171,
                0.58283111,  2.57319673,  0.43154201],
              [-0.96602978,  1.95313342,  1.56685205, -1.07134412,
                1.24054182, -0.99218704, -1.40214714],
              [-0.82823633, -1.87149092,  0.54425585,  1.03288874,
                0.87459299, -0.70930382, -0.14372735],
              [ 0.15809608,  1.20022505,  1.51685738,  1.18160504,
               -0.21131612,  0.02746981, -0.65240715]],
      
             [[-0.60228522,  1.76673585, -0.53051331,  0.34395563,
                0.28490643, -0.76413676, -2.59369344],
              [-0.7071827 , -0.16828121, -0.80118713, -1.43534924,
                0.46818736, -1.25485989,  0.14563744],
              [ 0.18051121,  1.64375119,  0.49077178, -2.69894487,
               -2.54625675,  1.15855515,  0.55661785],
              [ 0.28288257,  0.30063656,  1.32514957, -0.49511467,
               -2.22847894, -1.01659864,  0.62573462],
              [-0.10475908, -0.39669181,  1.73450886, -1.10069765,
                0.30144238, -0.79784192, -1.67833048]]])
    • PM25
      (x, y, time)
      float64
      1.149 -1.045 ... 0.01034 -0.07389
      array([[[ 1.14942133e+00, -1.04513297e+00, -8.03607617e-02,
                1.99354580e+00,  1.09731930e+00,  1.38722040e-01,
               -8.35446640e-01],
              [ 8.26586757e-01, -1.65826306e-02, -1.67572929e+00,
               -1.49658499e+00, -6.55535560e-01, -1.13255119e+00,
               -6.62022616e-01],
              [-1.47701503e+00, -6.92415573e-01, -3.12028428e+00,
                1.00236630e+00, -1.02402615e+00, -3.17190482e-01,
               -2.99456106e-01],
              [ 1.42893050e+00, -1.21543089e+00,  1.02972802e+00,
                1.14535031e+00, -2.05797367e-01,  3.17312174e-01,
                2.72734691e+00],
              [ 2.40283034e-01,  2.70292152e-01, -1.09793778e-01,
               -5.64620231e-01,  6.91831717e-02,  1.05257571e+00,
               -8.52768464e-01]],
      
             [[ 1.70862604e+00,  8.32009334e-01, -5.17421515e-01,
                6.02894373e-01, -7.06527232e-01,  7.60821520e-01,
               -1.50305205e-01],
              [-8.37166196e-01,  2.12666983e+00, -3.25378307e-01,
      ...
               -5.47985161e-01],
              [-9.49655124e-01, -8.63043266e-01,  4.10380876e-01,
               -1.31391951e+00,  8.53101463e-01, -4.03442068e-01,
                1.53879341e+00]],
      
             [[ 1.18998007e-01,  1.35084642e+00,  1.94379822e+00,
               -3.40083777e-01,  1.31582655e-03, -1.02573574e+00,
                1.01054907e+00],
              [ 5.70838063e-01, -3.62113981e-01, -2.43829109e+00,
               -3.32860193e-01,  3.08150115e-01, -6.66209068e-01,
                1.43865551e+00],
              [-1.82446794e+00,  5.68793112e-01,  6.50500612e-01,
               -1.02004409e+00,  2.34714662e+00, -2.09622832e+00,
               -6.00190258e-01],
              [-7.54482730e-01, -6.82451422e-01,  8.91158242e-01,
                1.95245997e-01,  1.75215239e-01, -3.21497707e-01,
               -6.21718753e-01],
              [-1.46312025e+00,  1.04896631e-01,  5.96195221e-02,
               -1.60372280e+00, -7.68560851e-01,  1.03403426e-02,
               -7.38877135e-02]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record

ones_like()

The ones_like() method works like its counterpart in numpy. It takes as input Dataset object and returns another Dataset object which has same dimensions as input Dataset but all values in the Dataset are replaced with 1s.

In [10]:
xr.ones_like(dataset6)
Out[10]:
<xarray.Dataset>
Dimensions:        (x: 3, y: 5, time: 7)
Coordinates:
    lat            (x, y) float64 15.0 16.07 17.14 18.21 ... 27.86 28.93 30.0
    lon            (x, y) float64 1.0 2.0 3.0 4.0 5.0 ... 12.0 13.0 14.0 15.0
  * time           (time) datetime64[ns] 2021-01-01 2021-01-02 ... 2021-01-07
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 1 1 1 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 1 1 1
    Humidity       (x, y, time) float64 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
    Pressure       (x, y, time) float64 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
    WindSpeed      (x, y, time) float64 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
    Precipitation  (x, y, time) float64 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
    PM25           (x, y, time) float64 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 3
    • y: 5
    • time: 7
    • lat
      (x, y)
      float64
      15.0 16.07 17.14 ... 28.93 30.0
      array([[15.        , 16.07142857, 17.14285714, 18.21428571, 19.28571429],
             [20.35714286, 21.42857143, 22.5       , 23.57142857, 24.64285714],
             [25.71428571, 26.78571429, 27.85714286, 28.92857143, 30.        ]])
    • lon
      (x, y)
      float64
      1.0 2.0 3.0 4.0 ... 13.0 14.0 15.0
      array([[ 1.,  2.,  3.,  4.,  5.],
             [ 6.,  7.,  8.,  9., 10.],
             [11., 12., 13., 14., 15.]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 ... 2021-01-07
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000',
             '2021-01-03T00:00:00.000000000', '2021-01-04T00:00:00.000000000',
             '2021-01-05T00:00:00.000000000', '2021-01-06T00:00:00.000000000',
             '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      1 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 1 1
      array([[[1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1]],
      
             [[1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1]],
      
             [[1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1]]])
    • Humidity
      (x, y, time)
      float64
      1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
      array([[[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]],
      
             [[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]],
      
             [[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]]])
    • Pressure
      (x, y, time)
      float64
      1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
      array([[[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]],
      
             [[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]],
      
             [[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]]])
    • WindSpeed
      (x, y, time)
      float64
      1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
      array([[[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]],
      
             [[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]],
      
             [[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]]])
    • Precipitation
      (x, y, time)
      float64
      1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
      array([[[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]],
      
             [[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]],
      
             [[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]]])
    • PM25
      (x, y, time)
      float64
      1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
      array([[[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]],
      
             [[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]],
      
             [[1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.],
              [1., 1., 1., 1., 1., 1., 1.]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record

zeros_like()

The zeros_like() method works exactly like ones_like() but all the values of Dataset are 0s.

In [11]:
xr.zeros_like(dataset6)
Out[11]:
<xarray.Dataset>
Dimensions:        (x: 3, y: 5, time: 7)
Coordinates:
    lat            (x, y) float64 15.0 16.07 17.14 18.21 ... 27.86 28.93 30.0
    lon            (x, y) float64 1.0 2.0 3.0 4.0 5.0 ... 12.0 13.0 14.0 15.0
  * time           (time) datetime64[ns] 2021-01-01 2021-01-02 ... 2021-01-07
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0
    Humidity       (x, y, time) float64 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
    Pressure       (x, y, time) float64 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
    WindSpeed      (x, y, time) float64 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
    Precipitation  (x, y, time) float64 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
    PM25           (x, y, time) float64 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 3
    • y: 5
    • time: 7
    • lat
      (x, y)
      float64
      15.0 16.07 17.14 ... 28.93 30.0
      array([[15.        , 16.07142857, 17.14285714, 18.21428571, 19.28571429],
             [20.35714286, 21.42857143, 22.5       , 23.57142857, 24.64285714],
             [25.71428571, 26.78571429, 27.85714286, 28.92857143, 30.        ]])
    • lon
      (x, y)
      float64
      1.0 2.0 3.0 4.0 ... 13.0 14.0 15.0
      array([[ 1.,  2.,  3.,  4.,  5.],
             [ 6.,  7.,  8.,  9., 10.],
             [11., 12., 13., 14., 15.]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 ... 2021-01-07
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000',
             '2021-01-03T00:00:00.000000000', '2021-01-04T00:00:00.000000000',
             '2021-01-05T00:00:00.000000000', '2021-01-06T00:00:00.000000000',
             '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0
      array([[[0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0]],
      
             [[0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0]],
      
             [[0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0]]])
    • Humidity
      (x, y, time)
      float64
      0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
      array([[[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]],
      
             [[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]],
      
             [[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]]])
    • Pressure
      (x, y, time)
      float64
      0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
      array([[[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]],
      
             [[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]],
      
             [[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]]])
    • WindSpeed
      (x, y, time)
      float64
      0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
      array([[[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]],
      
             [[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]],
      
             [[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]]])
    • Precipitation
      (x, y, time)
      float64
      0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
      array([[[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]],
      
             [[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]],
      
             [[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]]])
    • PM25
      (x, y, time)
      float64
      0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
      array([[[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]],
      
             [[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]],
      
             [[0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.],
              [0., 0., 0., 0., 0., 0., 0.]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record

full_like()

The full_like() method takes as input Dataset object and another value. It then returns another Dataset object which has the same dimensions as input Dataset but all values are replaced with a value given as second input to the method.

In [12]:
xr.full_like(dataset6, 101)
Out[12]:
<xarray.Dataset>
Dimensions:        (x: 3, y: 5, time: 7)
Coordinates:
    lat            (x, y) float64 15.0 16.07 17.14 18.21 ... 27.86 28.93 30.0
    lon            (x, y) float64 1.0 2.0 3.0 4.0 5.0 ... 12.0 13.0 14.0 15.0
  * time           (time) datetime64[ns] 2021-01-01 2021-01-02 ... 2021-01-07
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 101 101 101 101 101 ... 101 101 101 101
    Humidity       (x, y, time) float64 101.0 101.0 101.0 ... 101.0 101.0 101.0
    Pressure       (x, y, time) float64 101.0 101.0 101.0 ... 101.0 101.0 101.0
    WindSpeed      (x, y, time) float64 101.0 101.0 101.0 ... 101.0 101.0 101.0
    Precipitation  (x, y, time) float64 101.0 101.0 101.0 ... 101.0 101.0 101.0
    PM25           (x, y, time) float64 101.0 101.0 101.0 ... 101.0 101.0 101.0
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 3
    • y: 5
    • time: 7
    • lat
      (x, y)
      float64
      15.0 16.07 17.14 ... 28.93 30.0
      array([[15.        , 16.07142857, 17.14285714, 18.21428571, 19.28571429],
             [20.35714286, 21.42857143, 22.5       , 23.57142857, 24.64285714],
             [25.71428571, 26.78571429, 27.85714286, 28.92857143, 30.        ]])
    • lon
      (x, y)
      float64
      1.0 2.0 3.0 4.0 ... 13.0 14.0 15.0
      array([[ 1.,  2.,  3.,  4.,  5.],
             [ 6.,  7.,  8.,  9., 10.],
             [11., 12., 13., 14., 15.]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 ... 2021-01-07
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000',
             '2021-01-03T00:00:00.000000000', '2021-01-04T00:00:00.000000000',
             '2021-01-05T00:00:00.000000000', '2021-01-06T00:00:00.000000000',
             '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      101 101 101 101 ... 101 101 101 101
      array([[[101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101]],
      
             [[101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101]],
      
             [[101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101],
              [101, 101, 101, 101, 101, 101, 101]]])
    • Humidity
      (x, y, time)
      float64
      101.0 101.0 101.0 ... 101.0 101.0
      array([[[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]],
      
             [[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]],
      
             [[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]]])
    • Pressure
      (x, y, time)
      float64
      101.0 101.0 101.0 ... 101.0 101.0
      array([[[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]],
      
             [[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]],
      
             [[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]]])
    • WindSpeed
      (x, y, time)
      float64
      101.0 101.0 101.0 ... 101.0 101.0
      array([[[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]],
      
             [[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]],
      
             [[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]]])
    • Precipitation
      (x, y, time)
      float64
      101.0 101.0 101.0 ... 101.0 101.0
      array([[[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]],
      
             [[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]],
      
             [[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]]])
    • PM25
      (x, y, time)
      float64
      101.0 101.0 101.0 ... 101.0 101.0
      array([[[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]],
      
             [[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]],
      
             [[101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.],
              [101., 101., 101., 101., 101., 101., 101.]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record

2. Dataset Attributes

In this section, we'll explain a few useful attributes of Dataset objects and the information stored in them.

The attrs attribute returns dictionary of Dataset attributes.

In [13]:
dataset6.attrs
Out[13]:
{'Summary': 'Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.',
 'lon': 'Longitude',
 'lat': 'Latitude',
 'time': 'Date of Record'}
In [14]:
dataset6.attrs["Summary"]
Out[14]:
'Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.'

The coords attribute returns coordinates of the dataset. We can extract individual coordinates values by treating the output of coords attribute as a dictionary. Each individual coordinate is represented using xarray DataArray object.

In [15]:
dataset6.coords
Out[15]:
Coordinates:
    lon      (x, y) float64 1.0 2.0 3.0 4.0 5.0 6.0 ... 11.0 12.0 13.0 14.0 15.0
    lat      (x, y) float64 15.0 16.07 17.14 18.21 ... 26.79 27.86 28.93 30.0
  * time     (time) datetime64[ns] 2021-01-01 2021-01-02 ... 2021-01-07
In [16]:
dataset6.coords["lon"]
Out[16]:
<xarray.DataArray 'lon' (x: 3, y: 5)>
array([[ 1.,  2.,  3.,  4.,  5.],
       [ 6.,  7.,  8.,  9., 10.],
       [11., 12., 13., 14., 15.]])
Coordinates:
    lon      (x, y) float64 1.0 2.0 3.0 4.0 5.0 6.0 ... 11.0 12.0 13.0 14.0 15.0
    lat      (x, y) float64 15.0 16.07 17.14 18.21 ... 26.79 27.86 28.93 30.0
Dimensions without coordinates: x, y
xarray.DataArray
'lon'
  • x: 3
  • y: 5
  • 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0
    array([[ 1.,  2.,  3.,  4.,  5.],
           [ 6.,  7.,  8.,  9., 10.],
           [11., 12., 13., 14., 15.]])
    • lon
      (x, y)
      float64
      1.0 2.0 3.0 4.0 ... 13.0 14.0 15.0
      array([[ 1.,  2.,  3.,  4.,  5.],
             [ 6.,  7.,  8.,  9., 10.],
             [11., 12., 13., 14., 15.]])
    • lat
      (x, y)
      float64
      15.0 16.07 17.14 ... 28.93 30.0
      array([[15.        , 16.07142857, 17.14285714, 18.21428571, 19.28571429],
             [20.35714286, 21.42857143, 22.5       , 23.57142857, 24.64285714],
             [25.71428571, 26.78571429, 27.85714286, 28.92857143, 30.        ]])

The data_vars attribute returns data that we provided when creating a Dataset.

In [17]:
dataset6.data_vars
Out[17]:
Data variables:
    Temperature    (x, y, time) int64 82 3 36 70 38 65 83 ... 15 12 71 24 70 41
    Humidity       (x, y, time) float64 1.093 -0.8485 0.1826 ... -1.53 1.676
    Pressure       (x, y, time) float64 1.176 -1.544 -0.6974 ... -0.9302 -0.5022
    WindSpeed      (x, y, time) float64 0.07895 -0.1061 ... 0.2155 -1.026
    Precipitation  (x, y, time) float64 -0.5524 0.5605 0.3806 ... -0.7978 -1.678
    PM25           (x, y, time) float64 1.149 -1.045 ... 0.01034 -0.07389

We can access individual data from a list of data by treating the output of data_vars as a dictionary. The result will be xarray DataArray object.

In [18]:
pm25 = dataset6.data_vars["PM25"]

type(pm25)
Out[18]:
xarray.core.dataarray.DataArray

We can access dimensions of Dataset using dims attribute.

In [19]:
dataset6.dims
Out[19]:
Frozen({'x': 3, 'y': 5, 'time': 7})

The indexes attribute returns indices of different dimensions of Dataset.

In [20]:
dataset3.indexes
Out[20]:
x: Index(['x1', 'x2', 'x3'], dtype='object', name='x')
y: Index(['y1', 'y2', 'y3', 'y4', 'y5'], dtype='object', name='y')
In [21]:
 dataset1.indexes
Out[21]:
x: Int64Index([0, 1, 2, 3, 4], dtype='int64', name='x')
In [22]:
dataset6.indexes
Out[22]:
time: DatetimeIndex(['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04',
                     '2021-01-05', '2021-01-06', '2021-01-07'],
                    dtype='datetime64[ns]', name='time', freq='D')

The nbytes attribute returns a total number of memory bytes used by Dataset object.

In [23]:
dataset6.nbytes
Out[23]:
5336

3. Dataset Indexing/Slicing

In this section, we'll explain how we can index Dataset objects. We'll first explain how we can access and index individual DataArray from Dataset and then explain indexing of Dataset as a whole using sel() and isel() methods.

Please make a NOTE that all methods in this section return a new Dataset object based on indexing operation. It does not modify any Dataset object in place.

Indexing By Accessing Individual Arrays

In this section, we'll explain how we can access individual DataArray and perform indexing on it. If you are interested in learning about indexing on DataArray in detail then please feel free to check our tutorial on it.

Below we have retrieved DataArray which is stored in our Dataset object by Array1 name. We can retrieve it by treating our Dataset object as dictionary-like.

In [24]:
dataset3["Array1"]
Out[24]:
<xarray.DataArray 'Array1' (x: 3, y: 5)>
array([[28, 31, 53, 71, 27],
       [81,  7, 15, 76, 55],
       [72,  2, 44, 59, 56]])
Coordinates:
  * x        (x) <U2 'x1' 'x2' 'x3'
  * y        (y) <U2 'y1' 'y2' 'y3' 'y4' 'y5'
xarray.DataArray
'Array1'
  • x: 3
  • y: 5
  • 28 31 53 71 27 81 7 15 76 55 72 2 44 59 56
    array([[28, 31, 53, 71, 27],
           [81,  7, 15, 76, 55],
           [72,  2, 44, 59, 56]])
    • x
      (x)
      <U2
      'x1' 'x2' 'x3'
      array(['x1', 'x2', 'x3'], dtype='<U2')
    • y
      (y)
      <U2
      'y1' 'y2' 'y3' 'y4' 'y5'
      array(['y1', 'y2', 'y3', 'y4', 'y5'], dtype='<U2')

We can also retrieve individual DataArray by calling its name as an attribute of Dataset object. The below statement will return the same result as our previous cell.

In [25]:
dataset3.Array1
Out[25]:
<xarray.DataArray 'Array1' (x: 3, y: 5)>
array([[28, 31, 53, 71, 27],
       [81,  7, 15, 76, 55],
       [72,  2, 44, 59, 56]])
Coordinates:
  * x        (x) <U2 'x1' 'x2' 'x3'
  * y        (y) <U2 'y1' 'y2' 'y3' 'y4' 'y5'
xarray.DataArray
'Array1'
  • x: 3
  • y: 5
  • 28 31 53 71 27 81 7 15 76 55 72 2 44 59 56
    array([[28, 31, 53, 71, 27],
           [81,  7, 15, 76, 55],
           [72,  2, 44, 59, 56]])
    • x
      (x)
      <U2
      'x1' 'x2' 'x3'
      array(['x1', 'x2', 'x3'], dtype='<U2')
    • y
      (y)
      <U2
      'y1' 'y2' 'y3' 'y4' 'y5'
      array(['y1', 'y2', 'y3', 'y4', 'y5'], dtype='<U2')

We can treat DatArray object just like numpy array and perform integer indexing on it. Below we have retrieved 2x2 from our Array1 DataArray.

In [26]:
dataset3.Array1[:2, :2]
Out[26]:
<xarray.DataArray 'Array1' (x: 2, y: 2)>
array([[28, 31],
       [81,  7]])
Coordinates:
  * x        (x) <U2 'x1' 'x2'
  * y        (y) <U2 'y1' 'y2'
xarray.DataArray
'Array1'
  • x: 2
  • y: 2
  • 28 31 81 7
    array([[28, 31],
           [81,  7]])
    • x
      (x)
      <U2
      'x1' 'x2'
      array(['x1', 'x2'], dtype='<U2')
    • y
      (y)
      <U2
      'y1' 'y2'
      array(['y1', 'y2'], dtype='<U2')

We can also use .loc property on our DataArray object just like pandas series/dataframe to retrieve a subset of an array by specifying actual index values which can be another data type than integer indexing.

In [27]:
dataset3.Array1.loc[["x1", "x2", "x3"]]
Out[27]:
<xarray.DataArray 'Array1' (x: 3, y: 5)>
array([[28, 31, 53, 71, 27],
       [81,  7, 15, 76, 55],
       [72,  2, 44, 59, 56]])
Coordinates:
  * x        (x) <U2 'x1' 'x2' 'x3'
  * y        (y) <U2 'y1' 'y2' 'y3' 'y4' 'y5'
xarray.DataArray
'Array1'
  • x: 3
  • y: 5
  • 28 31 53 71 27 81 7 15 76 55 72 2 44 59 56
    array([[28, 31, 53, 71, 27],
           [81,  7, 15, 76, 55],
           [72,  2, 44, 59, 56]])
    • x
      (x)
      <U2
      'x1' 'x2' 'x3'
      array(['x1', 'x2', 'x3'], dtype='<U2')
    • y
      (y)
      <U2
      'y1' 'y2' 'y3' 'y4' 'y5'
      array(['y1', 'y2', 'y3', 'y4', 'y5'], dtype='<U2')
In [28]:
dataset3.Array1.loc[["x1", "x2", "x3"], ["y1", "y2"]]
Out[28]:
<xarray.DataArray 'Array1' (x: 3, y: 2)>
array([[28, 31],
       [81,  7],
       [72,  2]])
Coordinates:
  * x        (x) <U2 'x1' 'x2' 'x3'
  * y        (y) <U2 'y1' 'y2'
xarray.DataArray
'Array1'
  • x: 3
  • y: 2
  • 28 31 81 7 72 2
    array([[28, 31],
           [81,  7],
           [72,  2]])
    • x
      (x)
      <U2
      'x1' 'x2' 'x3'
      array(['x1', 'x2', 'x3'], dtype='<U2')
    • y
      (y)
      <U2
      'y1' 'y2'
      array(['y1', 'y2'], dtype='<U2')

Indexing using isel() Function

In this section, we'll explain how we can use isel() method to index Dataset objects.

The isel() method let us use integer indexing to index our Dataset and provides two different ways to specify indexing details.

  1. We can provide indexing details as if dimension names are parameters of isel() method. The parameter name can be the dimension name of Dataset and the parameter value can be a single integer or list of integers specifying index values for a particular dimension.
  2. We can provide a dictionary where keys are dimension names and values are a list of integer indexes for a particular dimension.

Below we called isel() method on one of our Dataset object. We have treated dimension name 'x' of the Dataset object as parameter of isel() method and provided single index value to it. We have basically retrieved the 0th element from Dataset. This indexing will be applied on all DataArray and coordinates of Dataset. We can notice from the result that coordinates 'x' holds single value 0 and DataArray object 'Array1' and 'Array2' also holds single values which is 0th entry in both.

In [29]:
x = dataset1.isel(x=0)

x
Out[29]:
<xarray.Dataset>
Dimensions:  ()
Coordinates:
    x        int64 0
Data variables:
    Array1   float64 -1.086
    Array2   float64 1.651
xarray.Dataset
    • x
      ()
      int64
      0
      array(0)
    • Array1
      ()
      float64
      -1.086
      array(-1.0856306)
    • Array2
      ()
      float64
      1.651
      array(1.65143654)

In the below cell, we have explained how we can provide indexing details to isel() method as a dictionary. The below method call will have the same impact as our previous cell.

In [30]:
x = dataset1.isel({"x":0})

x
Out[30]:
<xarray.Dataset>
Dimensions:  ()
Coordinates:
    x        int64 0
Data variables:
    Array1   float64 -1.086
    Array2   float64 1.651
xarray.Dataset
    • x
      ()
      int64
      0
      array(0)
    • Array1
      ()
      float64
      -1.086
      array(-1.0856306)
    • Array2
      ()
      float64
      1.651
      array(1.65143654)

In the below cell, we have again called isel() on one of our Dataset objects. This time we have provided a list of integers as indexing values for dimension 'x' of our Dataset object. This will retrieve the first two elements from the Dataset. We can notice from the results how coordinate x is populated with the first two values and both DataArray objects 'Array1' and 'Array2' are populated with the first two values as per indexing details.

In [31]:
x = dataset1.isel(x=[0,1])

x
Out[31]:
<xarray.Dataset>
Dimensions:  (x: 2)
Coordinates:
  * x        (x) int64 0 1
Data variables:
    Array1   (x) float64 -1.086 0.9973
    Array2   (x) float64 1.651 -2.427
xarray.Dataset
    • x: 2
    • x
      (x)
      int64
      0 1
      array([0, 1])
    • Array1
      (x)
      float64
      -1.086 0.9973
      array([-1.0856306 ,  0.99734545])
    • Array2
      (x)
      float64
      1.651 -2.427
      array([ 1.65143654, -2.42667924])

In the below cell, we have explained again how we can provide indexing details as a dictionary. The below method call will return the same results as our previous cell method call.

In [32]:
x = dataset1.isel({'x':[0,1]})

x
Out[32]:
<xarray.Dataset>
Dimensions:  (x: 2)
Coordinates:
  * x        (x) int64 0 1
Data variables:
    Array1   (x) float64 -1.086 0.9973
    Array2   (x) float64 1.651 -2.427
xarray.Dataset
    • x: 2
    • x
      (x)
      int64
      0 1
      array([0, 1])
    • Array1
      (x)
      float64
      -1.086 0.9973
      array([-1.0856306 ,  0.99734545])
    • Array2
      (x)
      float64
      1.651 -2.427
      array([ 1.65143654, -2.42667924])

In the below cell, we have called isel() method on one of our Dataset objects which has two dimensions ('x and y'). We have asked it to select 0th and 1st values from dimension 'x' and 1st and 2nd values from dimension 'y'. It'll return a subset of our original Dataset based on these indexing details. We can notice from the results how 'x' and 'y' coordinate values are retrieved based on indexing. The DataArray objects 'Array1' and 'Array2' both holds 2x2 array.

In [33]:
x = dataset3.isel(x=[0,1], y=[1,2])

x
Out[33]:
<xarray.Dataset>
Dimensions:  (x: 2, y: 2)
Coordinates:
  * x        (x) <U2 'x1' 'x2'
  * y        (y) <U2 'y2' 'y3'
Data variables:
    Array1   (x, y) int64 31 53 7 15
    Array2   (x, y) float64 2.392 0.4129 -1.039 1.744
xarray.Dataset
    • x: 2
    • y: 2
    • x
      (x)
      <U2
      'x1' 'x2'
      array(['x1', 'x2'], dtype='<U2')
    • y
      (y)
      <U2
      'y2' 'y3'
      array(['y2', 'y3'], dtype='<U2')
    • Array1
      (x, y)
      int64
      31 53 7 15
      array([[31, 53],
             [ 7, 15]])
    • Array2
      (x, y)
      float64
      2.392 0.4129 -1.039 1.744
      array([[ 2.39236527,  0.41291216],
             [-1.03878821,  1.74371223]])

In the below cell, we have explained indexing on our Dataset with 3 dimensions using isel() method. We have retrieved a subset of Dataset which consists of Dataset formed by first and second values from all three dimensions.

In [34]:
x = dataset4.isel(x=[0,1], y=[0,1], z=[0,1])

x
Out[34]:
<xarray.Dataset>
Dimensions:  (x: 2, y: 2, z: 2)
Coordinates:
  * x        (x) int64 0 1
  * y        (y) int64 0 1
  * z        (z) int64 0 1
Data variables:
    Array1   (x, y, z) int64 7 10 55 28 66 77 71 14
    Array2   (x, y, z) float64 1.013 0.2787 -0.5521 ... 0.2538 0.5539 -0.5307
xarray.Dataset
    • x: 2
    • y: 2
    • z: 2
    • x
      (x)
      int64
      0 1
      array([0, 1])
    • y
      (y)
      int64
      0 1
      array([0, 1])
    • z
      (z)
      int64
      0 1
      array([0, 1])
    • Array1
      (x, y, z)
      int64
      7 10 55 28 66 77 71 14
      array([[[ 7, 10],
              [55, 28]],
      
             [[66, 77],
              [71, 14]]])
    • Array2
      (x, y, z)
      float64
      1.013 0.2787 ... 0.5539 -0.5307
      array([[[ 1.01273905,  0.27874086],
              [-0.55210807,  0.12074736]],
      
             [[ 0.14330773,  0.25381648],
              [ 0.55385617, -0.53067456]]])

In the below cell, we have also displayed the contents of two DataArray objects present inside of our Dataset we got using isel() method.

In [35]:
x.Array1
Out[35]:
<xarray.DataArray 'Array1' (x: 2, y: 2, z: 2)>
array([[[ 7, 10],
        [55, 28]],

       [[66, 77],
        [71, 14]]])
Coordinates:
  * x        (x) int64 0 1
  * y        (y) int64 0 1
  * z        (z) int64 0 1
xarray.DataArray
'Array1'
  • x: 2
  • y: 2
  • z: 2
  • 7 10 55 28 66 77 71 14
    array([[[ 7, 10],
            [55, 28]],
    
           [[66, 77],
            [71, 14]]])
    • x
      (x)
      int64
      0 1
      array([0, 1])
    • y
      (y)
      int64
      0 1
      array([0, 1])
    • z
      (z)
      int64
      0 1
      array([0, 1])
In [36]:
x.Array2
Out[36]:
<xarray.DataArray 'Array2' (x: 2, y: 2, z: 2)>
array([[[ 1.01273905,  0.27874086],
        [-0.55210807,  0.12074736]],

       [[ 0.14330773,  0.25381648],
        [ 0.55385617, -0.53067456]]])
Coordinates:
  * x        (x) int64 0 1
  * y        (y) int64 0 1
  * z        (z) int64 0 1
xarray.DataArray
'Array2'
  • x: 2
  • y: 2
  • z: 2
  • 1.013 0.2787 -0.5521 0.1207 0.1433 0.2538 0.5539 -0.5307
    array([[[ 1.01273905,  0.27874086],
            [-0.55210807,  0.12074736]],
    
           [[ 0.14330773,  0.25381648],
            [ 0.55385617, -0.53067456]]])
    • x
      (x)
      int64
      0 1
      array([0, 1])
    • y
      (y)
      int64
      0 1
      array([0, 1])
    • z
      (z)
      int64
      0 1
      array([0, 1])

In the below cell, we have again called isel() method on our Dataset which had details about temperature, humidity, pressure, etc. The dimension names in that Dataset were x, y, and time.

We can notice from the results that how a subset of coordinates and DataArray objects are retrieved based on indexing details given to the method.

In [37]:
x = dataset6.isel(x=[0, 1], y=[0,1], time=[0,1])

x
Out[37]:
<xarray.Dataset>
Dimensions:        (x: 2, y: 2, time: 2)
Coordinates:
    lon            (x, y) float64 1.0 2.0 6.0 7.0
    lat            (x, y) float64 15.0 16.07 20.36 21.43
  * time           (time) datetime64[ns] 2021-01-01 2021-01-02
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 82 3 38 28 51 28 93 28
    Humidity       (x, y, time) float64 1.093 -0.8485 2.097 ... 0.9206 -0.3255
    Pressure       (x, y, time) float64 1.176 -1.544 0.3667 ... 2.002 -0.2852
    WindSpeed      (x, y, time) float64 0.07895 -0.1061 ... -0.4384 0.07389
    Precipitation  (x, y, time) float64 -0.5524 0.5605 -1.196 ... 2.24 2.155
    PM25           (x, y, time) float64 1.149 -1.045 0.8266 ... -0.8372 2.127
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 2
    • y: 2
    • time: 2
    • lon
      (x, y)
      float64
      1.0 2.0 6.0 7.0
      array([[1., 2.],
             [6., 7.]])
    • lat
      (x, y)
      float64
      15.0 16.07 20.36 21.43
      array([[15.        , 16.07142857],
             [20.35714286, 21.42857143]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 2021-01-02
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000'],
            dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      82 3 38 28 51 28 93 28
      array([[[82,  3],
              [38, 28]],
      
             [[51, 28],
              [93, 28]]])
    • Humidity
      (x, y, time)
      float64
      1.093 -0.8485 ... 0.9206 -0.3255
      array([[[ 1.09285914, -0.84854387],
              [ 2.09665566,  0.15985731]],
      
             [[-0.37605972, -0.54091566],
              [ 0.92056561, -0.32548426]]])
    • Pressure
      (x, y, time)
      float64
      1.176 -1.544 ... 2.002 -0.2852
      array([[[ 1.17620022, -1.54431399],
              [ 0.36674592,  0.670313  ]],
      
             [[ 1.63744119, -0.3252367 ],
              [ 2.00222948, -0.28522905]]])
    • WindSpeed
      (x, y, time)
      float64
      0.07895 -0.1061 ... -0.4384 0.07389
      array([[[ 0.07894539, -0.10608959],
              [-1.05765309, -1.71632891]],
      
             [[-1.20795325, -1.27012772],
              [-0.43844783,  0.07389177]]])
    • Precipitation
      (x, y, time)
      float64
      -0.5524 0.5605 ... 2.24 2.155
      array([[[-0.55236716,  0.56045232],
              [-1.19625733, -0.55042156]],
      
             [[ 0.73722263, -0.28084061],
              [ 2.24012964,  2.15538612]]])
    • PM25
      (x, y, time)
      float64
      1.149 -1.045 ... -0.8372 2.127
      array([[[ 1.14942133, -1.04513297],
              [ 0.82658676, -0.01658263]],
      
             [[ 1.70862604,  0.83200933],
              [-0.8371662 ,  2.12666983]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record
In [38]:
x.Temperature
Out[38]:
<xarray.DataArray 'Temperature' (x: 2, y: 2, time: 2)>
array([[[82,  3],
        [38, 28]],

       [[51, 28],
        [93, 28]]])
Coordinates:
    lon      (x, y) float64 1.0 2.0 6.0 7.0
    lat      (x, y) float64 15.0 16.07 20.36 21.43
  * time     (time) datetime64[ns] 2021-01-01 2021-01-02
Dimensions without coordinates: x, y
xarray.DataArray
'Temperature'
  • x: 2
  • y: 2
  • time: 2
  • 82 3 38 28 51 28 93 28
    array([[[82,  3],
            [38, 28]],
    
           [[51, 28],
            [93, 28]]])
    • lon
      (x, y)
      float64
      1.0 2.0 6.0 7.0
      array([[1., 2.],
             [6., 7.]])
    • lat
      (x, y)
      float64
      15.0 16.07 20.36 21.43
      array([[15.        , 16.07142857],
             [20.35714286, 21.42857143]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 2021-01-02
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000'],
            dtype='datetime64[ns]')
In [39]:
x.PM25
Out[39]:
<xarray.DataArray 'PM25' (x: 2, y: 2, time: 2)>
array([[[ 1.14942133, -1.04513297],
        [ 0.82658676, -0.01658263]],

       [[ 1.70862604,  0.83200933],
        [-0.8371662 ,  2.12666983]]])
Coordinates:
    lon      (x, y) float64 1.0 2.0 6.0 7.0
    lat      (x, y) float64 15.0 16.07 20.36 21.43
  * time     (time) datetime64[ns] 2021-01-01 2021-01-02
Dimensions without coordinates: x, y
xarray.DataArray
'PM25'
  • x: 2
  • y: 2
  • time: 2
  • 1.149 -1.045 0.8266 -0.01658 1.709 0.832 -0.8372 2.127
    array([[[ 1.14942133, -1.04513297],
            [ 0.82658676, -0.01658263]],
    
           [[ 1.70862604,  0.83200933],
            [-0.8371662 ,  2.12666983]]])
    • lon
      (x, y)
      float64
      1.0 2.0 6.0 7.0
      array([[1., 2.],
             [6., 7.]])
    • lat
      (x, y)
      float64
      15.0 16.07 20.36 21.43
      array([[15.        , 16.07142857],
             [20.35714286, 21.42857143]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 2021-01-02
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000'],
            dtype='datetime64[ns]')

Indexing using sel() Function

In this section, we have explained how we can use sel() method to perform indexing on our Dataset object.

The sel() method works exactly like isel() method but it accepts actual values of dimension to index Dataset object. The isel() method only accepts integer indexing values to index Dataset objects but sel() method accepts actual values of dimensions which can be of any data type (integer, string, datetime, etc).

Just like isel() method, it also lets us specify indexing details in two ways.

  1. We can provide indexing details as if dimension names are parameters of sel() method. The parameter name can be the dimension name of Dataset and the parameter value can be a single dimension value or list of values specifying index values for a particular dimension.
  2. We can provide a dictionary where keys are dimension names and values are a list of index values for a particular dimension.

Below we have used sel() method to retrieve a subset of one of our Dataset objects. The Dataset object used in this example had integers as values of dimensions hence integer indexing is used. When dimension values of Dataset is of type integers then isel() and sel() methods will work same. It's different when the data type of values of dimension is different.

In [40]:
x = dataset2.sel(x=[0,1], y=[0,1,2])

x
Out[40]:
<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) int64 0 1
  * y        (y) int64 0 1 2
Data variables:
    Array1   (x, y) int64 84 79 37 69 50 56
    Array2   (x, y) float64 1.004 0.3862 0.7374 1.176 -1.254 -0.6378
xarray.Dataset
    • x: 2
    • y: 3
    • x
      (x)
      int64
      0 1
      array([0, 1])
    • y
      (y)
      int64
      0 1 2
      array([0, 1, 2])
    • Array1
      (x, y)
      int64
      84 79 37 69 50 56
      array([[84, 79, 37],
             [69, 50, 56]])
    • Array2
      (x, y)
      float64
      1.004 0.3862 ... -1.254 -0.6378
      array([[ 1.0040539 ,  0.3861864 ,  0.73736858],
             [ 1.17582904, -1.25388067, -0.6377515 ]])

In the below example, we have explained how we can provide indexing details as a dictionary to sel() method. The output of the below cell will be the same as our previous cell because the indexing details are the same.

In [41]:
x = dataset2.sel({'x':[0,1], 'y':[0,1,2]})

x
Out[41]:
<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) int64 0 1
  * y        (y) int64 0 1 2
Data variables:
    Array1   (x, y) int64 84 79 37 69 50 56
    Array2   (x, y) float64 1.004 0.3862 0.7374 1.176 -1.254 -0.6378
xarray.Dataset
    • x: 2
    • y: 3
    • x
      (x)
      int64
      0 1
      array([0, 1])
    • y
      (y)
      int64
      0 1 2
      array([0, 1, 2])
    • Array1
      (x, y)
      int64
      84 79 37 69 50 56
      array([[84, 79, 37],
             [69, 50, 56]])
    • Array2
      (x, y)
      float64
      1.004 0.3862 ... -1.254 -0.6378
      array([[ 1.0040539 ,  0.3861864 ,  0.73736858],
             [ 1.17582904, -1.25388067, -0.6377515 ]])

In the below cell, We have provided actual string values of dimensions to sel() method to subset our Dataset object.

In [42]:
x  = dataset3.sel(x=["x1", "x2"], y=["y1", "y2", "y3"])

x
Out[42]:
<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) <U2 'x1' 'x2'
  * y        (y) <U2 'y1' 'y2' 'y3'
Data variables:
    Array1   (x, y) int64 28 31 53 81 7 15
    Array2   (x, y) float64 -0.6999 2.392 0.4129 -1.294 -1.039 1.744
xarray.Dataset
    • x: 2
    • y: 3
    • x
      (x)
      <U2
      'x1' 'x2'
      array(['x1', 'x2'], dtype='<U2')
    • y
      (y)
      <U2
      'y1' 'y2' 'y3'
      array(['y1', 'y2', 'y3'], dtype='<U2')
    • Array1
      (x, y)
      int64
      28 31 53 81 7 15
      array([[28, 31, 53],
             [81,  7, 15]])
    • Array2
      (x, y)
      float64
      -0.6999 2.392 ... -1.039 1.744
      array([[-0.69987723,  2.39236527,  0.41291216],
             [-1.29408532, -1.03878821,  1.74371223]])

In the below cell, we have tried to use sel() method to subset a Dataset object whose one dimension values are of type datetime. We have asked it to retrieve a subset of Dataset with a single date in time dimension.

Please make a NOTE how we provided datetime details as a string. We can provide datetime details as a string or original datetime type as well.

Then in the next few cells after the below cell, we have displayed coordinate and DataArray object detail of subset Dataset object that we got using sel() method.

In [43]:
x = dataset6.sel(x=0, y=0, time="2021-1-1")

x
Out[43]:
<xarray.Dataset>
Dimensions:        ()
Coordinates:
    lon            float64 1.0
    lat            float64 15.0
    time           datetime64[ns] 2021-01-01
Data variables:
    Temperature    int64 82
    Humidity       float64 1.093
    Pressure       float64 1.176
    WindSpeed      float64 0.07895
    Precipitation  float64 -0.5524
    PM25           float64 1.149
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • lon
      ()
      float64
      1.0
      array(1.)
    • lat
      ()
      float64
      15.0
      array(15.)
    • time
      ()
      datetime64[ns]
      2021-01-01
      array('2021-01-01T00:00:00.000000000', dtype='datetime64[ns]')
    • Temperature
      ()
      int64
      82
      array(82)
    • Humidity
      ()
      float64
      1.093
      array(1.09285914)
    • Pressure
      ()
      float64
      1.176
      array(1.17620022)
    • WindSpeed
      ()
      float64
      0.07895
      array(0.07894539)
    • Precipitation
      ()
      float64
      -0.5524
      array(-0.55236716)
    • PM25
      ()
      float64
      1.149
      array(1.14942133)
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record
In [44]:
x.lon
Out[44]:
<xarray.DataArray 'lon' ()>
array(1.)
Coordinates:
    lon      float64 1.0
    lat      float64 15.0
    time     datetime64[ns] 2021-01-01
xarray.DataArray
'lon'
  • 1.0
    array(1.)
    • lon
      ()
      float64
      1.0
      array(1.)
    • lat
      ()
      float64
      15.0
      array(15.)
    • time
      ()
      datetime64[ns]
      2021-01-01
      array('2021-01-01T00:00:00.000000000', dtype='datetime64[ns]')
In [45]:
x.Precipitation
Out[45]:
<xarray.DataArray 'Precipitation' ()>
array(-0.55236716)
Coordinates:
    lon      float64 1.0
    lat      float64 15.0
    time     datetime64[ns] 2021-01-01
xarray.DataArray
'Precipitation'
  • -0.5524
    array(-0.55236716)
    • lon
      ()
      float64
      1.0
      array(1.)
    • lat
      ()
      float64
      15.0
      array(15.)
    • time
      ()
      datetime64[ns]
      2021-01-01
      array('2021-01-01T00:00:00.000000000', dtype='datetime64[ns]')

In the below cell, we have created another example demonstrating usage of sel() method on Dataset whose one dimension values are of datetime type. This time we have provided a list of two strings specifying two different dates as values of time dimension inside sel() method function call.

Then in the next cell after the below cells, we have also displayed coordinate and DataArray object details of subset Dataset that we got through sel() method call.

In [46]:
x = dataset6.sel(x=[0,1], y=[0,1], time=["2021-1-1","2021-1-2"])

x
Out[46]:
<xarray.Dataset>
Dimensions:        (x: 2, y: 2, time: 2)
Coordinates:
    lon            (x, y) float64 1.0 2.0 6.0 7.0
    lat            (x, y) float64 15.0 16.07 20.36 21.43
  * time           (time) datetime64[ns] 2021-01-01 2021-01-02
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 82 3 38 28 51 28 93 28
    Humidity       (x, y, time) float64 1.093 -0.8485 2.097 ... 0.9206 -0.3255
    Pressure       (x, y, time) float64 1.176 -1.544 0.3667 ... 2.002 -0.2852
    WindSpeed      (x, y, time) float64 0.07895 -0.1061 ... -0.4384 0.07389
    Precipitation  (x, y, time) float64 -0.5524 0.5605 -1.196 ... 2.24 2.155
    PM25           (x, y, time) float64 1.149 -1.045 0.8266 ... -0.8372 2.127
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 2
    • y: 2
    • time: 2
    • lon
      (x, y)
      float64
      1.0 2.0 6.0 7.0
      array([[1., 2.],
             [6., 7.]])
    • lat
      (x, y)
      float64
      15.0 16.07 20.36 21.43
      array([[15.        , 16.07142857],
             [20.35714286, 21.42857143]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 2021-01-02
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000'],
            dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      82 3 38 28 51 28 93 28
      array([[[82,  3],
              [38, 28]],
      
             [[51, 28],
              [93, 28]]])
    • Humidity
      (x, y, time)
      float64
      1.093 -0.8485 ... 0.9206 -0.3255
      array([[[ 1.09285914, -0.84854387],
              [ 2.09665566,  0.15985731]],
      
             [[-0.37605972, -0.54091566],
              [ 0.92056561, -0.32548426]]])
    • Pressure
      (x, y, time)
      float64
      1.176 -1.544 ... 2.002 -0.2852
      array([[[ 1.17620022, -1.54431399],
              [ 0.36674592,  0.670313  ]],
      
             [[ 1.63744119, -0.3252367 ],
              [ 2.00222948, -0.28522905]]])
    • WindSpeed
      (x, y, time)
      float64
      0.07895 -0.1061 ... -0.4384 0.07389
      array([[[ 0.07894539, -0.10608959],
              [-1.05765309, -1.71632891]],
      
             [[-1.20795325, -1.27012772],
              [-0.43844783,  0.07389177]]])
    • Precipitation
      (x, y, time)
      float64
      -0.5524 0.5605 ... 2.24 2.155
      array([[[-0.55236716,  0.56045232],
              [-1.19625733, -0.55042156]],
      
             [[ 0.73722263, -0.28084061],
              [ 2.24012964,  2.15538612]]])
    • PM25
      (x, y, time)
      float64
      1.149 -1.045 ... -0.8372 2.127
      array([[[ 1.14942133, -1.04513297],
              [ 0.82658676, -0.01658263]],
      
             [[ 1.70862604,  0.83200933],
              [-0.8371662 ,  2.12666983]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record
In [47]:
x.lon
Out[47]:
<xarray.DataArray 'lon' (x: 2, y: 2)>
array([[1., 2.],
       [6., 7.]])
Coordinates:
    lon      (x, y) float64 1.0 2.0 6.0 7.0
    lat      (x, y) float64 15.0 16.07 20.36 21.43
Dimensions without coordinates: x, y
xarray.DataArray
'lon'
  • x: 2
  • y: 2
  • 1.0 2.0 6.0 7.0
    array([[1., 2.],
           [6., 7.]])
    • lon
      (x, y)
      float64
      1.0 2.0 6.0 7.0
      array([[1., 2.],
             [6., 7.]])
    • lat
      (x, y)
      float64
      15.0 16.07 20.36 21.43
      array([[15.        , 16.07142857],
             [20.35714286, 21.42857143]])
In [48]:
x.Humidity
Out[48]:
<xarray.DataArray 'Humidity' (x: 2, y: 2, time: 2)>
array([[[ 1.09285914, -0.84854387],
        [ 2.09665566,  0.15985731]],

       [[-0.37605972, -0.54091566],
        [ 0.92056561, -0.32548426]]])
Coordinates:
    lon      (x, y) float64 1.0 2.0 6.0 7.0
    lat      (x, y) float64 15.0 16.07 20.36 21.43
  * time     (time) datetime64[ns] 2021-01-01 2021-01-02
Dimensions without coordinates: x, y
xarray.DataArray
'Humidity'
  • x: 2
  • y: 2
  • time: 2
  • 1.093 -0.8485 2.097 0.1599 -0.3761 -0.5409 0.9206 -0.3255
    array([[[ 1.09285914, -0.84854387],
            [ 2.09665566,  0.15985731]],
    
           [[-0.37605972, -0.54091566],
            [ 0.92056561, -0.32548426]]])
    • lon
      (x, y)
      float64
      1.0 2.0 6.0 7.0
      array([[1., 2.],
             [6., 7.]])
    • lat
      (x, y)
      float64
      15.0 16.07 20.36 21.43
      array([[15.        , 16.07142857],
             [20.35714286, 21.42857143]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 2021-01-02
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000'],
            dtype='datetime64[ns]')

In the below cell, we have created another example demonstrating usage of sel() method on Dataset with datetime dimension. This time we have provided datetime values as a list of datetime type values created using pd.date_range() function. We can perform indexing on the dimension with datetime type values in this way as well.

In [49]:
x = dataset6.sel(x=[0,1], y=[0,1], time=pd.date_range(start="2021-1-1", periods=3))

x
Out[49]:
<xarray.Dataset>
Dimensions:        (x: 2, y: 2, time: 3)
Coordinates:
    lon            (x, y) float64 1.0 2.0 6.0 7.0
    lat            (x, y) float64 15.0 16.07 20.36 21.43
  * time           (time) datetime64[ns] 2021-01-01 2021-01-02 2021-01-03
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 82 3 36 38 28 47 51 28 99 93 28 74
    Humidity       (x, y, time) float64 1.093 -0.8485 0.1826 ... -0.3255 1.422
    Pressure       (x, y, time) float64 1.176 -1.544 -0.6974 ... -0.2852 0.4516
    WindSpeed      (x, y, time) float64 0.07895 -0.1061 ... 0.07389 0.581
    Precipitation  (x, y, time) float64 -0.5524 0.5605 0.3806 ... 2.155 0.02794
    PM25           (x, y, time) float64 1.149 -1.045 -0.08036 ... 2.127 -0.3254
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 2
    • y: 2
    • time: 3
    • lon
      (x, y)
      float64
      1.0 2.0 6.0 7.0
      array([[1., 2.],
             [6., 7.]])
    • lat
      (x, y)
      float64
      15.0 16.07 20.36 21.43
      array([[15.        , 16.07142857],
             [20.35714286, 21.42857143]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 2021-01-02 2021-01-03
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000',
             '2021-01-03T00:00:00.000000000'], dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      82 3 36 38 28 47 51 28 99 93 28 74
      array([[[82,  3, 36],
              [38, 28, 47]],
      
             [[51, 28, 99],
              [93, 28, 74]]])
    • Humidity
      (x, y, time)
      float64
      1.093 -0.8485 ... -0.3255 1.422
      array([[[ 1.09285914, -0.84854387,  0.1826349 ],
              [ 2.09665566,  0.15985731,  0.25153841]],
      
             [[-0.37605972, -0.54091566,  0.82583929],
              [ 0.92056561, -0.32548426,  1.42181688]]])
    • Pressure
      (x, y, time)
      float64
      1.176 -1.544 ... -0.2852 0.4516
      array([[[ 1.17620022, -1.54431399, -0.69740179],
              [ 0.36674592,  0.670313  ,  0.79318369]],
      
             [[ 1.63744119, -0.3252367 , -0.7357182 ],
              [ 2.00222948, -0.28522905,  0.45157815]]])
    • WindSpeed
      (x, y, time)
      float64
      0.07895 -0.1061 ... 0.07389 0.581
      array([[[ 0.07894539, -0.10608959, -0.15073608],
              [-1.05765309, -1.71632891,  1.12384288]],
      
             [[-1.20795325, -1.27012772,  1.3664836 ],
              [-0.43844783,  0.07389177,  0.58102792]]])
    • Precipitation
      (x, y, time)
      float64
      -0.5524 0.5605 ... 2.155 0.02794
      array([[[-0.55236716,  0.56045232,  0.38064352],
              [-1.19625733, -0.55042156,  1.10772148]],
      
             [[ 0.73722263, -0.28084061, -1.59074815],
              [ 2.24012964,  2.15538612,  0.02794398]]])
    • PM25
      (x, y, time)
      float64
      1.149 -1.045 ... 2.127 -0.3254
      array([[[ 1.14942133, -1.04513297, -0.08036076],
              [ 0.82658676, -0.01658263, -1.67572929]],
      
             [[ 1.70862604,  0.83200933, -0.51742151],
              [-0.8371662 ,  2.12666983, -0.32537831]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record

4. Normal Operations on Dataset

In this section, we'll explain commonly performing operations on Dataset objects like transpose, copy, change coordinate details, change attribute details, fill NaNs, add new attributes, etc. We'll explain various methods available from xarray to perform these operations.

Please make a NOTE that all methods in this section return a new Dataset object with details modified. It does not modify any Dataset object in place.

assign()

The assign() method lets us add new DataArray to our Dataset object. The method takes as input dictionary in the same format which we provide to Dataset() constructor to add new DataArray objects.

Below we have first created an array of random numbers with shape (3,5). We have then added this array to our Dataset object using assign() method. We have provided array name as key and value is a tuple of dimension names and actual data. We can add more than one DataArray to our Dataset object using this method.

In [50]:
arr3  = np.random.randn(3,5)

dataset2.assign({"Array3": (["x","y"], arr3)})
Out[50]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (x, y) int64 84 79 37 97 81 69 50 56 68 3 85 40 67 85 48
    Array2   (x, y) float64 1.004 0.3862 0.7374 1.491 ... -0.2556 -2.799 -1.772
    Array3   (x, y) float64 -1.472 2.394 -0.833 ... 0.9497 -0.004461 0.3107
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x, y)
      int64
      84 79 37 97 81 ... 85 40 67 85 48
      array([[84, 79, 37, 97, 81],
             [69, 50, 56, 68,  3],
             [85, 40, 67, 85, 48]])
    • Array2
      (x, y)
      float64
      1.004 0.3862 ... -2.799 -1.772
      array([[ 1.0040539 ,  0.3861864 ,  0.73736858,  1.49073203, -0.93583387],
             [ 1.17582904, -1.25388067, -0.6377515 ,  0.9071052 , -1.4286807 ],
             [-0.14006872, -0.8617549 , -0.25561937, -2.79858911, -1.7715331 ]])
    • Array3
      (x, y)
      float64
      -1.472 2.394 ... -0.004461 0.3107
      array([[-1.47205882,  2.39416739, -0.83302426,  0.76174253, -0.83201262],
             [-1.05491414, -0.94428225, -0.3252783 ,  1.08234614, -0.11124493],
             [-0.89092814, -1.25784033,  0.94973832, -0.0044614 ,  0.31073005]])

assign_attrs()

The assign_attrs() method takes as input dictionary of attributes and adds those attributes to Dataset object. If attributes are already present in Dataset object then provided attributes will add/update attributes. If Dataset does not have attributes then attributes will be added to it.

In [51]:
dataset2.assign_attrs({"x": "Row-Dimension", "y": "Column-Dimension"})
Out[51]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (x, y) int64 84 79 37 97 81 69 50 56 68 3 85 40 67 85 48
    Array2   (x, y) float64 1.004 0.3862 0.7374 1.491 ... -0.2556 -2.799 -1.772
Attributes:
    x:        Row-Dimension
    y:        Column-Dimension
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x, y)
      int64
      84 79 37 97 81 ... 85 40 67 85 48
      array([[84, 79, 37, 97, 81],
             [69, 50, 56, 68,  3],
             [85, 40, 67, 85, 48]])
    • Array2
      (x, y)
      float64
      1.004 0.3862 ... -2.799 -1.772
      array([[ 1.0040539 ,  0.3861864 ,  0.73736858,  1.49073203, -0.93583387],
             [ 1.17582904, -1.25388067, -0.6377515 ,  0.9071052 , -1.4286807 ],
             [-0.14006872, -0.8617549 , -0.25561937, -2.79858911, -1.7715331 ]])
  • x :
    Row-Dimension
    y :
    Column-Dimension

assign_coords()

The assign_coords() method let us update coordinates detail of Dataset object. It accepts dictionary specifying coordinate details just like we provide in Dataset() constructor when creating Dataset object.

In the below example, we have replaced the existing integer coordinates of Dataset object with a list of string coordinates.

In the next cell below, we have also tried to retrieve subset Dataset based on these new coordinate values.

In [52]:
dataset2_new_coords = dataset2.assign_coords(coords={"x": ["a1","a2","a3"], "y": ["b1","b2","b3","b4","b5"]})

dataset2_new_coords
Out[52]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) <U2 'a1' 'a2' 'a3'
  * y        (y) <U2 'b1' 'b2' 'b3' 'b4' 'b5'
Data variables:
    Array1   (x, y) int64 84 79 37 97 81 69 50 56 68 3 85 40 67 85 48
    Array2   (x, y) float64 1.004 0.3862 0.7374 1.491 ... -0.2556 -2.799 -1.772
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      <U2
      'a1' 'a2' 'a3'
      array(['a1', 'a2', 'a3'], dtype='<U2')
    • y
      (y)
      <U2
      'b1' 'b2' 'b3' 'b4' 'b5'
      array(['b1', 'b2', 'b3', 'b4', 'b5'], dtype='<U2')
    • Array1
      (x, y)
      int64
      84 79 37 97 81 ... 85 40 67 85 48
      array([[84, 79, 37, 97, 81],
             [69, 50, 56, 68,  3],
             [85, 40, 67, 85, 48]])
    • Array2
      (x, y)
      float64
      1.004 0.3862 ... -2.799 -1.772
      array([[ 1.0040539 ,  0.3861864 ,  0.73736858,  1.49073203, -0.93583387],
             [ 1.17582904, -1.25388067, -0.6377515 ,  0.9071052 , -1.4286807 ],
             [-0.14006872, -0.8617549 , -0.25561937, -2.79858911, -1.7715331 ]])
In [53]:
dataset2_new_coords.sel(x=["a1","a2"], y=["b1", "b2"])
Out[53]:
<xarray.Dataset>
Dimensions:  (x: 2, y: 2)
Coordinates:
  * x        (x) <U2 'a1' 'a2'
  * y        (y) <U2 'b1' 'b2'
Data variables:
    Array1   (x, y) int64 84 79 69 50
    Array2   (x, y) float64 1.004 0.3862 1.176 -1.254
xarray.Dataset
    • x: 2
    • y: 2
    • x
      (x)
      <U2
      'a1' 'a2'
      array(['a1', 'a2'], dtype='<U2')
    • y
      (y)
      <U2
      'b1' 'b2'
      array(['b1', 'b2'], dtype='<U2')
    • Array1
      (x, y)
      int64
      84 79 69 50
      array([[84, 79],
             [69, 50]])
    • Array2
      (x, y)
      float64
      1.004 0.3862 1.176 -1.254
      array([[ 1.0040539 ,  0.3861864 ],
             [ 1.17582904, -1.25388067]])

clip()

The clip() method takes range specified as the minimum and maximum number. It then replaces all values which are less than minimum with minimum value and all values greater than maximum with maximum value. All values in the range are kept unchanged.

Below we have restricted values of our Dataset in the range (0.3, 0.6).

In [54]:
dataset2_clipped = dataset2.clip(min=0.3, max=0.6)

dataset2_clipped
Out[54]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (x, y) float64 0.6 0.6 0.6 0.6 0.6 0.6 ... 0.6 0.6 0.6 0.6 0.6 0.6
    Array2   (x, y) float64 0.6 0.3862 0.6 0.6 0.3 0.6 ... 0.3 0.3 0.3 0.3 0.3
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x, y)
      float64
      0.6 0.6 0.6 0.6 ... 0.6 0.6 0.6 0.6
      array([[0.6, 0.6, 0.6, 0.6, 0.6],
             [0.6, 0.6, 0.6, 0.6, 0.6],
             [0.6, 0.6, 0.6, 0.6, 0.6]])
    • Array2
      (x, y)
      float64
      0.6 0.3862 0.6 0.6 ... 0.3 0.3 0.3
      array([[0.6      , 0.3861864, 0.6      , 0.6      , 0.3      ],
             [0.6      , 0.3      , 0.3      , 0.6      , 0.3      ],
             [0.3      , 0.3      , 0.3      , 0.3      , 0.3      ]])
In [55]:
dataset2_clipped.Array2
Out[55]:
<xarray.DataArray 'Array2' (x: 3, y: 5)>
array([[0.6      , 0.3861864, 0.6      , 0.6      , 0.3      ],
       [0.6      , 0.3      , 0.3      , 0.6      , 0.3      ],
       [0.3      , 0.3      , 0.3      , 0.3      , 0.3      ]])
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
xarray.DataArray
'Array2'
  • x: 3
  • y: 5
  • 0.6 0.3862 0.6 0.6 0.3 0.6 0.3 0.3 0.6 0.3 0.3 0.3 0.3 0.3 0.3
    array([[0.6      , 0.3861864, 0.6      , 0.6      , 0.3      ],
           [0.6      , 0.3      , 0.3      , 0.6      , 0.3      ],
           [0.3      , 0.3      , 0.3      , 0.3      , 0.3      ]])
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])

copy()

We can easily create a copy of Dataset object by calling copy() method on it.

In [56]:
dataset2_copy = dataset2.copy()

dataset2_copy
Out[56]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (x, y) int64 84 79 37 97 81 69 50 56 68 3 85 40 67 85 48
    Array2   (x, y) float64 1.004 0.3862 0.7374 1.491 ... -0.2556 -2.799 -1.772
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x, y)
      int64
      84 79 37 97 81 ... 85 40 67 85 48
      array([[84, 79, 37, 97, 81],
             [69, 50, 56, 68,  3],
             [85, 40, 67, 85, 48]])
    • Array2
      (x, y)
      float64
      1.004 0.3862 ... -2.799 -1.772
      array([[ 1.0040539 ,  0.3861864 ,  0.73736858,  1.49073203, -0.93583387],
             [ 1.17582904, -1.25388067, -0.6377515 ,  0.9071052 , -1.4286807 ],
             [-0.14006872, -0.8617549 , -0.25561937, -2.79858911, -1.7715331 ]])

astype()

We can change data type of DataArray present in Dataset object using astype() method. It accepts python or numpy data types as input to specify the data type.

In [57]:
dataset2_copy = dataset2.copy().astype(np.float32)

dataset2_copy
Out[57]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (x, y) float32 84.0 79.0 37.0 97.0 81.0 ... 40.0 67.0 85.0 48.0
    Array2   (x, y) float32 1.004 0.3862 0.7374 1.491 ... -0.2556 -2.799 -1.772
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x, y)
      float32
      84.0 79.0 37.0 ... 67.0 85.0 48.0
      array([[84., 79., 37., 97., 81.],
             [69., 50., 56., 68.,  3.],
             [85., 40., 67., 85., 48.]], dtype=float32)
    • Array2
      (x, y)
      float32
      1.004 0.3862 ... -2.799 -1.772
      array([[ 1.004054  ,  0.3861864 ,  0.7373686 ,  1.4907321 , -0.9358339 ],
             [ 1.175829  , -1.2538806 , -0.6377515 ,  0.9071052 , -1.4286807 ],
             [-0.14006872, -0.8617549 , -0.25561938, -2.798589  , -1.7715331 ]],
            dtype=float32)

fillna()

The fillna() method accepts single value as input and replaces all NaNs in Dataset object with that value. It'll replace NaN values present inside DataArray objects of our Dataset object.

In [58]:
dataset2_copy.Array1[0,0] = np.nan

dataset2_copy.Array1
Out[58]:
<xarray.DataArray 'Array1' (x: 3, y: 5)>
array([[nan, 79., 37., 97., 81.],
       [69., 50., 56., 68.,  3.],
       [85., 40., 67., 85., 48.]], dtype=float32)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
xarray.DataArray
'Array1'
  • x: 3
  • y: 5
  • nan 79.0 37.0 97.0 81.0 69.0 50.0 ... 3.0 85.0 40.0 67.0 85.0 48.0
    array([[nan, 79., 37., 97., 81.],
           [69., 50., 56., 68.,  3.],
           [85., 40., 67., 85., 48.]], dtype=float32)
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
In [59]:
dataset2_copy = dataset2_copy.fillna(value=9999.)

dataset2_copy.Array1
Out[59]:
<xarray.DataArray 'Array1' (x: 3, y: 5)>
array([[9.999e+03, 7.900e+01, 3.700e+01, 9.700e+01, 8.100e+01],
       [6.900e+01, 5.000e+01, 5.600e+01, 6.800e+01, 3.000e+00],
       [8.500e+01, 4.000e+01, 6.700e+01, 8.500e+01, 4.800e+01]],
      dtype=float32)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
xarray.DataArray
'Array1'
  • x: 3
  • y: 5
  • 9.999e+03 79.0 37.0 97.0 81.0 69.0 ... 3.0 85.0 40.0 67.0 85.0 48.0
    array([[9.999e+03, 7.900e+01, 3.700e+01, 9.700e+01, 8.100e+01],
           [6.900e+01, 5.000e+01, 5.600e+01, 6.800e+01, 3.000e+00],
           [8.500e+01, 4.000e+01, 6.700e+01, 8.500e+01, 4.800e+01]],
          dtype=float32)
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])

drop_vars(names)

We can easily drop DataArray objects from Dataset object using drop_vars() method. We need to provide a list of DataArray object names as input to the method and it'll return a new Dataset object with those DataArray objects removed.

In [60]:
dataset6.drop_vars(names=["Temperature", "Pressure", "PM25"])
Out[60]:
<xarray.Dataset>
Dimensions:        (x: 3, y: 5, time: 7)
Coordinates:
    lon            (x, y) float64 1.0 2.0 3.0 4.0 5.0 ... 12.0 13.0 14.0 15.0
    lat            (x, y) float64 15.0 16.07 17.14 18.21 ... 27.86 28.93 30.0
  * time           (time) datetime64[ns] 2021-01-01 2021-01-02 ... 2021-01-07
Dimensions without coordinates: x, y
Data variables:
    Humidity       (x, y, time) float64 1.093 -0.8485 0.1826 ... -1.53 1.676
    WindSpeed      (x, y, time) float64 0.07895 -0.1061 ... 0.2155 -1.026
    Precipitation  (x, y, time) float64 -0.5524 0.5605 0.3806 ... -0.7978 -1.678
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 3
    • y: 5
    • time: 7
    • lon
      (x, y)
      float64
      1.0 2.0 3.0 4.0 ... 13.0 14.0 15.0
      array([[ 1.,  2.,  3.,  4.,  5.],
             [ 6.,  7.,  8.,  9., 10.],
             [11., 12., 13., 14., 15.]])
    • lat
      (x, y)
      float64
      15.0 16.07 17.14 ... 28.93 30.0
      array([[15.        , 16.07142857, 17.14285714, 18.21428571, 19.28571429],
             [20.35714286, 21.42857143, 22.5       , 23.57142857, 24.64285714],
             [25.71428571, 26.78571429, 27.85714286, 28.92857143, 30.        ]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 ... 2021-01-07
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000',
             '2021-01-03T00:00:00.000000000', '2021-01-04T00:00:00.000000000',
             '2021-01-05T00:00:00.000000000', '2021-01-06T00:00:00.000000000',
             '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
    • Humidity
      (x, y, time)
      float64
      1.093 -0.8485 ... -1.53 1.676
      array([[[ 1.09285914, -0.84854387,  0.1826349 , -1.11396086,
                0.78225378, -0.78785743, -0.87511885],
              [ 2.09665566,  0.15985731,  0.25153841,  0.74573533,
               -0.28411693, -0.23392604,  0.7636526 ],
              [ 0.96068395, -1.1125588 ,  0.46637362,  0.29439909,
                0.82885291, -0.04324119,  0.6410817 ],
              [-1.00679045,  0.28511252, -1.29079086,  0.82044935,
               -0.44715373, -1.61174393, -0.85558611],
              [-0.27158379,  0.76722785,  1.16378164, -0.52366829,
               -0.30989558,  0.83373781, -0.78712166]],
      
             [[-0.37605972, -0.54091566,  0.82583929, -0.71282054,
                0.88832528,  1.11421149,  0.02472263],
              [ 0.92056561, -0.32548426,  1.42181688,  0.47777962,
               -0.85564121, -0.38539221, -1.25830263],
              [ 0.57649183, -0.20337122,  1.36695728,  0.87814834,
                0.90984742,  0.55089382,  0.21001439],
              [ 0.5065872 ,  0.74714323,  1.69817763,  1.08498814,
                0.95241091,  1.30515827, -2.78357597],
              [-0.77912696,  0.11587679, -0.96905842, -1.06861872,
               -0.0615628 ,  1.24060027, -0.70328684]],
      
             [[ 0.76795915,  1.94095997, -1.19133151,  0.35401862,
                0.50684127, -0.66506239, -0.34307771],
              [ 1.40531069, -0.34715154, -0.50778276,  1.90611924,
                1.97774463, -0.33045863, -0.08441006],
              [-0.94689976,  0.46821417,  2.79138734,  0.97905794,
               -1.11122656,  0.95154709,  1.58695372],
              [-1.9940226 , -1.3042813 , -1.39580034, -0.44978336,
               -0.54981007, -0.78062656, -0.28168974],
              [-0.15129096,  0.94958385, -0.23475781,  1.62449484,
               -0.44738014, -1.52953396,  1.67588006]]])
    • WindSpeed
      (x, y, time)
      float64
      0.07895 -0.1061 ... 0.2155 -1.026
      array([[[ 0.07894539, -0.10608959, -0.15073608, -0.34567601,
                1.54519634,  0.38600075,  0.64598828],
              [-1.05765309, -1.71632891,  1.12384288,  0.60882024,
               -0.07093692,  0.72749619,  2.3643872 ],
              [ 0.46617626,  0.82395283, -2.52790159,  1.54609636,
               -0.44263358,  0.55701387, -0.75322983],
              [-1.2630226 , -0.06455496,  0.04505647, -0.0518783 ,
               -0.13284939, -0.21291942,  0.79013423],
              [-0.38895218,  1.51207925, -1.90532691,  1.07004393,
                0.297842  , -0.41960468, -2.02513535]],
      
             [[-1.20795325, -1.27012772,  1.3664836 ,  1.63163257,
               -0.36656294, -1.46336765, -0.82472209],
              [-0.43844783,  0.07389177,  0.58102792,  1.77656698,
                1.5170325 ,  1.25235069, -1.64758168],
              [-1.49854381,  0.83048688, -0.96246745,  0.23664752,
               -0.11588952,  1.63056501, -0.34286339],
              [ 0.49633755, -0.35311826,  0.74824454, -2.27923857,
                2.11301479,  1.10075378, -0.63347715],
              [ 0.88666826, -2.51101704, -0.31195891,  0.83481415,
               -1.54296428, -0.50920692, -1.02948406]],
      
             [[ 0.55171479,  0.51425579,  0.78569017,  0.30599779,
                0.18657313,  0.82820387, -0.26162739],
              [ 0.58689156, -0.42296983, -1.1292345 ,  1.94049605,
                1.36913959,  1.05035477, -0.26093031],
              [-0.4132902 , -0.88769071,  0.04975729,  0.2306687 ,
                0.85900227,  0.2660529 ,  0.20363354],
              [ 0.94501406,  0.22654847,  0.50482622,  0.41408703,
                0.34409454,  0.99606496,  1.6020004 ],
              [-1.08944353, -0.36530911, -1.33081251, -0.02537123,
               -1.99927196,  0.21548673, -1.02648887]]])
    • Precipitation
      (x, y, time)
      float64
      -0.5524 0.5605 ... -0.7978 -1.678
      array([[[-0.55236716,  0.56045232,  0.38064352, -1.93996829,
               -0.86920922, -1.18397041, -0.40251371],
              [-1.19625733, -0.55042156,  1.10772148, -0.93566846,
               -0.27924748,  0.47137331,  0.62897312],
              [ 0.20955818,  0.47893734,  1.11841985, -0.45516288,
                1.95094827, -0.61284131,  1.66222385],
              [ 0.70743435,  0.03414732, -1.98738552, -0.64425647,
                0.59936148, -0.69318562,  1.23651906],
              [ 0.13764513,  0.84321747,  0.34240088, -0.1092026 ,
               -0.80374936,  1.12265484, -0.17310687]],
      
             [[ 0.73722263, -0.28084061, -1.59074815,  0.16298992,
                0.78558901, -0.57622863, -0.14254492],
              [ 2.24012964,  2.15538612,  0.02794398,  0.55651171,
                0.58283111,  2.57319673,  0.43154201],
              [-0.96602978,  1.95313342,  1.56685205, -1.07134412,
                1.24054182, -0.99218704, -1.40214714],
              [-0.82823633, -1.87149092,  0.54425585,  1.03288874,
                0.87459299, -0.70930382, -0.14372735],
              [ 0.15809608,  1.20022505,  1.51685738,  1.18160504,
               -0.21131612,  0.02746981, -0.65240715]],
      
             [[-0.60228522,  1.76673585, -0.53051331,  0.34395563,
                0.28490643, -0.76413676, -2.59369344],
              [-0.7071827 , -0.16828121, -0.80118713, -1.43534924,
                0.46818736, -1.25485989,  0.14563744],
              [ 0.18051121,  1.64375119,  0.49077178, -2.69894487,
               -2.54625675,  1.15855515,  0.55661785],
              [ 0.28288257,  0.30063656,  1.32514957, -0.49511467,
               -2.22847894, -1.01659864,  0.62573462],
              [-0.10475908, -0.39669181,  1.73450886, -1.10069765,
                0.30144238, -0.79784192, -1.67833048]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record

drop_isel()

The drop_isel() method can be used to remove a subset of our Dataset object using integer indexing. It works exactly like isel() indexing method but it removes values that satisfy indexing details provided to it.

The method takes indexing details either as parameters of the method or as dictionary just-like isel() method.

Below we have dropped 0th value of dimension 'x', 0th & 1st value of dimension 'y' and 0th & 1st value of dimension 'time' of our dataset. This will then remove a subset of DataArray objects which satisfies these indexing details as well.

In [61]:
dataset6.drop_isel(x=[0,], y=[0,1], time=[0,1])
Out[61]:
<xarray.Dataset>
Dimensions:        (x: 2, y: 3, time: 5)
Coordinates:
    lon            (x, y) float64 8.0 9.0 10.0 13.0 14.0 15.0
    lat            (x, y) float64 22.5 23.57 24.64 27.86 28.93 30.0
  * time           (time) datetime64[ns] 2021-01-03 2021-01-04 ... 2021-01-07
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 89 95 47 15 38 72 40 ... 68 12 71 24 70 41
    Humidity       (x, y, time) float64 1.367 0.8781 0.9098 ... -1.53 1.676
    Pressure       (x, y, time) float64 1.467 1.152 0.4269 ... -0.9302 -0.5022
    WindSpeed      (x, y, time) float64 -0.9625 0.2366 -0.1159 ... 0.2155 -1.026
    Precipitation  (x, y, time) float64 1.567 -1.071 1.241 ... -0.7978 -1.678
    PM25           (x, y, time) float64 -0.167 0.6797 1.517 ... 0.01034 -0.07389
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 2
    • y: 3
    • time: 5
    • lon
      (x, y)
      float64
      8.0 9.0 10.0 13.0 14.0 15.0
      array([[ 8.,  9., 10.],
             [13., 14., 15.]])
    • lat
      (x, y)
      float64
      22.5 23.57 24.64 27.86 28.93 30.0
      array([[22.5       , 23.57142857, 24.64285714],
             [27.85714286, 28.92857143, 30.        ]])
    • time
      (time)
      datetime64[ns]
      2021-01-03 ... 2021-01-07
      array(['2021-01-03T00:00:00.000000000', '2021-01-04T00:00:00.000000000',
             '2021-01-05T00:00:00.000000000', '2021-01-06T00:00:00.000000000',
             '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      89 95 47 15 38 ... 12 71 24 70 41
      array([[[89, 95, 47, 15, 38],
              [72, 40, 16, 34,  6],
              [21, 99, 29, 99,  2]],
      
             [[87, 78,  2,  4, 88],
              [96, 32, 27, 15, 68],
              [12, 71, 24, 70, 41]]])
    • Humidity
      (x, y, time)
      float64
      1.367 0.8781 0.9098 ... -1.53 1.676
      array([[[ 1.36695728,  0.87814834,  0.90984742,  0.55089382,
                0.21001439],
              [ 1.69817763,  1.08498814,  0.95241091,  1.30515827,
               -2.78357597],
              [-0.96905842, -1.06861872, -0.0615628 ,  1.24060027,
               -0.70328684]],
      
             [[ 2.79138734,  0.97905794, -1.11122656,  0.95154709,
                1.58695372],
              [-1.39580034, -0.44978336, -0.54981007, -0.78062656,
               -0.28168974],
              [-0.23475781,  1.62449484, -0.44738014, -1.52953396,
                1.67588006]]])
    • Pressure
      (x, y, time)
      float64
      1.467 1.152 ... -0.9302 -0.5022
      array([[[ 1.46696652,  1.15184296,  0.42686459,  0.75804519,
                1.81746665],
              [ 0.36539539,  1.22952385,  0.18460093, -0.68319853,
                0.4164532 ],
              [ 1.07662905, -0.58588456, -1.70815234, -0.296188  ,
                1.61925102]],
      
             [[-0.55982602,  0.56826116, -1.36354003, -0.76608813,
                0.15614456],
              [ 1.46400708,  0.09948321,  0.03405099, -0.55550723,
               -0.05456826],
              [ 0.31132147,  0.069857  ,  0.47862362, -0.93024318,
               -0.50223001]]])
    • WindSpeed
      (x, y, time)
      float64
      -0.9625 0.2366 ... 0.2155 -1.026
      array([[[-0.96246745,  0.23664752, -0.11588952,  1.63056501,
               -0.34286339],
              [ 0.74824454, -2.27923857,  2.11301479,  1.10075378,
               -0.63347715],
              [-0.31195891,  0.83481415, -1.54296428, -0.50920692,
               -1.02948406]],
      
             [[ 0.04975729,  0.2306687 ,  0.85900227,  0.2660529 ,
                0.20363354],
              [ 0.50482622,  0.41408703,  0.34409454,  0.99606496,
                1.6020004 ],
              [-1.33081251, -0.02537123, -1.99927196,  0.21548673,
               -1.02648887]]])
    • Precipitation
      (x, y, time)
      float64
      1.567 -1.071 ... -0.7978 -1.678
      array([[[ 1.56685205, -1.07134412,  1.24054182, -0.99218704,
               -1.40214714],
              [ 0.54425585,  1.03288874,  0.87459299, -0.70930382,
               -0.14372735],
              [ 1.51685738,  1.18160504, -0.21131612,  0.02746981,
               -0.65240715]],
      
             [[ 0.49077178, -2.69894487, -2.54625675,  1.15855515,
                0.55661785],
              [ 1.32514957, -0.49511467, -2.22847894, -1.01659864,
                0.62573462],
              [ 1.73450886, -1.10069765,  0.30144238, -0.79784192,
               -1.67833048]]])
    • PM25
      (x, y, time)
      float64
      -0.167 0.6797 ... 0.01034 -0.07389
      array([[[-0.16698317,  0.6797431 ,  1.51676002, -1.03842146,
                1.00312552],
              [ 0.40363315,  0.60889672,  1.92265336,  0.04333985,
               -0.54798516],
              [ 0.41038088, -1.31391951,  0.85310146, -0.40344207,
                1.53879341]],
      
             [[ 0.65050061, -1.02004409,  2.34714662, -2.09622832,
               -0.60019026],
              [ 0.89115824,  0.195246  ,  0.17521524, -0.32149771,
               -0.62171875],
              [ 0.05961952, -1.6037228 , -0.76856085,  0.01034034,
               -0.07388771]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record

In the below cell, we have created an example which is a copy of our previous cell example with the only change that indexing details are provided as a dictionary.

In [62]:
dataset6.drop_isel({'x':[0,], 'y':[0,1], 'time':[0,1]})
Out[62]:
<xarray.Dataset>
Dimensions:        (x: 2, y: 3, time: 5)
Coordinates:
    lon            (x, y) float64 8.0 9.0 10.0 13.0 14.0 15.0
    lat            (x, y) float64 22.5 23.57 24.64 27.86 28.93 30.0
  * time           (time) datetime64[ns] 2021-01-03 2021-01-04 ... 2021-01-07
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 89 95 47 15 38 72 40 ... 68 12 71 24 70 41
    Humidity       (x, y, time) float64 1.367 0.8781 0.9098 ... -1.53 1.676
    Pressure       (x, y, time) float64 1.467 1.152 0.4269 ... -0.9302 -0.5022
    WindSpeed      (x, y, time) float64 -0.9625 0.2366 -0.1159 ... 0.2155 -1.026
    Precipitation  (x, y, time) float64 1.567 -1.071 1.241 ... -0.7978 -1.678
    PM25           (x, y, time) float64 -0.167 0.6797 1.517 ... 0.01034 -0.07389
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 2
    • y: 3
    • time: 5
    • lon
      (x, y)
      float64
      8.0 9.0 10.0 13.0 14.0 15.0
      array([[ 8.,  9., 10.],
             [13., 14., 15.]])
    • lat
      (x, y)
      float64
      22.5 23.57 24.64 27.86 28.93 30.0
      array([[22.5       , 23.57142857, 24.64285714],
             [27.85714286, 28.92857143, 30.        ]])
    • time
      (time)
      datetime64[ns]
      2021-01-03 ... 2021-01-07
      array(['2021-01-03T00:00:00.000000000', '2021-01-04T00:00:00.000000000',
             '2021-01-05T00:00:00.000000000', '2021-01-06T00:00:00.000000000',
             '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      89 95 47 15 38 ... 12 71 24 70 41
      array([[[89, 95, 47, 15, 38],
              [72, 40, 16, 34,  6],
              [21, 99, 29, 99,  2]],
      
             [[87, 78,  2,  4, 88],
              [96, 32, 27, 15, 68],
              [12, 71, 24, 70, 41]]])
    • Humidity
      (x, y, time)
      float64
      1.367 0.8781 0.9098 ... -1.53 1.676
      array([[[ 1.36695728,  0.87814834,  0.90984742,  0.55089382,
                0.21001439],
              [ 1.69817763,  1.08498814,  0.95241091,  1.30515827,
               -2.78357597],
              [-0.96905842, -1.06861872, -0.0615628 ,  1.24060027,
               -0.70328684]],
      
             [[ 2.79138734,  0.97905794, -1.11122656,  0.95154709,
                1.58695372],
              [-1.39580034, -0.44978336, -0.54981007, -0.78062656,
               -0.28168974],
              [-0.23475781,  1.62449484, -0.44738014, -1.52953396,
                1.67588006]]])
    • Pressure
      (x, y, time)
      float64
      1.467 1.152 ... -0.9302 -0.5022
      array([[[ 1.46696652,  1.15184296,  0.42686459,  0.75804519,
                1.81746665],
              [ 0.36539539,  1.22952385,  0.18460093, -0.68319853,
                0.4164532 ],
              [ 1.07662905, -0.58588456, -1.70815234, -0.296188  ,
                1.61925102]],
      
             [[-0.55982602,  0.56826116, -1.36354003, -0.76608813,
                0.15614456],
              [ 1.46400708,  0.09948321,  0.03405099, -0.55550723,
               -0.05456826],
              [ 0.31132147,  0.069857  ,  0.47862362, -0.93024318,
               -0.50223001]]])
    • WindSpeed
      (x, y, time)
      float64
      -0.9625 0.2366 ... 0.2155 -1.026
      array([[[-0.96246745,  0.23664752, -0.11588952,  1.63056501,
               -0.34286339],
              [ 0.74824454, -2.27923857,  2.11301479,  1.10075378,
               -0.63347715],
              [-0.31195891,  0.83481415, -1.54296428, -0.50920692,
               -1.02948406]],
      
             [[ 0.04975729,  0.2306687 ,  0.85900227,  0.2660529 ,
                0.20363354],
              [ 0.50482622,  0.41408703,  0.34409454,  0.99606496,
                1.6020004 ],
              [-1.33081251, -0.02537123, -1.99927196,  0.21548673,
               -1.02648887]]])
    • Precipitation
      (x, y, time)
      float64
      1.567 -1.071 ... -0.7978 -1.678
      array([[[ 1.56685205, -1.07134412,  1.24054182, -0.99218704,
               -1.40214714],
              [ 0.54425585,  1.03288874,  0.87459299, -0.70930382,
               -0.14372735],
              [ 1.51685738,  1.18160504, -0.21131612,  0.02746981,
               -0.65240715]],
      
             [[ 0.49077178, -2.69894487, -2.54625675,  1.15855515,
                0.55661785],
              [ 1.32514957, -0.49511467, -2.22847894, -1.01659864,
                0.62573462],
              [ 1.73450886, -1.10069765,  0.30144238, -0.79784192,
               -1.67833048]]])
    • PM25
      (x, y, time)
      float64
      -0.167 0.6797 ... 0.01034 -0.07389
      array([[[-0.16698317,  0.6797431 ,  1.51676002, -1.03842146,
                1.00312552],
              [ 0.40363315,  0.60889672,  1.92265336,  0.04333985,
               -0.54798516],
              [ 0.41038088, -1.31391951,  0.85310146, -0.40344207,
                1.53879341]],
      
             [[ 0.65050061, -1.02004409,  2.34714662, -2.09622832,
               -0.60019026],
              [ 0.89115824,  0.195246  ,  0.17521524, -0.32149771,
               -0.62171875],
              [ 0.05961952, -1.6037228 , -0.76856085,  0.01034034,
               -0.07388771]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record

drop_sel()

The drop_sel() method works exactly like drop_isel() method but it accepts indexing details where actual indexing values are provided instead of integer indexing. The actual indexing values can be of any data type like string, integer, float, datetime, etc.

The drop_sel() method is based on sel() indexing method but it removes entries which satisfies indexing details provided to it.

In [63]:
dataset3.drop_sel(x=["x1",], y=["y4","y5"])
Out[63]:
<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) <U2 'x2' 'x3'
  * y        (y) <U2 'y1' 'y2' 'y3'
Data variables:
    Array1   (x, y) int64 81 7 15 72 2 44
    Array2   (x, y) float64 -1.294 -1.039 1.744 1.069 0.8907 1.755
xarray.Dataset
    • x: 2
    • y: 3
    • x
      (x)
      <U2
      'x2' 'x3'
      array(['x2', 'x3'], dtype='<U2')
    • y
      (y)
      <U2
      'y1' 'y2' 'y3'
      array(['y1', 'y2', 'y3'], dtype='<U2')
    • Array1
      (x, y)
      int64
      81 7 15 72 2 44
      array([[81,  7, 15],
             [72,  2, 44]])
    • Array2
      (x, y)
      float64
      -1.294 -1.039 ... 0.8907 1.755
      array([[-1.29408532, -1.03878821,  1.74371223],
             [ 1.06931597,  0.89070639,  1.75488618]])

In the below cell, we have created another example demonstrating usage of drop_sel() method which explains how we can give indexing details as a dictionary. The indexing details are the same as our previous cell.

In [64]:
dataset3.drop_sel({'x':["x1",], 'y':["y4","y5"]})
Out[64]:
<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) <U2 'x2' 'x3'
  * y        (y) <U2 'y1' 'y2' 'y3'
Data variables:
    Array1   (x, y) int64 81 7 15 72 2 44
    Array2   (x, y) float64 -1.294 -1.039 1.744 1.069 0.8907 1.755
xarray.Dataset
    • x: 2
    • y: 3
    • x
      (x)
      <U2
      'x2' 'x3'
      array(['x2', 'x3'], dtype='<U2')
    • y
      (y)
      <U2
      'y1' 'y2' 'y3'
      array(['y1', 'y2', 'y3'], dtype='<U2')
    • Array1
      (x, y)
      int64
      81 7 15 72 2 44
      array([[81,  7, 15],
             [72,  2, 44]])
    • Array2
      (x, y)
      float64
      -1.294 -1.039 ... 0.8907 1.755
      array([[-1.29408532, -1.03878821,  1.74371223],
             [ 1.06931597,  0.89070639,  1.75488618]])

get(key)

The get() method can be used to access individual DataArray or coordinate by providing names for them.

Below we have retrieved DataArray object using get() method.

In [65]:
dataset2.get("Array1")
Out[65]:
<xarray.DataArray 'Array1' (x: 3, y: 5)>
array([[84, 79, 37, 97, 81],
       [69, 50, 56, 68,  3],
       [85, 40, 67, 85, 48]])
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
xarray.DataArray
'Array1'
  • x: 3
  • y: 5
  • 84 79 37 97 81 69 50 56 68 3 85 40 67 85 48
    array([[84, 79, 37, 97, 81],
           [69, 50, 56, 68,  3],
           [85, 40, 67, 85, 48]])
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])

In the below cell, we have retrieved coordinate values for coordinate 'lon' using get() method.

In [66]:
dataset6.get("lon")
Out[66]:
<xarray.DataArray 'lon' (x: 3, y: 5)>
array([[ 1.,  2.,  3.,  4.,  5.],
       [ 6.,  7.,  8.,  9., 10.],
       [11., 12., 13., 14., 15.]])
Coordinates:
    lon      (x, y) float64 1.0 2.0 3.0 4.0 5.0 6.0 ... 11.0 12.0 13.0 14.0 15.0
    lat      (x, y) float64 15.0 16.07 17.14 18.21 ... 26.79 27.86 28.93 30.0
Dimensions without coordinates: x, y
xarray.DataArray
'lon'
  • x: 3
  • y: 5
  • 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0
    array([[ 1.,  2.,  3.,  4.,  5.],
           [ 6.,  7.,  8.,  9., 10.],
           [11., 12., 13., 14., 15.]])
    • lon
      (x, y)
      float64
      1.0 2.0 3.0 4.0 ... 13.0 14.0 15.0
      array([[ 1.,  2.,  3.,  4.,  5.],
             [ 6.,  7.,  8.,  9., 10.],
             [11., 12., 13., 14., 15.]])
    • lat
      (x, y)
      float64
      15.0 16.07 17.14 ... 28.93 30.0
      array([[15.        , 16.07142857, 17.14285714, 18.21428571, 19.28571429],
             [20.35714286, 21.42857143, 22.5       , 23.57142857, 24.64285714],
             [25.71428571, 26.78571429, 27.85714286, 28.92857143, 30.        ]])

isnull()

The isnull() method checks values of each DataArray objects and returns True if value is NaN else False.

In [67]:
dataset2_copy = dataset2.copy().astype(np.float32)
dataset2_copy.Array1[0,0] = np.nan

dataset2_copy.isnull()
Out[67]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (x, y) bool True False False False ... False False False False
    Array2   (x, y) bool False False False False ... False False False False
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x, y)
      bool
      True False False ... False False
      array([[ True, False, False, False, False],
             [False, False, False, False, False],
             [False, False, False, False, False]])
    • Array2
      (x, y)
      bool
      False False False ... False False
      array([[False, False, False, False, False],
             [False, False, False, False, False],
             [False, False, False, False, False]])

isin(list_of_elements)

The isin() method takes an input array of elements. It then checks values of all DataArray objects and returns True/False based on the presence/absence of values in the given input array.

In [68]:
dataset2.isin([37,14,33, 84, 0.3861864])
Out[68]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (x, y) bool True False True False False ... False False False False
    Array2   (x, y) bool False False False False ... False False False False
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x, y)
      bool
      True False True ... False False
      array([[ True, False,  True, False, False],
             [False, False, False, False, False],
             [False, False, False, False, False]])
    • Array2
      (x, y)
      bool
      False False False ... False False
      array([[False, False, False, False, False],
             [False, False, False, False, False],
             [False, False, False, False, False]])

map(func)

The map() function takes as input another function which takes as input a single value and returns the single value after performing some operation on the input value. The map() function applies input function on each value of all DataArray objects. It works exactly like apply() function of pandas.

Below we have multiplied all values by 10 using map() function.

In [69]:
dataset2.map(lambda x : x*10)
Out[69]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (x, y) int64 840 790 370 970 810 690 500 ... 30 850 400 670 850 480
    Array2   (x, y) float64 10.04 3.862 7.374 14.91 ... -2.556 -27.99 -17.72
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x, y)
      int64
      840 790 370 970 ... 400 670 850 480
      array([[840, 790, 370, 970, 810],
             [690, 500, 560, 680,  30],
             [850, 400, 670, 850, 480]])
    • Array2
      (x, y)
      float64
      10.04 3.862 7.374 ... -27.99 -17.72
      array([[ 10.04053898,   3.86186399,   7.37368576,  14.90732028,
               -9.35833868],
             [ 11.75829045, -12.53880668,  -6.37751502,   9.07105196,
              -14.286807  ],
             [ -1.4006872 ,  -8.61754896,  -2.55619371, -27.98589105,
              -17.71533105]])

In the below cell, we have again called map() method to multiply all values by 10 but this time we have also asked it explicitly to keep all Dataset attributes.

In [70]:
dataset6.map(lambda x : x*10, keep_attrs=True)
Out[70]:
<xarray.Dataset>
Dimensions:        (x: 3, y: 5, time: 7)
Coordinates:
    lon            (x, y) float64 1.0 2.0 3.0 4.0 5.0 ... 12.0 13.0 14.0 15.0
    lat            (x, y) float64 15.0 16.07 17.14 18.21 ... 27.86 28.93 30.0
  * time           (time) datetime64[ns] 2021-01-01 2021-01-02 ... 2021-01-07
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 820 30 360 700 380 ... 120 710 240 700 410
    Humidity       (x, y, time) float64 10.93 -8.485 1.826 ... -15.3 16.76
    Pressure       (x, y, time) float64 11.76 -15.44 -6.974 ... -9.302 -5.022
    WindSpeed      (x, y, time) float64 0.7895 -1.061 -1.507 ... 2.155 -10.26
    Precipitation  (x, y, time) float64 -5.524 5.605 3.806 ... -7.978 -16.78
    PM25           (x, y, time) float64 11.49 -10.45 -0.8036 ... 0.1034 -0.7389
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 3
    • y: 5
    • time: 7
    • lon
      (x, y)
      float64
      1.0 2.0 3.0 4.0 ... 13.0 14.0 15.0
      array([[ 1.,  2.,  3.,  4.,  5.],
             [ 6.,  7.,  8.,  9., 10.],
             [11., 12., 13., 14., 15.]])
    • lat
      (x, y)
      float64
      15.0 16.07 17.14 ... 28.93 30.0
      array([[15.        , 16.07142857, 17.14285714, 18.21428571, 19.28571429],
             [20.35714286, 21.42857143, 22.5       , 23.57142857, 24.64285714],
             [25.71428571, 26.78571429, 27.85714286, 28.92857143, 30.        ]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 ... 2021-01-07
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000',
             '2021-01-03T00:00:00.000000000', '2021-01-04T00:00:00.000000000',
             '2021-01-05T00:00:00.000000000', '2021-01-06T00:00:00.000000000',
             '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      820 30 360 700 ... 710 240 700 410
      array([[[820,  30, 360, 700, 380, 650, 830],
              [380, 280, 470, 730, 470, 100, 880],
              [900, 720,  50, 130, 860, 340, 250],
              [230, 850,  30,  80, 460, 620, 390],
              [510, 220, 450, 750, 230, 490, 800]],
      
             [[510, 280, 990, 290, 590, 700, 780],
              [930, 280, 740, 470,  40, 880,  20],
              [640, 290, 890, 950, 470, 150, 380],
              [970, 490, 720, 400, 160, 340,  60],
              [870, 530, 210, 990, 290, 990,  20]],
      
             [[970, 260, 440, 960, 180, 980, 300],
              [230, 480, 550, 840, 230, 400, 640],
              [720, 140, 870, 780,  20,  40, 880],
              [ 90, 160, 960, 320, 270, 150, 680],
              [250, 150, 120, 710, 240, 700, 410]]])
    • Humidity
      (x, y, time)
      float64
      10.93 -8.485 1.826 ... -15.3 16.76
      array([[[ 10.92859137,  -8.48543871,   1.82634899, -11.13960857,
                 7.82253781,  -7.87857429,  -8.75118852],
              [ 20.96655662,   1.59857313,   2.51538406,   7.45735335,
                -2.8411693 ,  -2.33926038,   7.63652599],
              [  9.60683954, -11.12558804,   4.66373617,   2.94399087,
                 8.28852909,  -0.43241185,   6.41081697],
              [-10.06790446,   2.85112525, -12.90790862,   8.20449353,
                -4.47153732, -16.11743928,  -8.55586106],
              [ -2.71583793,   7.6722785 ,  11.63781642,  -5.2366829 ,
                -3.09895578,   8.3373781 ,  -7.87121659]],
      
             [[ -3.76059718,  -5.40915658,   8.25839293,  -7.12820539,
                 8.88325275,  11.14211489,   0.24722633],
              [  9.20565605,  -3.25484263,  14.21816881,   4.77779622,
                -8.55641213,  -3.85392208, -12.58302632],
              [  5.76491832,  -2.03371224,  13.6695728 ,   8.78148342,
                 9.09847425,   5.50893823,   2.10014393],
              [  5.06587196,   7.47143232,  16.98177627,  10.8498814 ,
                 9.5241091 ,  13.0515827 , -27.83575972],
              [ -7.79126965,   1.1587679 ,  -9.69058415, -10.68618722,
                -0.61562803,  12.4060027 ,  -7.03286836]],
      
             [[  7.67959147,  19.40959973, -11.91331507,   3.54018624,
                 5.0684127 ,  -6.65062387,  -3.4307771 ],
              [ 14.05310695,  -3.47151536,  -5.07782758,  19.06119242,
                19.77744632,  -3.30458628,  -0.84410064],
              [ -9.46899756,   4.68214166,  27.91387337,   9.79057937,
               -11.11226557,   9.51547085,  15.86953717],
              [-19.94022598, -13.04281302, -13.95800343,  -4.49783362,
                -5.49810073,  -7.80626559,  -2.81689736],
              [ -1.5129096 ,   9.49583846,  -2.34757813,  16.24494842,
                -4.47380139, -15.29533958,  16.75880056]]])
    • Pressure
      (x, y, time)
      float64
      11.76 -15.44 ... -9.302 -5.022
      array([[[ 11.76200222, -15.44313988,  -6.97401794,   8.97956954,
                13.42250839,  12.98649393,  -0.17706148],
              [  3.66745925,   6.70313001,   7.9318369 , -14.85573222,
                 2.48764171,  -0.28096831,   7.03256688],
              [  4.39664294, -10.25305535,  -1.03412018,   6.9840893 ,
                -6.88412728,   3.29417741,  13.8918332 ],
              [  7.71131724, -19.94685322,   5.16907183,  -8.26471742,
                -5.95095786,  -9.51007391,  12.13061407],
              [ -2.85540349,  -3.92557036, -11.55655299,   1.56777756,
                14.29456697, -13.93241579, -23.52282978]],
      
             [[ 16.37441186,  -3.25236696,  -7.35718204,   2.21999129,
                 1.88744359,  16.86388367,  13.80396185],
              [ 20.02229483,  -2.85229054,   4.51578152,  11.72479139,
               -12.21892941,  -3.97715018,   3.20213531],
              [ 14.66522238, -10.85740322,  14.66966521,  11.51842964,
                 4.26864594,   7.58045185,  18.17466648],
              [-22.84526324, -15.17290597,   3.65395394,  12.29523848,
                 1.84600933,  -6.83198527,   4.16453197],
              [-13.88479331,  -7.32825797,  10.76629047,  -5.85884559,
               -17.08152341,  -2.96188003,  16.19251021]],
      
             [[  2.51177596,  -2.22077213,   5.22969187, -20.74896492,
                11.71330825, -19.71761269,  -6.889194  ],
              [ 10.73806985,  14.43728282,  -4.14625805,   4.55390465,
                 1.86073576, -21.06625213,  -4.51008861],
              [ -6.99815058,  11.30175836,  -5.59826023,   5.68261163,
               -13.63540028,  -7.66088127,   1.56144557],
              [-12.68672449,  15.09677895,  14.64007078,   0.99483206,
                 0.34050989,  -5.55507232,  -0.54568262],
              [ -4.456235  ,  -3.32822132,   3.11321466,   0.69856997,
                 4.78623617,  -9.30243179,  -5.02230006]]])
    • WindSpeed
      (x, y, time)
      float64
      0.7895 -1.061 ... 2.155 -10.26
      array([[[  0.78945395,  -1.06089588,  -1.50736075,  -3.45676012,
                15.45196339,   3.86000753,   6.4598828 ],
              [-10.57653085, -17.16328914,  11.2384288 ,   6.08820239,
                -0.70936924,   7.27496192,  23.64387201],
              [  4.66176264,   8.2395283 , -25.27901588,  15.46096359,
                -4.42633583,   5.57013868,  -7.5322983 ],
              [-12.63022605,  -0.64554963,   0.45056473,  -0.518783  ,
                -1.32849393,  -2.1291942 ,   7.90134229],
              [ -3.88952175,  15.12079247, -19.05326911,  10.70043935,
                 2.97841995,  -4.1960468 , -20.25135349]],
      
             [[-12.07953249, -12.70127716,  13.66483605,  16.3163257 ,
                -3.66562941, -14.63367653,  -8.24722089],
              [ -4.38447834,   0.73891769,   5.81027923,  17.76566983,
                15.17032505,  12.5235069 , -16.4758168 ],
              [-14.9854381 ,   8.30486879,  -9.62467448,   2.36647515,
                -1.15889517,  16.30565007,  -3.42863386],
              [  4.96337547,  -3.53118261,   7.48244542, -22.79238573,
                21.13014787,  11.00753776,  -6.33477146],
              [  8.86668261, -25.11017044,  -3.11958908,   8.34814155,
               -15.42964282,  -5.09206916, -10.29484061]],
      
             [[  5.51714786,   5.14255794,   7.85690172,   3.05997785,
                 1.8657313 ,   8.28203872,  -2.61627388],
              [  5.86891564,  -4.22969829, -11.29234502,  19.40496045,
                13.69139586,  10.50354774,  -2.60930305],
              [ -4.13290197,  -8.87690711,   0.49757292,   2.30668702,
                 8.59002275,   2.660529  ,   2.0363354 ],
              [  9.4501406 ,   2.26548475,   5.04826221,   4.14087032,
                 3.44094539,   9.96064956,  16.02000397],
              [-10.89443531,  -3.65309112, -13.30812506,  -0.25371231,
               -19.99271956,   2.15486731, -10.26488872]]])
    • Precipitation
      (x, y, time)
      float64
      -5.524 5.605 ... -7.978 -16.78
      array([[[ -5.52367157,   5.60452324,   3.80643516, -19.39968291,
                -8.69209221, -11.8397041 ,  -4.02513715],
              [-11.9625733 ,  -5.50421557,  11.07721476,  -9.35668458,
                -2.79247482,   4.71373312,   6.28973117],
              [  2.09558178,   4.78937341,  11.18419853,  -4.55162878,
                19.50948275,  -6.12841305,  16.62223846],
              [  7.07434354,   0.3414732 , -19.87385519,  -6.44256466,
                 5.99361476,  -6.93185625,  12.36519063],
              [  1.37645127,   8.4321747 ,   3.42400876,  -1.09202595,
                -8.03749355,  11.2265484 ,  -1.73106866]],
      
             [[  7.37222635,  -2.8084061 , -15.90748151,   1.62989919,
                 7.85589008,  -5.76228627,  -1.42544919],
              [ 22.40129643,  21.55386121,   0.27943982,   5.56511706,
                 5.82831105,  25.73196728,   4.31542009],
              [ -9.6602978 ,  19.53133416,  15.6685205 , -10.71344122,
                12.40541815,  -9.92187038, -14.02147143],
              [ -8.28236327, -18.71490919,   5.44255852,  10.32888741,
                 8.74592994,  -7.09303816,  -1.4372735 ],
              [  1.58096077,  12.00225047,  15.16857376,  11.8160504 ,
                -2.11316123,   0.27469813,  -6.52407152]],
      
             [[ -6.0228522 ,  17.66735851,  -5.30513314,   3.43955625,
                 2.84906428,  -7.64136756, -25.9369344 ],
              [ -7.07182702,  -1.6828121 ,  -8.01187128, -14.35349244,
                 4.6818736 , -12.54859892,   1.45637443],
              [  1.80511209,  16.43751187,   4.90771781, -26.98944872,
               -25.46256748,  11.58555147,   5.56617854],
              [  2.82882568,   3.0063656 ,  13.25149567,  -4.95114673,
               -22.2847894 , -10.16598636,   6.25734618],
              [ -1.04759078,  -3.96691812,  17.34508862, -11.00697652,
                 3.01442377,  -7.97841924, -16.7833048 ]]])
    • PM25
      (x, y, time)
      float64
      11.49 -10.45 ... 0.1034 -0.7389
      array([[[ 1.14942133e+01, -1.04513297e+01, -8.03607617e-01,
                1.99354580e+01,  1.09731930e+01,  1.38722040e+00,
               -8.35446640e+00],
              [ 8.26586757e+00, -1.65826306e-01, -1.67572929e+01,
               -1.49658499e+01, -6.55535560e+00, -1.13255119e+01,
               -6.62022616e+00],
              [-1.47701503e+01, -6.92415573e+00, -3.12028428e+01,
                1.00236630e+01, -1.02402615e+01, -3.17190482e+00,
               -2.99456106e+00],
              [ 1.42893050e+01, -1.21543089e+01,  1.02972802e+01,
                1.14535031e+01, -2.05797367e+00,  3.17312174e+00,
                2.72734691e+01],
              [ 2.40283034e+00,  2.70292152e+00, -1.09793778e+00,
               -5.64620231e+00,  6.91831717e-01,  1.05257571e+01,
               -8.52768464e+00]],
      
             [[ 1.70862604e+01,  8.32009334e+00, -5.17421515e+00,
                6.02894373e+00, -7.06527232e+00,  7.60821520e+00,
               -1.50305205e+00],
              [-8.37166196e+00,  2.12666983e+01, -3.25378307e+00,
      ...
               -5.47985161e+00],
              [-9.49655124e+00, -8.63043266e+00,  4.10380876e+00,
               -1.31391951e+01,  8.53101463e+00, -4.03442068e+00,
                1.53879341e+01]],
      
             [[ 1.18998007e+00,  1.35084642e+01,  1.94379822e+01,
               -3.40083777e+00,  1.31582655e-02, -1.02573574e+01,
                1.01054907e+01],
              [ 5.70838063e+00, -3.62113981e+00, -2.43829109e+01,
               -3.32860193e+00,  3.08150115e+00, -6.66209068e+00,
                1.43865551e+01],
              [-1.82446794e+01,  5.68793112e+00,  6.50500612e+00,
               -1.02004409e+01,  2.34714662e+01, -2.09622832e+01,
               -6.00190258e+00],
              [-7.54482730e+00, -6.82451422e+00,  8.91158242e+00,
                1.95245997e+00,  1.75215239e+00, -3.21497707e+00,
               -6.21718753e+00],
              [-1.46312025e+01,  1.04896631e+00,  5.96195221e-01,
               -1.60372280e+01, -7.68560851e+00,  1.03403426e-01,
               -7.38877135e-01]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record

apply(func)

The apply() function is simply copy of map() function and works exactly same.

In [71]:
dataset2.apply(lambda x : x * 10)
Out[71]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (x, y) int64 840 790 370 970 810 690 500 ... 30 850 400 670 850 480
    Array2   (x, y) float64 10.04 3.862 7.374 14.91 ... -2.556 -27.99 -17.72
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (x, y)
      int64
      840 790 370 970 ... 400 670 850 480
      array([[840, 790, 370, 970, 810],
             [690, 500, 560, 680,  30],
             [850, 400, 670, 850, 480]])
    • Array2
      (x, y)
      float64
      10.04 3.862 7.374 ... -27.99 -17.72
      array([[ 10.04053898,   3.86186399,   7.37368576,  14.90732028,
               -9.35833868],
             [ 11.75829045, -12.53880668,  -6.37751502,   9.07105196,
              -14.286807  ],
             [ -1.4006872 ,  -8.61754896,  -2.55619371, -27.98589105,
              -17.71533105]])
In [72]:
dataset6.apply(lambda x : x*10, keep_attrs=True)
Out[72]:
<xarray.Dataset>
Dimensions:        (x: 3, y: 5, time: 7)
Coordinates:
    lon            (x, y) float64 1.0 2.0 3.0 4.0 5.0 ... 12.0 13.0 14.0 15.0
    lat            (x, y) float64 15.0 16.07 17.14 18.21 ... 27.86 28.93 30.0
  * time           (time) datetime64[ns] 2021-01-01 2021-01-02 ... 2021-01-07
Dimensions without coordinates: x, y
Data variables:
    Temperature    (x, y, time) int64 820 30 360 700 380 ... 120 710 240 700 410
    Humidity       (x, y, time) float64 10.93 -8.485 1.826 ... -15.3 16.76
    Pressure       (x, y, time) float64 11.76 -15.44 -6.974 ... -9.302 -5.022
    WindSpeed      (x, y, time) float64 0.7895 -1.061 -1.507 ... 2.155 -10.26
    Precipitation  (x, y, time) float64 -5.524 5.605 3.806 ... -7.978 -16.78
    PM25           (x, y, time) float64 11.49 -10.45 -0.8036 ... 0.1034 -0.7389
Attributes:
    Summary:  Dataset holds information like temperature, humidity. pressure,...
    lon:      Longitude
    lat:      Latitude
    time:     Date of Record
xarray.Dataset
    • x: 3
    • y: 5
    • time: 7
    • lon
      (x, y)
      float64
      1.0 2.0 3.0 4.0 ... 13.0 14.0 15.0
      array([[ 1.,  2.,  3.,  4.,  5.],
             [ 6.,  7.,  8.,  9., 10.],
             [11., 12., 13., 14., 15.]])
    • lat
      (x, y)
      float64
      15.0 16.07 17.14 ... 28.93 30.0
      array([[15.        , 16.07142857, 17.14285714, 18.21428571, 19.28571429],
             [20.35714286, 21.42857143, 22.5       , 23.57142857, 24.64285714],
             [25.71428571, 26.78571429, 27.85714286, 28.92857143, 30.        ]])
    • time
      (time)
      datetime64[ns]
      2021-01-01 ... 2021-01-07
      array(['2021-01-01T00:00:00.000000000', '2021-01-02T00:00:00.000000000',
             '2021-01-03T00:00:00.000000000', '2021-01-04T00:00:00.000000000',
             '2021-01-05T00:00:00.000000000', '2021-01-06T00:00:00.000000000',
             '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
    • Temperature
      (x, y, time)
      int64
      820 30 360 700 ... 710 240 700 410
      array([[[820,  30, 360, 700, 380, 650, 830],
              [380, 280, 470, 730, 470, 100, 880],
              [900, 720,  50, 130, 860, 340, 250],
              [230, 850,  30,  80, 460, 620, 390],
              [510, 220, 450, 750, 230, 490, 800]],
      
             [[510, 280, 990, 290, 590, 700, 780],
              [930, 280, 740, 470,  40, 880,  20],
              [640, 290, 890, 950, 470, 150, 380],
              [970, 490, 720, 400, 160, 340,  60],
              [870, 530, 210, 990, 290, 990,  20]],
      
             [[970, 260, 440, 960, 180, 980, 300],
              [230, 480, 550, 840, 230, 400, 640],
              [720, 140, 870, 780,  20,  40, 880],
              [ 90, 160, 960, 320, 270, 150, 680],
              [250, 150, 120, 710, 240, 700, 410]]])
    • Humidity
      (x, y, time)
      float64
      10.93 -8.485 1.826 ... -15.3 16.76
      array([[[ 10.92859137,  -8.48543871,   1.82634899, -11.13960857,
                 7.82253781,  -7.87857429,  -8.75118852],
              [ 20.96655662,   1.59857313,   2.51538406,   7.45735335,
                -2.8411693 ,  -2.33926038,   7.63652599],
              [  9.60683954, -11.12558804,   4.66373617,   2.94399087,
                 8.28852909,  -0.43241185,   6.41081697],
              [-10.06790446,   2.85112525, -12.90790862,   8.20449353,
                -4.47153732, -16.11743928,  -8.55586106],
              [ -2.71583793,   7.6722785 ,  11.63781642,  -5.2366829 ,
                -3.09895578,   8.3373781 ,  -7.87121659]],
      
             [[ -3.76059718,  -5.40915658,   8.25839293,  -7.12820539,
                 8.88325275,  11.14211489,   0.24722633],
              [  9.20565605,  -3.25484263,  14.21816881,   4.77779622,
                -8.55641213,  -3.85392208, -12.58302632],
              [  5.76491832,  -2.03371224,  13.6695728 ,   8.78148342,
                 9.09847425,   5.50893823,   2.10014393],
              [  5.06587196,   7.47143232,  16.98177627,  10.8498814 ,
                 9.5241091 ,  13.0515827 , -27.83575972],
              [ -7.79126965,   1.1587679 ,  -9.69058415, -10.68618722,
                -0.61562803,  12.4060027 ,  -7.03286836]],
      
             [[  7.67959147,  19.40959973, -11.91331507,   3.54018624,
                 5.0684127 ,  -6.65062387,  -3.4307771 ],
              [ 14.05310695,  -3.47151536,  -5.07782758,  19.06119242,
                19.77744632,  -3.30458628,  -0.84410064],
              [ -9.46899756,   4.68214166,  27.91387337,   9.79057937,
               -11.11226557,   9.51547085,  15.86953717],
              [-19.94022598, -13.04281302, -13.95800343,  -4.49783362,
                -5.49810073,  -7.80626559,  -2.81689736],
              [ -1.5129096 ,   9.49583846,  -2.34757813,  16.24494842,
                -4.47380139, -15.29533958,  16.75880056]]])
    • Pressure
      (x, y, time)
      float64
      11.76 -15.44 ... -9.302 -5.022
      array([[[ 11.76200222, -15.44313988,  -6.97401794,   8.97956954,
                13.42250839,  12.98649393,  -0.17706148],
              [  3.66745925,   6.70313001,   7.9318369 , -14.85573222,
                 2.48764171,  -0.28096831,   7.03256688],
              [  4.39664294, -10.25305535,  -1.03412018,   6.9840893 ,
                -6.88412728,   3.29417741,  13.8918332 ],
              [  7.71131724, -19.94685322,   5.16907183,  -8.26471742,
                -5.95095786,  -9.51007391,  12.13061407],
              [ -2.85540349,  -3.92557036, -11.55655299,   1.56777756,
                14.29456697, -13.93241579, -23.52282978]],
      
             [[ 16.37441186,  -3.25236696,  -7.35718204,   2.21999129,
                 1.88744359,  16.86388367,  13.80396185],
              [ 20.02229483,  -2.85229054,   4.51578152,  11.72479139,
               -12.21892941,  -3.97715018,   3.20213531],
              [ 14.66522238, -10.85740322,  14.66966521,  11.51842964,
                 4.26864594,   7.58045185,  18.17466648],
              [-22.84526324, -15.17290597,   3.65395394,  12.29523848,
                 1.84600933,  -6.83198527,   4.16453197],
              [-13.88479331,  -7.32825797,  10.76629047,  -5.85884559,
               -17.08152341,  -2.96188003,  16.19251021]],
      
             [[  2.51177596,  -2.22077213,   5.22969187, -20.74896492,
                11.71330825, -19.71761269,  -6.889194  ],
              [ 10.73806985,  14.43728282,  -4.14625805,   4.55390465,
                 1.86073576, -21.06625213,  -4.51008861],
              [ -6.99815058,  11.30175836,  -5.59826023,   5.68261163,
               -13.63540028,  -7.66088127,   1.56144557],
              [-12.68672449,  15.09677895,  14.64007078,   0.99483206,
                 0.34050989,  -5.55507232,  -0.54568262],
              [ -4.456235  ,  -3.32822132,   3.11321466,   0.69856997,
                 4.78623617,  -9.30243179,  -5.02230006]]])
    • WindSpeed
      (x, y, time)
      float64
      0.7895 -1.061 ... 2.155 -10.26
      array([[[  0.78945395,  -1.06089588,  -1.50736075,  -3.45676012,
                15.45196339,   3.86000753,   6.4598828 ],
              [-10.57653085, -17.16328914,  11.2384288 ,   6.08820239,
                -0.70936924,   7.27496192,  23.64387201],
              [  4.66176264,   8.2395283 , -25.27901588,  15.46096359,
                -4.42633583,   5.57013868,  -7.5322983 ],
              [-12.63022605,  -0.64554963,   0.45056473,  -0.518783  ,
                -1.32849393,  -2.1291942 ,   7.90134229],
              [ -3.88952175,  15.12079247, -19.05326911,  10.70043935,
                 2.97841995,  -4.1960468 , -20.25135349]],
      
             [[-12.07953249, -12.70127716,  13.66483605,  16.3163257 ,
                -3.66562941, -14.63367653,  -8.24722089],
              [ -4.38447834,   0.73891769,   5.81027923,  17.76566983,
                15.17032505,  12.5235069 , -16.4758168 ],
              [-14.9854381 ,   8.30486879,  -9.62467448,   2.36647515,
                -1.15889517,  16.30565007,  -3.42863386],
              [  4.96337547,  -3.53118261,   7.48244542, -22.79238573,
                21.13014787,  11.00753776,  -6.33477146],
              [  8.86668261, -25.11017044,  -3.11958908,   8.34814155,
               -15.42964282,  -5.09206916, -10.29484061]],
      
             [[  5.51714786,   5.14255794,   7.85690172,   3.05997785,
                 1.8657313 ,   8.28203872,  -2.61627388],
              [  5.86891564,  -4.22969829, -11.29234502,  19.40496045,
                13.69139586,  10.50354774,  -2.60930305],
              [ -4.13290197,  -8.87690711,   0.49757292,   2.30668702,
                 8.59002275,   2.660529  ,   2.0363354 ],
              [  9.4501406 ,   2.26548475,   5.04826221,   4.14087032,
                 3.44094539,   9.96064956,  16.02000397],
              [-10.89443531,  -3.65309112, -13.30812506,  -0.25371231,
               -19.99271956,   2.15486731, -10.26488872]]])
    • Precipitation
      (x, y, time)
      float64
      -5.524 5.605 ... -7.978 -16.78
      array([[[ -5.52367157,   5.60452324,   3.80643516, -19.39968291,
                -8.69209221, -11.8397041 ,  -4.02513715],
              [-11.9625733 ,  -5.50421557,  11.07721476,  -9.35668458,
                -2.79247482,   4.71373312,   6.28973117],
              [  2.09558178,   4.78937341,  11.18419853,  -4.55162878,
                19.50948275,  -6.12841305,  16.62223846],
              [  7.07434354,   0.3414732 , -19.87385519,  -6.44256466,
                 5.99361476,  -6.93185625,  12.36519063],
              [  1.37645127,   8.4321747 ,   3.42400876,  -1.09202595,
                -8.03749355,  11.2265484 ,  -1.73106866]],
      
             [[  7.37222635,  -2.8084061 , -15.90748151,   1.62989919,
                 7.85589008,  -5.76228627,  -1.42544919],
              [ 22.40129643,  21.55386121,   0.27943982,   5.56511706,
                 5.82831105,  25.73196728,   4.31542009],
              [ -9.6602978 ,  19.53133416,  15.6685205 , -10.71344122,
                12.40541815,  -9.92187038, -14.02147143],
              [ -8.28236327, -18.71490919,   5.44255852,  10.32888741,
                 8.74592994,  -7.09303816,  -1.4372735 ],
              [  1.58096077,  12.00225047,  15.16857376,  11.8160504 ,
                -2.11316123,   0.27469813,  -6.52407152]],
      
             [[ -6.0228522 ,  17.66735851,  -5.30513314,   3.43955625,
                 2.84906428,  -7.64136756, -25.9369344 ],
              [ -7.07182702,  -1.6828121 ,  -8.01187128, -14.35349244,
                 4.6818736 , -12.54859892,   1.45637443],
              [  1.80511209,  16.43751187,   4.90771781, -26.98944872,
               -25.46256748,  11.58555147,   5.56617854],
              [  2.82882568,   3.0063656 ,  13.25149567,  -4.95114673,
               -22.2847894 , -10.16598636,   6.25734618],
              [ -1.04759078,  -3.96691812,  17.34508862, -11.00697652,
                 3.01442377,  -7.97841924, -16.7833048 ]]])
    • PM25
      (x, y, time)
      float64
      11.49 -10.45 ... 0.1034 -0.7389
      array([[[ 1.14942133e+01, -1.04513297e+01, -8.03607617e-01,
                1.99354580e+01,  1.09731930e+01,  1.38722040e+00,
               -8.35446640e+00],
              [ 8.26586757e+00, -1.65826306e-01, -1.67572929e+01,
               -1.49658499e+01, -6.55535560e+00, -1.13255119e+01,
               -6.62022616e+00],
              [-1.47701503e+01, -6.92415573e+00, -3.12028428e+01,
                1.00236630e+01, -1.02402615e+01, -3.17190482e+00,
               -2.99456106e+00],
              [ 1.42893050e+01, -1.21543089e+01,  1.02972802e+01,
                1.14535031e+01, -2.05797367e+00,  3.17312174e+00,
                2.72734691e+01],
              [ 2.40283034e+00,  2.70292152e+00, -1.09793778e+00,
               -5.64620231e+00,  6.91831717e-01,  1.05257571e+01,
               -8.52768464e+00]],
      
             [[ 1.70862604e+01,  8.32009334e+00, -5.17421515e+00,
                6.02894373e+00, -7.06527232e+00,  7.60821520e+00,
               -1.50305205e+00],
              [-8.37166196e+00,  2.12666983e+01, -3.25378307e+00,
      ...
               -5.47985161e+00],
              [-9.49655124e+00, -8.63043266e+00,  4.10380876e+00,
               -1.31391951e+01,  8.53101463e+00, -4.03442068e+00,
                1.53879341e+01]],
      
             [[ 1.18998007e+00,  1.35084642e+01,  1.94379822e+01,
               -3.40083777e+00,  1.31582655e-02, -1.02573574e+01,
                1.01054907e+01],
              [ 5.70838063e+00, -3.62113981e+00, -2.43829109e+01,
               -3.32860193e+00,  3.08150115e+00, -6.66209068e+00,
                1.43865551e+01],
              [-1.82446794e+01,  5.68793112e+00,  6.50500612e+00,
               -1.02004409e+01,  2.34714662e+01, -2.09622832e+01,
               -6.00190258e+00],
              [-7.54482730e+00, -6.82451422e+00,  8.91158242e+00,
                1.95245997e+00,  1.75215239e+00, -3.21497707e+00,
               -6.21718753e+00],
              [-1.46312025e+01,  1.04896631e+00,  5.96195221e-01,
               -1.60372280e+01, -7.68560851e+00,  1.03403426e-01,
               -7.38877135e-01]]])
  • Summary :
    Dataset holds information like temperature, humidity. pressure, windspeed, precipitation and pm 2.5 particle presence based on location (lon, lat) and time.
    lon :
    Longitude
    lat :
    Latitude
    time :
    Date of Record

transpose()

We can retrieve transpose of our Dataset object using transpose() method. This will transpose all DataArray objects.

In [73]:
dataset2_transpose = dataset2.transpose()

dataset2_transpose
Out[73]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 5)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
Data variables:
    Array1   (y, x) int64 84 69 85 79 50 40 37 56 67 97 68 85 81 3 48
    Array2   (y, x) float64 1.004 1.176 -0.1401 0.3862 ... -0.9358 -1.429 -1.772
xarray.Dataset
    • x: 3
    • y: 5
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])
    • Array1
      (y, x)
      int64
      84 69 85 79 50 40 ... 68 85 81 3 48
      array([[84, 69, 85],
             [79, 50, 40],
             [37, 56, 67],
             [97, 68, 85],
             [81,  3, 48]])
    • Array2
      (y, x)
      float64
      1.004 1.176 ... -1.429 -1.772
      array([[ 1.0040539 ,  1.17582904, -0.14006872],
             [ 0.3861864 , -1.25388067, -0.8617549 ],
             [ 0.73736858, -0.6377515 , -0.25561937],
             [ 1.49073203,  0.9071052 , -2.79858911],
             [-0.93583387, -1.4286807 , -1.7715331 ]])
In [74]:
dataset2_transpose.Array1
Out[74]:
<xarray.DataArray 'Array1' (y: 5, x: 3)>
array([[84, 69, 85],
       [79, 50, 40],
       [37, 56, 67],
       [97, 68, 85],
       [81,  3, 48]])
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1 2 3 4
xarray.DataArray
'Array1'
  • y: 5
  • x: 3
  • 84 69 85 79 50 40 37 56 67 97 68 85 81 3 48
    array([[84, 69, 85],
           [79, 50, 40],
           [37, 56, 67],
           [97, 68, 85],
           [81,  3, 48]])
    • x
      (x)
      int64
      0 1 2
      array([0, 1, 2])
    • y
      (y)
      int64
      0 1 2 3 4
      array([0, 1, 2, 3, 4])

query()

The query() works exactly like query() method of pandas dataframe. It let us specify python expressions as input to the method to perform filtering on Dataset object. We need to provide expressions for each dimension separately.

If you want to know about how query() method works with pandas dataframe with examples then please feel free to check our tutorial on the same.

Below we have asked to keep only dimension values that are greater than 0.5 for dimension 'x' and values that are greater than 1.5 for dimension 'y' of our dataset. The returned Dataset object will have dimension values that satisfy input conditions.

In [75]:
dataset2.query(x="x > 0.5", y="y > 1.5")
Out[75]:
<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) int64 1 2
  * y        (y) int64 2 3 4
Data variables:
    Array1   (x, y) int64 56 68 3 67 85 48
    Array2   (x, y) float64 -0.6378 0.9071 -1.429 -0.2556 -2.799 -1.772
xarray.Dataset
    • x: 2
    • y: 3
    • x
      (x)
      int64
      1 2
      array([1, 2])
    • y
      (y)
      int64
      2 3 4
      array([2, 3, 4])
    • Array1
      (x, y)
      int64
      56 68 3 67 85 48
      array([[56, 68,  3],
             [67, 85, 48]])
    • Array2
      (x, y)
      float64
      -0.6378 0.9071 ... -2.799 -1.772
      array([[-0.6377515 ,  0.9071052 , -1.4286807 ],
             [-0.25561937, -2.79858911, -1.7715331 ]])

In the below cell, we have created another example explaining the usage of query() method on xarray Dataset object.

In [76]:
dataset3.query(x="x in ['x1','x2']", y="y in ['y4','y5']")
Out[76]: