Updated On : Dec-02,2021 Time Investment : ~30 mins

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.

import xarray as xr

print("Xarray Version : {}".format(xr.__version__))
Xarray Version : 0.20.1
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.

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.

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
<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.

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
<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.

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
<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.

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
<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.

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
<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)

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
<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.

xr.ones_like(dataset6)
<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.

xr.zeros_like(dataset6)
<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.

xr.full_like(dataset6, 101)
<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.

dataset6.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.attrs["Summary"]
'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.

dataset6.coords
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
dataset6.coords["lon"]
<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.

dataset6.data_vars
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.

pm25 = dataset6.data_vars["PM25"]

type(pm25)
xarray.core.dataarray.DataArray

We can access dimensions of Dataset using dims attribute.

dataset6.dims
Frozen({'x': 3, 'y': 5, 'time': 7})

The indexes attribute returns indices of different dimensions of Dataset.

dataset3.indexes
x: Index(['x1', 'x2', 'x3'], dtype='object', name='x')
y: Index(['y1', 'y2', 'y3', 'y4', 'y5'], dtype='object', name='y')
 dataset1.indexes
x: Int64Index([0, 1, 2, 3, 4], dtype='int64', name='x')
dataset6.indexes
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.

dataset6.nbytes
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.

dataset3["Array1"]
<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.

dataset3.Array1
<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.

dataset3.Array1[:2, :2]
<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.

dataset3.Array1.loc[["x1", "x2", "x3"]]
<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')
dataset3.Array1.loc[["x1", "x2", "x3"], ["y1", "y2"]]
<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.

x = dataset1.isel(x=0)

x
<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.

x = dataset1.isel({"x":0})

x
<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.

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

x
<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.

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

x
<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.

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

x
<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.

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

x
<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.

x.Array1
<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])
x.Array2
<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.

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

x
<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
x.Temperature
<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]')
x.PM25
<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.

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

x
<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.

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

x
<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.

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

x
<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.

x = dataset6.sel(x=0, y=0, time="2021-1-1")

x
<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
x.lon
<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]')
x.Precipitation
<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.

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

x
<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
x.lon
<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]])
x.Humidity
<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.

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

x
<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.

arr3  = np.random.randn(3,5)

dataset2.assign({"Array3": (["x","y"], arr3)})
<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.

dataset2.assign_attrs({"x": "Row-Dimension", "y": "Column-Dimension"})
<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.

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

dataset2_new_coords
<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 ]])
dataset2_new_coords.sel(x=["a1","a2"], y=["b1", "b2"])
<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).

dataset2_clipped = dataset2.clip(min=0.3, max=0.6)

dataset2_clipped
<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      ]])
dataset2_clipped.Array2
<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.

dataset2_copy = dataset2.copy()

dataset2_copy
<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.

dataset2_copy = dataset2.copy().astype(np.float32)

dataset2_copy
<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.

dataset2_copy.Array1[0,0] = np.nan

dataset2_copy.Array1
<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])
dataset2_copy = dataset2_copy.fillna(value=9999.)

dataset2_copy.Array1
<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.

dataset6.drop_vars(names=["Temperature", "Pressure", "PM25"])
<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.

dataset6.drop_isel(x=[0,], y=[0,1], time=[0,1])
<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.

dataset6.drop_isel({'x':[0,], 'y':[0,1], 'time':[0,1]})
<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.

dataset3.drop_sel(x=["x1",], y=["y4","y5"])
<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.

dataset3.drop_sel({'x':["x1",], 'y':["y4","y5"]})
<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.

dataset2.get("Array1")
<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.

dataset6.get("lon")
<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.

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

dataset2_copy.isnull()
<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.

dataset2.isin([37,14,33, 84, 0.3861864])
<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.

dataset2.map(lambda x : x*10)
<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.

dataset6.map(lambda x : x*10, keep_attrs=True)
<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.

dataset2.apply(lambda x : x * 10)
<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]])
dataset6.apply(lambda x : x*10, keep_attrs=True)
<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.

dataset2_transpose = dataset2.transpose()

dataset2_transpose
<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 ]])
dataset2_transpose.Array1
<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.

dataset2.query(x="x > 0.5", y="y > 1.5")
<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.

dataset3.query(x="x in ['x1','x2']", y="y in ['y4','y5']")
<xarray.Dataset>
Dimensions:  (x: 2, y: 2)
Coordinates:
  * x        (x) <U2 'x1' 'x2'
  * y        (y) <U2 'y4' 'y5'
Data variables:
    Array1   (x, y) int64 71 27 76 55
    Array2   (x, y) float64 0.9787 2.238 -0.7981 0.02968
xarray.Dataset
    • x: 2
    • y: 2
    • x
      (x)
      <U2
      'x1' 'x2'
      array(['x1', 'x2'], dtype='<U2')
    • y
      (y)
      <U2
      'y4' 'y5'
      array(['y4', 'y5'], dtype='<U2')
    • Array1
      (x, y)
      int64
      71 27 76 55
      array([[71, 27],
             [76, 55]])
    • Array2
      (x, y)
      float64
      0.9787 2.238 -0.7981 0.02968
      array([[ 0.97873601,  2.23814334],
             [-0.79806274,  0.02968323]])

info()

The info() function returns information about our Dataset object like data type, dimension details, etc.

dataset6.info()
xarray.Dataset {
dimensions:
	x = 3 ;
	y = 5 ;
	time = 7 ;

variables:
	int64 Temperature(x, y, time) ;
	float64 Humidity(x, y, time) ;
	float64 Pressure(x, y, time) ;
	float64 WindSpeed(x, y, time) ;
	float64 Precipitation(x, y, time) ;
	float64 PM25(x, y, time) ;
	float64 lon(x, y) ;
	float64 lat(x, y) ;
	datetime64[ns] time(time) ;

// global attributes:
	: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 ;
}
dataset3.info()
xarray.Dataset {
dimensions:
	x = 3 ;
	y = 5 ;

variables:
	int64 Array1(x, y) ;
	float64 Array2(x, y) ;
	<U2 x(x) ;
	<U2 y(y) ;

// global attributes:
}

5. Simple Statistics

In this section, we'll explain how we can perform simple statistics like min, max, mean, standard deviation, variance, rolling window functions, etc on Dataset object.

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

min(dim)

The min() method let us find minimum element for each DataArray objects of our Dataset object. It even let us provide dimension name if we want to retrieve minimum at a particular dimension of our Dataset.

dataset2.min()
<xarray.Dataset>
Dimensions:  ()
Data variables:
    Array1   int64 3
    Array2   float64 -2.799
xarray.Dataset
      • Array1
        ()
        int64
        3
        array(3)
      • Array2
        ()
        float64
        -2.799
        array(-2.79858911)
    dataset2.min(dim="x")
    
    <xarray.Dataset>
    Dimensions:  (y: 5)
    Coordinates:
      * y        (y) int64 0 1 2 3 4
    Data variables:
        Array1   (y) int64 69 40 37 68 3
        Array2   (y) float64 -0.1401 -1.254 -0.6378 -2.799 -1.772
    xarray.Dataset
      • y: 5
      • y
        (y)
        int64
        0 1 2 3 4
        array([0, 1, 2, 3, 4])
      • Array1
        (y)
        int64
        69 40 37 68 3
        array([69, 40, 37, 68,  3])
      • Array2
        (y)
        float64
        -0.1401 -1.254 ... -2.799 -1.772
        array([-0.14006872, -1.25388067, -0.6377515 , -2.79858911, -1.7715331 ])

    argmin(dim)

    The argmin() method let us find index of minimum element for each DataArray objects of our Dataset object. We can also provide dimension names to retrieve minimum element indices at a particular dimension.

    dataset2.argmin(dim="x")
    
    <xarray.Dataset>
    Dimensions:  (y: 5)
    Coordinates:
      * y        (y) int64 0 1 2 3 4
    Data variables:
        Array1   (y) int64 1 2 0 1 1
        Array2   (y) int64 2 1 1 2 2
    xarray.Dataset
      • y: 5
      • y
        (y)
        int64
        0 1 2 3 4
        array([0, 1, 2, 3, 4])
      • Array1
        (y)
        int64
        1 2 0 1 1
        array([1, 2, 0, 1, 1])
      • Array2
        (y)
        int64
        2 1 1 2 2
        array([2, 1, 1, 2, 2])

    max(dim)

    The max() method helps us find maximum element for each DataArray object of our Dataset object. We can also provide dimension names if we want maximum at a particular dimension.

    dataset3.max()
    
    <xarray.Dataset>
    Dimensions:  ()
    Data variables:
        Array1   int64 81
        Array2   float64 2.392
    xarray.Dataset
        • Array1
          ()
          int64
          81
          array(81)
        • Array2
          ()
          float64
          2.392
          array(2.39236527)
      dataset3.max(dim="y")
      
      <xarray.Dataset>
      Dimensions:  (x: 3)
      Coordinates:
        * x        (x) <U2 'x1' 'x2' 'x3'
      Data variables:
          Array1   (x) int64 71 81 72
          Array2   (x) float64 2.392 1.744 1.755
      xarray.Dataset
        • x: 3
        • x
          (x)
          <U2
          'x1' 'x2' 'x3'
          array(['x1', 'x2', 'x3'], dtype='<U2')
        • Array1
          (x)
          int64
          71 81 72
          array([71, 81, 72])
        • Array2
          (x)
          float64
          2.392 1.744 1.755
          array([2.39236527, 1.74371223, 1.75488618])
      dataset6.max(dim="time")
      
      <xarray.Dataset>
      Dimensions:        (x: 3, y: 5)
      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
      Dimensions without coordinates: x, y
      Data variables:
          Temperature    (x, y) int64 83 88 90 85 80 99 93 95 97 99 98 84 88 96 71
          Humidity       (x, y) float64 1.093 2.097 0.9607 ... 2.791 -0.2817 1.676
          Pressure       (x, y) float64 1.342 0.7932 1.389 1.213 ... 1.13 1.51 0.4786
          WindSpeed      (x, y) float64 1.545 2.364 1.546 ... 0.859 1.602 0.2155
          Precipitation  (x, y) float64 0.5605 1.108 1.951 1.237 ... 1.644 1.325 1.735
          PM25           (x, y) float64 1.994 0.8266 1.002 ... 2.347 0.8912 0.1049
      xarray.Dataset
        • x: 3
        • y: 5
        • 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.        ]])
        • Temperature
          (x, y)
          int64
          83 88 90 85 80 ... 98 84 88 96 71
          array([[83, 88, 90, 85, 80],
                 [99, 93, 95, 97, 99],
                 [98, 84, 88, 96, 71]])
        • Humidity
          (x, y)
          float64
          1.093 2.097 ... -0.2817 1.676
          array([[ 1.09285914,  2.09665566,  0.96068395,  0.82044935,  1.16378164],
                 [ 1.11421149,  1.42181688,  1.36695728,  1.69817763,  1.24060027],
                 [ 1.94095997,  1.97774463,  2.79138734, -0.28168974,  1.67588006]])
        • Pressure
          (x, y)
          float64
          1.342 0.7932 1.389 ... 1.51 0.4786
          array([[1.34225084, 0.79318369, 1.38918332, 1.21306141, 1.4294567 ],
                 [1.68638837, 2.00222948, 1.81746665, 1.22952385, 1.61925102],
                 [1.17133082, 1.44372828, 1.13017584, 1.50967789, 0.47862362]])
        • WindSpeed
          (x, y)
          float64
          1.545 2.364 1.546 ... 1.602 0.2155
          array([[1.54519634, 2.3643872 , 1.54609636, 0.79013423, 1.51207925],
                 [1.63163257, 1.77656698, 1.63056501, 2.11301479, 0.88666826],
                 [0.82820387, 1.94049605, 0.85900227, 1.6020004 , 0.21548673]])
        • Precipitation
          (x, y)
          float64
          0.5605 1.108 1.951 ... 1.325 1.735
          array([[0.56045232, 1.10772148, 1.95094827, 1.23651906, 1.12265484],
                 [0.78558901, 2.57319673, 1.95313342, 1.03288874, 1.51685738],
                 [1.76673585, 0.46818736, 1.64375119, 1.32514957, 1.73450886]])
        • PM25
          (x, y)
          float64
          1.994 0.8266 ... 0.8912 0.1049
          array([[1.9935458 , 0.82658676, 1.0023663 , 2.72734691, 1.05257571],
                 [1.70862604, 2.12666983, 1.51676002, 1.92265336, 1.53879341],
                 [1.94379822, 1.43865551, 2.34714662, 0.89115824, 0.10489663]])

      argmax(dim)

      The argmax() method return index of maximum elements for each DataArray objects of our Dataset object.

      dataset2.argmax(dim="x")
      
      <xarray.Dataset>
      Dimensions:  (y: 5)
      Coordinates:
        * y        (y) int64 0 1 2 3 4
      Data variables:
          Array1   (y) int64 2 0 2 0 0
          Array2   (y) int64 1 0 0 0 0
      xarray.Dataset
        • y: 5
        • y
          (y)
          int64
          0 1 2 3 4
          array([0, 1, 2, 3, 4])
        • Array1
          (y)
          int64
          2 0 2 0 0
          array([2, 0, 2, 0, 0])
        • Array2
          (y)
          int64
          1 0 0 0 0
          array([1, 0, 0, 0, 0])

      sum(dim)

      The sum() method returns sum of all elements for each DataArray objects of our Dataset object. We can provide dimension name as argument if we want sum at a particular dimension of our data.

      dataset2.sum()
      
      <xarray.Dataset>
      Dimensions:  ()
      Data variables:
          Array1   int64 949
          Array2   float64 -4.382
      xarray.Dataset
          • Array1
            ()
            int64
            949
            array(949)
          • Array2
            ()
            float64
            -4.382
            array(-4.38243679)
        dataset2.sum(dim="x")
        
        <xarray.Dataset>
        Dimensions:  (y: 5)
        Coordinates:
          * y        (y) int64 0 1 2 3 4
        Data variables:
            Array1   (y) int64 238 169 160 250 132
            Array2   (y) float64 2.04 -1.729 -0.156 -0.4008 -4.136
        xarray.Dataset
          • y: 5
          • y
            (y)
            int64
            0 1 2 3 4
            array([0, 1, 2, 3, 4])
          • Array1
            (y)
            int64
            238 169 160 250 132
            array([238, 169, 160, 250, 132])
          • Array2
            (y)
            float64
            2.04 -1.729 -0.156 -0.4008 -4.136
            array([ 2.03981422, -1.72944916, -0.1560023 , -0.40075188, -4.13604767])

        mean(dim)

        The mean() method returns average of all elements for each DataArray objects of our Dataset object. We can provide dimension names as an argument if we want the mean at a particular dimension of our data.

        dataset2.mean()
        
        <xarray.Dataset>
        Dimensions:  ()
        Data variables:
            Array1   float64 63.27
            Array2   float64 -0.2922
        xarray.Dataset
            • Array1
              ()
              float64
              63.27
              array(63.26666667)
            • Array2
              ()
              float64
              -0.2922
              array(-0.29216245)
          dataset2.mean(dim="y")
          
          <xarray.Dataset>
          Dimensions:  (x: 3)
          Coordinates:
            * x        (x) int64 0 1 2
          Data variables:
              Array1   (x) float64 75.6 49.2 65.0
              Array2   (x) float64 0.5365 -0.2475 -1.166
          xarray.Dataset
            • x: 3
            • x
              (x)
              int64
              0 1 2
              array([0, 1, 2])
            • Array1
              (x)
              float64
              75.6 49.2 65.0
              array([75.6, 49.2, 65. ])
            • Array2
              (x)
              float64
              0.5365 -0.2475 -1.166
              array([ 0.53650141, -0.24747573, -1.16551304])

          median(dim)

          The median() method returns median for each DataArray objects of our Dataset object. We can provide dimension names as an argument if we want a median at a particular dimension of our data.

          dataset2.median()
          
          <xarray.Dataset>
          Dimensions:  ()
          Data variables:
              Array1   float64 68.0
              Array2   float64 -0.2556
          xarray.Dataset
              • Array1
                ()
                float64
                68.0
                array(68.)
              • Array2
                ()
                float64
                -0.2556
                array(-0.25561937)
            dataset2.median(dim="x")
            
            <xarray.Dataset>
            Dimensions:  (y: 5)
            Coordinates:
              * y        (y) int64 0 1 2 3 4
            Data variables:
                Array1   (y) float64 84.0 50.0 56.0 85.0 48.0
                Array2   (y) float64 1.004 -0.8618 -0.2556 0.9071 -1.429
            xarray.Dataset
              • y: 5
              • y
                (y)
                int64
                0 1 2 3 4
                array([0, 1, 2, 3, 4])
              • Array1
                (y)
                float64
                84.0 50.0 56.0 85.0 48.0
                array([84., 50., 56., 85., 48.])
              • Array2
                (y)
                float64
                1.004 -0.8618 -0.2556 0.9071 -1.429
                array([ 1.0040539 , -0.8617549 , -0.25561937,  0.9071052 , -1.4286807 ])

            std(dim)

            The std() method returns standard deviation of all elements for each DataArray objects of our Dataset object. We can provide dimension names as arguments if we want standard deviation at a particular dimension of our data.

            dataset2.std()
            
            <xarray.Dataset>
            Dimensions:  ()
            Data variables:
                Array1   float64 23.76
                Array2   float64 1.198
            xarray.Dataset
                • Array1
                  ()
                  float64
                  23.76
                  array(23.76121957)
                • Array2
                  ()
                  float64
                  1.198
                  array(1.19839222)
              dataset2.std(dim="x")
              
              <xarray.Dataset>
              Dimensions:  (y: 5)
              Coordinates:
                * y        (y) int64 0 1 2 3 4
              Data variables:
                  Array1   (y) float64 7.318 16.54 12.39 11.9 31.97
                  Array2   (y) float64 0.5841 0.6993 0.5796 1.899 0.343
              xarray.Dataset
                • y: 5
                • y
                  (y)
                  int64
                  0 1 2 3 4
                  array([0, 1, 2, 3, 4])
                • Array1
                  (y)
                  float64
                  7.318 16.54 12.39 11.9 31.97
                  array([ 7.31816613, 16.53951497, 12.39175353, 11.8977122 , 31.96873473])
                • Array2
                  (y)
                  float64
                  0.5841 0.6993 0.5796 1.899 0.343
                  array([0.58405766, 0.69928059, 0.57955972, 1.8994463 , 0.34299967])

              var(dim)

              The var() method returns variance for each DataArray objects of our Dataset object. We can provide dimension names as arguments if we want variance at a particular dimension of our data.

              dataset2.var()
              
              <xarray.Dataset>
              Dimensions:  ()
              Data variables:
                  Array1   float64 564.6
                  Array2   float64 1.436
              xarray.Dataset
                  • Array1
                    ()
                    float64
                    564.6
                    array(564.59555556)
                  • Array2
                    ()
                    float64
                    1.436
                    array(1.43614391)
                dataset2.var(dim="x")
                
                <xarray.Dataset>
                Dimensions:  (y: 5)
                Coordinates:
                  * y        (y) int64 0 1 2 3 4
                Data variables:
                    Array1   (y) float64 53.56 273.6 153.6 141.6 1.022e+03
                    Array2   (y) float64 0.3411 0.489 0.3359 3.608 0.1176
                xarray.Dataset
                  • y: 5
                  • y
                    (y)
                    int64
                    0 1 2 3 4
                    array([0, 1, 2, 3, 4])
                  • Array1
                    (y)
                    float64
                    53.56 273.6 153.6 141.6 1.022e+03
                    array([  53.55555556,  273.55555556,  153.55555556,  141.55555556,
                           1022.        ])
                  • Array2
                    (y)
                    float64
                    0.3411 0.489 0.3359 3.608 0.1176
                    array([0.34112335, 0.48899334, 0.33588947, 3.60789626, 0.11764878])

                cumsum(dim)

                The cumsum() method returns cumulative sum at particular dimension for each DataArray objects of our Dataset object.

                dataset2.cumsum()
                
                <xarray.Dataset>
                Dimensions:  (x: 3, y: 5)
                Dimensions without coordinates: x, y
                Data variables:
                    Array1   (x, y) int64 84 163 200 297 378 153 282 ... 624 238 407 567 817 949
                    Array2   (x, y) float64 1.004 1.39 2.128 3.618 ... 0.1544 -0.2464 -4.382
                xarray.Dataset
                  • x: 3
                  • y: 5
                    • Array1
                      (x, y)
                      int64
                      84 163 200 297 ... 407 567 817 949
                      array([[ 84, 163, 200, 297, 378],
                             [153, 282, 375, 540, 624],
                             [238, 407, 567, 817, 949]])
                    • Array2
                      (x, y)
                      float64
                      1.004 1.39 2.128 ... -0.2464 -4.382
                      array([[ 1.0040539 ,  1.3902403 ,  2.12760887,  3.6183409 ,  2.68250703],
                             [ 2.17988294,  1.31218867,  1.41180575,  3.80964297,  1.4451284 ],
                             [ 2.03981422,  0.31036506,  0.15436276, -0.24638912, -4.38243679]])
                  dataset2.cumsum(dim="x")
                  
                  <xarray.Dataset>
                  Dimensions:  (x: 3, y: 5)
                  Coordinates:
                    * y        (y) int64 0 1 2 3 4
                  Dimensions without coordinates: x
                  Data variables:
                      Array1   (x, y) int64 84 79 37 97 81 153 129 93 165 84 238 169 160 250 132
                      Array2   (x, y) float64 1.004 0.3862 0.7374 1.491 ... -0.156 -0.4008 -4.136
                  xarray.Dataset
                    • x: 3
                    • y: 5
                    • y
                      (y)
                      int64
                      0 1 2 3 4
                      array([0, 1, 2, 3, 4])
                    • Array1
                      (x, y)
                      int64
                      84 79 37 97 81 ... 169 160 250 132
                      array([[ 84,  79,  37,  97,  81],
                             [153, 129,  93, 165,  84],
                             [238, 169, 160, 250, 132]])
                    • Array2
                      (x, y)
                      float64
                      1.004 0.3862 ... -0.4008 -4.136
                      array([[ 1.0040539 ,  0.3861864 ,  0.73736858,  1.49073203, -0.93583387],
                             [ 2.17988294, -0.86769427,  0.09961707,  2.39783722, -2.36451457],
                             [ 2.03981422, -1.72944916, -0.1560023 , -0.40075188, -4.13604767]])

                  cumprod(dim)

                  The cumprod() method returns cumulative product at particular dimension for each DataArray objects of our Dataset object.

                  dataset2.cumprod()
                  
                  <xarray.Dataset>
                  Dimensions:  (x: 3, y: 5)
                  Dimensions without coordinates: x, y
                  Data variables:
                      Array1   (x, y) int64 84 6636 ... 8015426386742269952 3834409401829130240
                      Array2   (x, y) float64 1.004 0.3878 0.2859 ... -0.008295 0.03139 -0.07435
                  xarray.Dataset
                    • x: 3
                    • y: 5
                      • Array1
                        (x, y)
                        int64
                        84 6636 ... 3834409401829130240
                        array([[                 84,                6636,              245532,
                                           23816604,          1929144924],
                               [               5796,            22894200,         47436782400,
                                    312893016710400,   76033003060627200],
                               [             492660,         77840280000,   10806099030720000,
                                8015426386742269952, 3834409401829130240]])
                      • Array2
                        (x, y)
                        float64
                        1.004 0.3878 ... 0.03139 -0.07435
                        array([[ 1.0040539 ,  0.38775196,  0.28591611,  0.4262243 , -0.39887514],
                               [ 1.18059574, -0.57168183,  0.26883791,  0.36353627,  0.48605082],
                               [-0.16536453, -0.0690048 , -0.00829486,  0.03139103, -0.07435134]])
                    dataset2.cumprod(dim="y")
                    
                    <xarray.Dataset>
                    Dimensions:  (x: 3, y: 5)
                    Coordinates:
                      * x        (x) int64 0 1 2
                    Dimensions without coordinates: y
                    Data variables:
                        Array1   (x, y) int64 84 6636 245532 23816604 ... 227800 19363000 929424000
                        Array2   (x, y) float64 1.004 0.3878 0.2859 ... -0.03085 0.08635 -0.153
                    xarray.Dataset
                      • x: 3
                      • y: 5
                      • x
                        (x)
                        int64
                        0 1 2
                        array([0, 1, 2])
                      • Array1
                        (x, y)
                        int64
                        84 6636 ... 19363000 929424000
                        array([[        84,       6636,     245532,   23816604, 1929144924],
                               [        69,       3450,     193200,   13137600,   39412800],
                               [        85,       3400,     227800,   19363000,  929424000]])
                      • Array2
                        (x, y)
                        float64
                        1.004 0.3878 ... 0.08635 -0.153
                        array([[ 1.0040539 ,  0.38775196,  0.28591611,  0.4262243 , -0.39887514],
                               [ 1.17582904, -1.47434931,  0.94026849,  0.85292243, -1.21855381],
                               [-0.14006872,  0.12070491, -0.03085451,  0.0863491 , -0.15297029]])

                    rolling()

                    The rolling() method lets us perform rolling window functions on each DataArray objects of our Dataset object. It accepts dimension names at which to apply rolling window and window size as input. We can provide dimension name and window size as a dictionary or as if they are parameters of the method. We can perform various aggregate functions (like min, max, mean, std, var, etc) on a rolling object returned by rolling() method.

                    Below we have taken the rolling window function on our 'y' dimension with a window size of 2. We have then calculated the mean aggregate function on samples of each window.

                    If you want to know how to perform moving window functions in pandas then please feel free to check our tutorial on the same where we cover the topic in detail.

                    rolling_mean = dataset2.rolling({"y": 2}).mean()
                    
                    rolling_mean
                    
                    <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 nan 81.5 58.0 67.0 89.0 ... nan 62.5 53.5 76.0 66.5
                        Array2   (x, y) float64 nan 0.6951 0.5618 1.114 ... -0.5587 -1.527 -2.285
                    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
                        nan 81.5 58.0 ... 53.5 76.0 66.5
                        array([[ nan, 81.5, 58. , 67. , 89. ],
                               [ nan, 59.5, 53. , 62. , 35.5],
                               [ nan, 62.5, 53.5, 76. , 66.5]])
                      • Array2
                        (x, y)
                        float64
                        nan 0.6951 0.5618 ... -1.527 -2.285
                        array([[        nan,  0.69512015,  0.56177749,  1.1140503 ,  0.27744908],
                               [        nan, -0.03902581, -0.94581609,  0.13467685, -0.26078775],
                               [        nan, -0.50091181, -0.55868713, -1.52710424, -2.2850611 ]])
                    rolling_mean.Array1
                    
                    <xarray.DataArray 'Array1' (x: 3, y: 5)>
                    array([[ nan, 81.5, 58. , 67. , 89. ],
                           [ nan, 59.5, 53. , 62. , 35.5],
                           [ nan, 62.5, 53.5, 76. , 66.5]])
                    Coordinates:
                      * x        (x) int64 0 1 2
                      * y        (y) int64 0 1 2 3 4
                    xarray.DataArray
                    'Array1'
                    • x: 3
                    • y: 5
                    • nan 81.5 58.0 67.0 89.0 nan 59.5 ... 62.0 35.5 nan 62.5 53.5 76.0 66.5
                      array([[ nan, 81.5, 58. , 67. , 89. ],
                             [ nan, 59.5, 53. , 62. , 35.5],
                             [ nan, 62.5, 53.5, 76. , 66.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])

                    Below we have created another example demonstrating usage of rolling() function. This time we have performed a rolling window on 'time' dimension with a window size of 5. We have then applied the mean aggregate function on samples of each window.

                    rolling_mean = dataset6.rolling({"time": 5}).mean()
                    
                    rolling_mean
                    
                    <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) float64 nan nan nan nan ... nan 29.4 38.4 43.6
                        Humidity       (x, y, time) float64 nan nan nan ... 0.3481 0.07248 0.2177
                        Pressure       (x, y, time) float64 nan nan nan ... 0.01627 -0.08065 -0.1145
                        WindSpeed      (x, y, time) float64 nan nan nan ... -0.962 -0.7011 -0.8333
                        Precipitation  (x, y, time) float64 nan nan nan ... 0.08676 -0.05186 -0.3082
                        PM25           (x, y, time) float64 nan nan nan ... -0.7342 -0.4395 -0.4752
                    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)
                        float64
                        nan nan nan nan ... 29.4 38.4 43.6
                        array([[[ nan,  nan,  nan,  nan, 45.8, 42.4, 58.4],
                                [ nan,  nan,  nan,  nan, 46.6, 41. , 53. ],
                                [ nan,  nan,  nan,  nan, 53.2, 42. , 32.6],
                                [ nan,  nan,  nan,  nan, 33. , 40.8, 31.6],
                                [ nan,  nan,  nan,  nan, 43.2, 42.8, 54.4]],
                        
                               [[ nan,  nan,  nan,  nan, 53.2, 57. , 67. ],
                                [ nan,  nan,  nan,  nan, 49.2, 48.2, 43. ],
                                [ nan,  nan,  nan,  nan, 64.8, 55. , 56.8],
                                [ nan,  nan,  nan,  nan, 54.8, 42.2, 33.6],
                                [ nan,  nan,  nan,  nan, 57.8, 60.2, 50. ]],
                        
                               [[ nan,  nan,  nan,  nan, 56.2, 56.4, 57.2],
                                [ nan,  nan,  nan,  nan, 46.6, 50. , 53.2],
                                [ nan,  nan,  nan,  nan, 50.6, 37. , 51.8],
                                [ nan,  nan,  nan,  nan, 36. , 37.2, 47.6],
                                [ nan,  nan,  nan,  nan, 29.4, 38.4, 43.6]]])
                      • Humidity
                        (x, y, time)
                        float64
                        nan nan nan ... 0.07248 0.2177
                        array([[[        nan,         nan,         nan,         nan,
                                  0.01904862, -0.3570947 , -0.36240969],
                                [        nan,         nan,         nan,         nan,
                                  0.59393396,  0.12781762,  0.24857667],
                                [        nan,         nan,         nan,         nan,
                                  0.28755015,  0.08676512,  0.43749322],
                                [        nan,         nan,         nan,         nan,
                                 -0.32783463, -0.44882533, -0.67696506],
                                [        nan,         nan,         nan,         nan,
                                  0.16517237,  0.38623669,  0.07536678]],
                        
                               [[        nan,         nan,         nan,         nan,
                                  0.01687373,  0.31492797,  0.42805563],
                                [        nan,         nan,         nan,         nan,
                                  0.32780733,  0.06661576, -0.11994791],
                                [        nan,         nan,         nan,         nan,
                                  0.70561473,  0.70049513,  0.78317225],
                                [        nan,         nan,         nan,         nan,
                                  0.99786142,  1.15757564,  0.4514318 ],
                                [        nan,         nan,         nan,         nan,
                                 -0.55249802, -0.14855258, -0.3123853 ]],
                        
                               [[        nan,         nan,         nan,         nan,
                                  0.4756895 ,  0.18908519, -0.26772234],
                                [        nan,         nan,         nan,         nan,
                                  0.88684805,  0.53969419,  0.59224248],
                                [        nan,         nan,         nan,         nan,
                                  0.43610663,  0.81579599,  1.0395439 ],
                                [        nan,         nan,         nan,         nan,
                                 -1.13873954, -0.89606033, -0.69154201],
                                [        nan,         nan,         nan,         nan,
                                  0.34812996,  0.07248136,  0.2177406 ]]])
                      • Pressure
                        (x, y, time)
                        float64
                        nan nan nan ... -0.08065 -0.1145
                        array([[[        nan,         nan,         nan,         nan,
                                  0.23493845,  0.25942828,  0.56474985],
                                [        nan,         nan,         nan,         nan,
                                  0.11868671,  0.03971816,  0.0463069 ],
                                [        nan,         nan,         nan,         nan,
                                 -0.13581141, -0.15786072,  0.32503705],
                                [        nan,         nan,         nan,         nan,
                                 -0.42564279, -0.77007061, -0.12852127],
                                [        nan,         nan,         nan,         nan,
                                 -0.04950365, -0.27104389, -0.66298908]],
                        
                               [[        nan,         nan,         nan,         nan,
                                  0.19744595,  0.20723539,  0.54836197],
                                [        nan,         nan,         nan,         nan,
                                  0.42383296, -0.05615594,  0.06493257],
                                [        nan,         nan,         nan,         nan,
                                  0.6852912 ,  0.54359579,  1.12423718],
                                [        nan,         nan,         nan,         nan,
                                 -0.40445935, -0.08419379,  0.30255497],
                                [        nan,         nan,         nan,         nan,
                                 -0.6677426 , -0.44928433,  0.02113103]],
                        
                               [[        nan,         nan,         nan,         nan,
                                 -0.07029922, -0.51488699, -0.60825543],
                                [        nan,         nan,         nan,         nan,
                                  0.5488747 , -0.08721174, -0.46615917],
                                [        nan,         nan,         nan,         nan,
                                 -0.18494882, -0.19820344, -0.39300969],
                                [        nan,         nan,         nan,         nan,
                                  0.36770934,  0.51034239,  0.19749316],
                                [        nan,         nan,         nan,         nan,
                                  0.01627129, -0.08065265, -0.11453422]]])
                      • WindSpeed
                        (x, y, time)
                        float64
                        nan nan nan ... -0.7011 -0.8333
                        array([[[        nan,         nan,         nan,         nan,
                                  0.20432801,  0.26573908,  0.41615466],
                                [        nan,         nan,         nan,         nan,
                                 -0.22245116,  0.13457869,  0.95072192],
                                [        nan,         nan,         nan,         nan,
                                 -0.02686194, -0.00869442, -0.32413095],
                                [        nan,         nan,         nan,         nan,
                                 -0.29344976, -0.08342912,  0.08750872],
                                [        nan,         nan,         nan,         nan,
                                  0.11713722,  0.11100672, -0.5964362 ]],
                        
                               [[        nan,         nan,         nan,         nan,
                                  0.03069445, -0.02038843,  0.0686927 ],
                                [        nan,         nan,         nan,         nan,
                                  0.70201427,  1.04017397,  0.69587928],
                                [        nan,         nan,         nan,         nan,
                                 -0.30195328,  0.32386849,  0.08919843],
                                [        nan,         nan,         nan,         nan,
                                  0.14504801,  0.26593125,  0.20985948],
                                [        nan,         nan,         nan,         nan,
                                 -0.52889156, -0.8080666 , -0.51176   ]],
                        
                               [[        nan,         nan,         nan,         nan,
                                  0.46884633,  0.52414415,  0.36896751],
                                [        nan,         nan,         nan,         nan,
                                  0.46886457,  0.56155721,  0.59396512],
                                [        nan,         nan,         nan,         nan,
                                 -0.03231053,  0.10355809,  0.32182294],
                                [        nan,         nan,         nan,         nan,
                                  0.48691407,  0.49712424,  0.77221463],
                                [        nan,         nan,         nan,         nan,
                                 -0.96204167, -0.70105561, -0.83329157]]])
                      • Precipitation
                        (x, y, time)
                        float64
                        nan nan nan ... -0.05186 -0.3082
                        array([[[        nan,         nan,         nan,         nan,
                                 -0.48408977, -0.61041042, -0.80300362],
                                [        nan,         nan,         nan,         nan,
                                 -0.37077467, -0.03724854,  0.19863039],
                                [        nan,         nan,         nan,         nan,
                                  0.66054015,  0.49606026,  0.73271756],
                                [        nan,         nan,         nan,         nan,
                                 -0.25813977, -0.53826376, -0.29778941],
                                [        nan,         nan,         nan,         nan,
                                  0.0820623 ,  0.27906425,  0.07579938]],
                        
                               [[        nan,         nan,         nan,         nan,
                                 -0.03715744, -0.29984769, -0.27218855],
                                [        nan,         nan,         nan,         nan,
                                  1.11256051,  1.17917393,  0.83440511],
                                [        nan,         nan,         nan,         nan,
                                  0.54463068,  0.53939922, -0.13165689],
                                [        nan,         nan,         nan,         nan,
                                 -0.04959793, -0.02581143,  0.31974128],
                                [        nan,         nan,         nan,         nan,
                                  0.76909348,  0.74296823,  0.37244179]],
                        
                               [[        nan,         nan,         nan,         nan,
                                  0.25255987,  0.22018957, -0.65189629],
                                [        nan,         nan,         nan,         nan,
                                 -0.52876258, -0.63829802, -0.57551429],
                                [        nan,         nan,         nan,         nan,
                                 -0.58603349, -0.3904247 , -0.60785137],
                                [        nan,         nan,         nan,         nan,
                                 -0.16298498, -0.42288122, -0.35786161],
                                [        nan,         nan,         nan,         nan,
                                  0.08676054, -0.05185603, -0.30818376]]])
                      • PM25
                        (x, y, time)
                        float64
                        nan nan nan ... -0.4395 -0.4752
                        array([[[        nan,         nan,         nan,         nan,
                                  0.62295854,  0.42081868,  0.46275595],
                                [        nan,         nan,         nan,         nan,
                                 -0.60356914, -0.99539673, -1.12448473],
                                [        nan,         nan,         nan,         nan,
                                 -1.06227495, -0.83031004, -0.75171815],
                                [        nan,         nan,         nan,         nan,
                                  0.43655611,  0.21423245,  1.00278801],
                                [        nan,         nan,         nan,         nan,
                                 -0.01893113,  0.14352741, -0.08108472]],
                        
                               [[        nan,         nan,         nan,         nan,
                                  0.3839162 ,  0.1943553 , -0.00210761],
                                [        nan,         nan,         nan,         nan,
                                  0.65638282,  1.17728902,  0.66886076],
                                [        nan,         nan,         nan,         nan,
                                  0.70008543,  0.39567524,  0.3988448 ],
                                [        nan,         nan,         nan,         nan,
                                  0.06322066,  0.43305012,  0.48610758],
                                [        nan,         nan,         nan,         nan,
                                 -0.37262711, -0.2633845 ,  0.21698284]],
                        
                               [[        nan,         nan,         nan,         nan,
                                  0.61497494,  0.38602819,  0.31796872],
                                [        nan,         nan,         nan,         nan,
                                 -0.45085542, -0.69826484, -0.33811095],
                                [        nan,         nan,         nan,         nan,
                                  0.14438566,  0.09003359, -0.14376309],
                                [        nan,         nan,         nan,         nan,
                                 -0.03506294,  0.05153407,  0.0636806 ],
                                [        nan,         nan,         nan,         nan,
                                 -0.73417755, -0.43948543, -0.4752423 ]]])
                    • 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
                    rolling_mean.Temperature
                    
                    <xarray.DataArray 'Temperature' (x: 3, y: 5, time: 7)>
                    array([[[ nan,  nan,  nan,  nan, 45.8, 42.4, 58.4],
                            [ nan,  nan,  nan,  nan, 46.6, 41. , 53. ],
                            [ nan,  nan,  nan,  nan, 53.2, 42. , 32.6],
                            [ nan,  nan,  nan,  nan, 33. , 40.8, 31.6],
                            [ nan,  nan,  nan,  nan, 43.2, 42.8, 54.4]],
                    
                           [[ nan,  nan,  nan,  nan, 53.2, 57. , 67. ],
                            [ nan,  nan,  nan,  nan, 49.2, 48.2, 43. ],
                            [ nan,  nan,  nan,  nan, 64.8, 55. , 56.8],
                            [ nan,  nan,  nan,  nan, 54.8, 42.2, 33.6],
                            [ nan,  nan,  nan,  nan, 57.8, 60.2, 50. ]],
                    
                           [[ nan,  nan,  nan,  nan, 56.2, 56.4, 57.2],
                            [ nan,  nan,  nan,  nan, 46.6, 50. , 53.2],
                            [ nan,  nan,  nan,  nan, 50.6, 37. , 51.8],
                            [ nan,  nan,  nan,  nan, 36. , 37.2, 47.6],
                            [ nan,  nan,  nan,  nan, 29.4, 38.4, 43.6]]])
                    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
                    Dimensions without coordinates: x, y
                    xarray.DataArray
                    'Temperature'
                    • x: 3
                    • y: 5
                    • time: 7
                    • nan nan nan nan 45.8 42.4 58.4 nan ... nan nan nan nan 29.4 38.4 43.6
                      array([[[ nan,  nan,  nan,  nan, 45.8, 42.4, 58.4],
                              [ nan,  nan,  nan,  nan, 46.6, 41. , 53. ],
                              [ nan,  nan,  nan,  nan, 53.2, 42. , 32.6],
                              [ nan,  nan,  nan,  nan, 33. , 40.8, 31.6],
                              [ nan,  nan,  nan,  nan, 43.2, 42.8, 54.4]],
                      
                             [[ nan,  nan,  nan,  nan, 53.2, 57. , 67. ],
                              [ nan,  nan,  nan,  nan, 49.2, 48.2, 43. ],
                              [ nan,  nan,  nan,  nan, 64.8, 55. , 56.8],
                              [ nan,  nan,  nan,  nan, 54.8, 42.2, 33.6],
                              [ nan,  nan,  nan,  nan, 57.8, 60.2, 50. ]],
                      
                             [[ nan,  nan,  nan,  nan, 56.2, 56.4, 57.2],
                              [ nan,  nan,  nan,  nan, 46.6, 50. , 53.2],
                              [ nan,  nan,  nan,  nan, 50.6, 37. , 51.8],
                              [ nan,  nan,  nan,  nan, 36. , 37.2, 47.6],
                              [ nan,  nan,  nan,  nan, 29.4, 38.4, 43.6]]])
                      • 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]')

                    resample()

                    The resample() function is useful when one of the dimensions of our Dataset object is datetime and we want to change the existing frequency of that dimension. The frequency can be changed in two ways using resample() function.

                    1. Up Sampling - We increase frequency from lower to higher. E.g - Daily frequency to monthly.
                    2. Down Sampling - We decrease frequency. E.g - Daily to 6 hourly.

                    The resample() function takes as input dimension name and new frequency as input. We can provide dimension name and frequency as a dictionary or as if they are parameters of the resample() method.

                    If you are interested in learning about resampling using pandas then please feel free to check our tutorial where we discuss resampling in detail.

                    Up Sampling

                    Below we have upsampled our Dataset from daily frequency to 2-day frequency for 'time' dimension. After upsampling, we have called mean() function on upsampled Dataset to replace values with an average of values.

                    two_day_sampled = dataset6.resample({"time": "2D"})
                    
                    two_day_sampled
                    
                    DatasetResample, grouped over '__resample_dim__'
                    4 groups with labels 2021-01-01, ..., 2021-01-07.
                    for dt, dset in two_day_sampled:
                        print(dt, dset.dims)
                    
                    2021-01-01T00:00:00.000000000 Frozen({'x': 3, 'y': 5, 'time': 2})
                    2021-01-03T00:00:00.000000000 Frozen({'x': 3, 'y': 5, 'time': 2})
                    2021-01-05T00:00:00.000000000 Frozen({'x': 3, 'y': 5, 'time': 2})
                    2021-01-07T00:00:00.000000000 Frozen({'x': 3, 'y': 5, 'time': 1})
                    
                    two_day_sampled_mean = two_day_sampled.mean()
                    
                    two_day_sampled_mean
                    
                    <xarray.Dataset>
                    Dimensions:        (time: 4, x: 3, y: 5)
                    Coordinates:
                      * time           (time) datetime64[ns] 2021-01-01 2021-01-03 ... 2021-01-07
                        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
                    Dimensions without coordinates: x, y
                    Data variables:
                        Temperature    (time, x, y) float64 42.5 33.0 81.0 54.0 ... 88.0 68.0 41.0
                        Humidity       (time, x, y) float64 0.1222 1.128 -0.07594 ... -0.2817 1.676
                        Pressure       (time, x, y) float64 -0.1841 0.5185 ... -0.05457 -0.5022
                        WindSpeed      (time, x, y) float64 -0.01357 -1.387 0.6451 ... 1.602 -1.026
                        Precipitation  (time, x, y) float64 0.004043 -0.8733 ... 0.6257 -1.678
                        PM25           (time, x, y) float64 0.05214 0.405 ... -0.6217 -0.07389
                    xarray.Dataset
                      • time: 4
                      • x: 3
                      • y: 5
                      • time
                        (time)
                        datetime64[ns]
                        2021-01-01 ... 2021-01-07
                        array(['2021-01-01T00:00:00.000000000', '2021-01-03T00:00:00.000000000',
                               '2021-01-05T00:00:00.000000000', '2021-01-07T00:00:00.000000000'],
                              dtype='datetime64[ns]')
                      • 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.        ]])
                      • Temperature
                        (time, x, y)
                        float64
                        42.5 33.0 81.0 ... 88.0 68.0 41.0
                        array([[[42.5, 33. , 81. , 54. , 36.5],
                                [39.5, 60.5, 46.5, 73. , 70. ],
                                [61.5, 35.5, 43. , 12.5, 20. ]],
                        
                               [[53. , 60. ,  9. ,  5.5, 60. ],
                                [64. , 60.5, 92. , 56. , 60. ],
                                [70. , 69.5, 82.5, 64. , 41.5]],
                        
                               [[51.5, 28.5, 60. , 54. , 36. ],
                                [64.5, 46. , 31. , 25. , 64. ],
                                [58. , 31.5,  3. , 21. , 47. ]],
                        
                               [[83. , 88. , 25. , 39. , 80. ],
                                [78. ,  2. , 38. ,  6. ,  2. ],
                                [30. , 64. , 88. , 68. , 41. ]]])
                      • Humidity
                        (time, x, y)
                        float64
                        0.1222 1.128 ... -0.2817 1.676
                        array([[[ 0.12215763,  1.12825649, -0.07593743, -0.36083896,
                                  0.24782203],
                                [-0.45848769,  0.29754067,  0.1865603 ,  0.62686521,
                                 -0.33162509],
                                [ 1.35445956,  0.52907958, -0.2393428 , -1.64915195,
                                  0.39914644]],
                        
                               [[-0.46566298,  0.49863687,  0.38038635, -0.23517075,
                                  0.32005668],
                                [ 0.05650938,  0.94979825,  1.12255281,  1.39158288,
                                 -1.01883857],
                                [-0.41865644,  0.69916824,  1.88522264, -0.92279185,
                                  0.69486851]],
                        
                               [[-0.00280182, -0.25902148,  0.39280586, -1.02944883,
                                  0.26192112],
                                [ 1.00126838, -0.62051671,  0.73037062,  1.12878459,
                                  0.58951873],
                                [-0.07911056,  0.823643  , -0.07983974, -0.66521832,
                                 -0.98845705]],
                        
                               [[-0.87511885,  0.7636526 ,  0.6410817 , -0.85558611,
                                 -0.78712166],
                                [ 0.02472263, -1.25830263,  0.21001439, -2.78357597,
                                 -0.70328684],
                                [-0.34307771, -0.08441006,  1.58695372, -0.28168974,
                                  1.67588006]]])
                      • Pressure
                        (time, x, y)
                        float64
                        -0.1841 0.5185 ... -0.05457 -0.5022
                        array([[[-0.18405688,  0.51852946, -0.29282062, -0.6117768 ,
                                 -0.33904869],
                                [ 0.65610225,  0.85850021,  0.19039096, -1.90090846,
                                 -1.06065256],
                                [ 0.01455019,  1.25876763,  0.21518039,  0.12050272,
                                 -0.38922282]],
                        
                               [[ 0.10027758, -0.34619477,  0.29749846, -0.15478228,
                                 -0.49943877],
                                [-0.25685954,  0.81202865,  1.30940474,  0.79745962,
                                  0.24537224],
                                [-0.77596365,  0.02038233,  0.00421757,  0.78174514,
                                  0.19058923]],
                        
                               [[ 1.32045012,  0.11033367, -0.17949749, -0.77305159,
                                  0.01810756],
                                [ 0.93756636, -0.80980398,  0.59245489, -0.2492988 ,
                                 -1.00217017],
                                [-0.40021522, -0.96027582, -1.06481408, -0.26072812,
                                 -0.22580978]],
                        
                               [[-0.01770615,  0.70325669,  1.38918332,  1.21306141,
                                 -2.35228298],
                                [ 1.38039618,  0.32021353,  1.81746665,  0.4164532 ,
                                  1.61925102],
                                [-0.6889194 , -0.45100886,  0.15614456, -0.05456826,
                                 -0.50223001]]])
                      • WindSpeed
                        (time, x, y)
                        float64
                        -0.01357 -1.387 ... 1.602 -1.026
                        array([[[-0.0135721 , -1.386991  ,  0.64506455, -0.66378878,
                                  0.56156354],
                                [-1.23904048, -0.18227803, -0.33402847,  0.07160964,
                                 -0.81217439],
                                [ 0.53298529,  0.08196087, -0.65049045,  0.58578127,
                                 -0.72737632]],
                        
                               [[-0.24820604,  0.86633156, -0.49090261, -0.00341091,
                                 -0.41764149],
                                [ 1.49905809,  1.17879745, -0.36290997, -0.76549702,
                                  0.26142762],
                                [ 0.54584398,  0.40563077,  0.140213  ,  0.45945663,
                                 -0.67809187]],
                        
                               [[ 0.96559855,  0.32827963,  0.05719014, -0.17288441,
                                 -0.06088134],
                                [-0.9149653 ,  1.3846916 ,  0.75733774,  1.60688428,
                                 -1.0260856 ],
                                [ 0.5073885 ,  1.20974718,  0.56252759,  0.67007975,
                                 -0.89189261]],
                        
                               [[ 0.64598828,  2.3643872 , -0.75322983,  0.79013423,
                                 -2.02513535],
                                [-0.82472209, -1.64758168, -0.34286339, -0.63347715,
                                 -1.02948406],
                                [-0.26162739, -0.26093031,  0.20363354,  1.6020004 ,
                                 -1.02648887]]])
                      • Precipitation
                        (time, x, y)
                        float64
                        0.004043 -0.8733 ... 0.6257 -1.678
                        array([[[ 0.00404258, -0.87333944,  0.34424776,  0.37079084,
                                  0.4904313 ],
                                [ 0.22819101,  2.19775788,  0.49355182, -1.34986362,
                                  0.67916056],
                                [ 0.58222532, -0.43773196,  0.9121312 ,  0.29175956,
                                 -0.25072544]],
                        
                               [[-0.77966239,  0.08602651,  0.33162849, -1.31582099,
                                  0.11659914],
                                [-0.71387912,  0.29222784,  0.24775396,  0.7885723 ,
                                  1.34923121],
                                [-0.09327884, -1.11826819, -1.10408655,  0.41501745,
                                  0.3169056 ]],
                        
                               [[-1.02658982,  0.09606292,  0.66905348, -0.04691207,
                                  0.15945274],
                                [ 0.10468019,  1.57801392,  0.12417739,  0.08264459,
                                 -0.09192316],
                                [-0.23961516, -0.39333627, -0.6938508 , -1.62253879,
                                 -0.24819977]],
                        
                               [[-0.40251371,  0.62897312,  1.66222385,  1.23651906,
                                 -0.17310687],
                                [-0.14254492,  0.43154201, -1.40214714, -0.14372735,
                                 -0.65240715],
                                [-2.59369344,  0.14563744,  0.55661785,  0.62573462,
                                 -1.67833048]]])
                      • PM25
                        (time, x, y)
                        float64
                        0.05214 0.405 ... -0.6217 -0.07389
                        array([[[ 0.05214418,  0.40500206, -1.0847153 ,  0.1067498 ,
                                  0.25528759],
                                [ 1.27031769,  0.64475182,  0.73545361, -1.30953995,
                                 -0.90634919],
                                [ 0.73492221,  0.10436204, -0.62783741, -0.71846708,
                                 -0.67911181]],
                        
                               [[ 0.95659252, -1.58615714, -1.05895899,  1.08753917,
                                 -0.337207  ],
                                [ 0.04273643,  0.40236683,  0.25637997,  0.50626493,
                                 -0.45176932],
                                [ 0.80185722, -1.38557564, -0.18477174,  0.54320212,
                                 -0.77205164]],
                        
                               [[ 0.61802067, -0.89404338, -0.67060832,  0.0557574 ,
                                  0.56087944],
                                [ 0.02714714,  1.47752081,  0.23916928,  0.9829966 ,
                                  0.2248297 ],
                                [-0.51220996, -0.17902948,  0.12545915, -0.07314123,
                                 -0.37911025]],
                        
                               [[-0.83544664, -0.66202262, -0.29945611,  2.72734691,
                                 -0.85276846],
                                [-0.15030521, -0.41547149,  1.00312552, -0.54798516,
                                  1.53879341],
                                [ 1.01054907,  1.43865551, -0.60019026, -0.62171875,
                                 -0.07388771]]])
                    two_day_sampled_mean.Temperature
                    
                    <xarray.DataArray 'Temperature' (time: 4, x: 3, y: 5)>
                    array([[[42.5, 33. , 81. , 54. , 36.5],
                            [39.5, 60.5, 46.5, 73. , 70. ],
                            [61.5, 35.5, 43. , 12.5, 20. ]],
                    
                           [[53. , 60. ,  9. ,  5.5, 60. ],
                            [64. , 60.5, 92. , 56. , 60. ],
                            [70. , 69.5, 82.5, 64. , 41.5]],
                    
                           [[51.5, 28.5, 60. , 54. , 36. ],
                            [64.5, 46. , 31. , 25. , 64. ],
                            [58. , 31.5,  3. , 21. , 47. ]],
                    
                           [[83. , 88. , 25. , 39. , 80. ],
                            [78. ,  2. , 38. ,  6. ,  2. ],
                            [30. , 64. , 88. , 68. , 41. ]]])
                    Coordinates:
                      * time     (time) datetime64[ns] 2021-01-01 2021-01-03 2021-01-05 2021-01-07
                        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
                    'Temperature'
                    • time: 4
                    • x: 3
                    • y: 5
                    • 42.5 33.0 81.0 54.0 36.5 39.5 60.5 ... 2.0 30.0 64.0 88.0 68.0 41.0
                      array([[[42.5, 33. , 81. , 54. , 36.5],
                              [39.5, 60.5, 46.5, 73. , 70. ],
                              [61.5, 35.5, 43. , 12.5, 20. ]],
                      
                             [[53. , 60. ,  9. ,  5.5, 60. ],
                              [64. , 60.5, 92. , 56. , 60. ],
                              [70. , 69.5, 82.5, 64. , 41.5]],
                      
                             [[51.5, 28.5, 60. , 54. , 36. ],
                              [64.5, 46. , 31. , 25. , 64. ],
                              [58. , 31.5,  3. , 21. , 47. ]],
                      
                             [[83. , 88. , 25. , 39. , 80. ],
                              [78. ,  2. , 38. ,  6. ,  2. ],
                              [30. , 64. , 88. , 68. , 41. ]]])
                      • time
                        (time)
                        datetime64[ns]
                        2021-01-01 ... 2021-01-07
                        array(['2021-01-01T00:00:00.000000000', '2021-01-03T00:00:00.000000000',
                               '2021-01-05T00:00:00.000000000', '2021-01-07T00:00:00.000000000'],
                              dtype='datetime64[ns]')
                      • 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.        ]])
                    Down Sampling

                    In this example, we have down sampled our Dataset from daily frequency to 12-hourly freqeuncy. Down sampling generally introdues NaN/Null entries in dataset because we have new datetime entries in dataset which were not present earlier. We can fill NaN/Null entries using xarray functions like fillna(), ffill(), bfill(), etc.

                    After downsampling, we have taken an average of resampled entries.

                    twelve_hour_sampled_mean = dataset6.resample({"time": "12H"}).mean()
                    
                    twelve_hour_sampled_mean
                    
                    <xarray.Dataset>
                    Dimensions:        (time: 13, x: 3, y: 5)
                    Coordinates:
                      * time           (time) datetime64[ns] 2021-01-01 ... 2021-01-07
                        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
                    Dimensions without coordinates: x, y
                    Data variables:
                        Temperature    (time, x, y) float64 82.0 38.0 90.0 23.0 ... 88.0 68.0 41.0
                        Humidity       (time, x, y) float64 1.093 2.097 0.9607 ... -0.2817 1.676
                        Pressure       (time, x, y) float64 1.176 0.3667 0.4397 ... -0.05457 -0.5022
                        WindSpeed      (time, x, y) float64 0.07895 -1.058 0.4662 ... 1.602 -1.026
                        Precipitation  (time, x, y) float64 -0.5524 -1.196 0.2096 ... 0.6257 -1.678
                        PM25           (time, x, y) float64 1.149 0.8266 -1.477 ... -0.6217 -0.07389
                    xarray.Dataset
                      • time: 13
                      • x: 3
                      • y: 5
                      • time
                        (time)
                        datetime64[ns]
                        2021-01-01 ... 2021-01-07
                        array(['2021-01-01T00:00:00.000000000', '2021-01-01T12:00:00.000000000',
                               '2021-01-02T00:00:00.000000000', '2021-01-02T12:00:00.000000000',
                               '2021-01-03T00:00:00.000000000', '2021-01-03T12:00:00.000000000',
                               '2021-01-04T00:00:00.000000000', '2021-01-04T12:00:00.000000000',
                               '2021-01-05T00:00:00.000000000', '2021-01-05T12:00:00.000000000',
                               '2021-01-06T00:00:00.000000000', '2021-01-06T12:00:00.000000000',
                               '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
                      • 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.        ]])
                      • Temperature
                        (time, x, y)
                        float64
                        82.0 38.0 90.0 ... 88.0 68.0 41.0
                        array([[[82., 38., 90., 23., 51.],
                                [51., 93., 64., 97., 87.],
                                [97., 23., 72.,  9., 25.]],
                        
                               [[nan, nan, nan, nan, nan],
                                [nan, nan, nan, nan, nan],
                                [nan, nan, nan, nan, nan]],
                        
                               [[ 3., 28., 72., 85., 22.],
                                [28., 28., 29., 49., 53.],
                                [26., 48., 14., 16., 15.]],
                        
                               [[nan, nan, nan, nan, nan],
                                [nan, nan, nan, nan, nan],
                                [nan, nan, nan, nan, nan]],
                        
                               [[36., 47.,  5.,  3., 45.],
                                [99., 74., 89., 72., 21.],
                                [44., 55., 87., 96., 12.]],
                        
                        ...
                        
                               [[38., 47., 86., 46., 23.],
                                [59.,  4., 47., 16., 29.],
                                [18., 23.,  2., 27., 24.]],
                        
                               [[nan, nan, nan, nan, nan],
                                [nan, nan, nan, nan, nan],
                                [nan, nan, nan, nan, nan]],
                        
                               [[65., 10., 34., 62., 49.],
                                [70., 88., 15., 34., 99.],
                                [98., 40.,  4., 15., 70.]],
                        
                               [[nan, nan, nan, nan, nan],
                                [nan, nan, nan, nan, nan],
                                [nan, nan, nan, nan, nan]],
                        
                               [[83., 88., 25., 39., 80.],
                                [78.,  2., 38.,  6.,  2.],
                                [30., 64., 88., 68., 41.]]])
                      • Humidity
                        (time, x, y)
                        float64
                        1.093 2.097 ... -0.2817 1.676
                        array([[[ 1.09285914,  2.09665566,  0.96068395, -1.00679045,
                                 -0.27158379],
                                [-0.37605972,  0.92056561,  0.57649183,  0.5065872 ,
                                 -0.77912696],
                                [ 0.76795915,  1.40531069, -0.94689976, -1.9940226 ,
                                 -0.15129096]],
                        
                               [[        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan]],
                        
                               [[-0.84854387,  0.15985731, -1.1125588 ,  0.28511252,
                                  0.76722785],
                                [-0.54091566, -0.32548426, -0.20337122,  0.74714323,
                                  0.11587679],
                                [ 1.94095997, -0.34715154,  0.46821417, -1.3042813 ,
                                  0.94958385]],
                        ...
                               [[-0.78785743, -0.23392604, -0.04324119, -1.61174393,
                                  0.83373781],
                                [ 1.11421149, -0.38539221,  0.55089382,  1.30515827,
                                  1.24060027],
                                [-0.66506239, -0.33045863,  0.95154709, -0.78062656,
                                 -1.52953396]],
                        
                               [[        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan]],
                        
                               [[-0.87511885,  0.7636526 ,  0.6410817 , -0.85558611,
                                 -0.78712166],
                                [ 0.02472263, -1.25830263,  0.21001439, -2.78357597,
                                 -0.70328684],
                                [-0.34307771, -0.08441006,  1.58695372, -0.28168974,
                                  1.67588006]]])
                      • Pressure
                        (time, x, y)
                        float64
                        1.176 0.3667 ... -0.05457 -0.5022
                        array([[[ 1.17620022,  0.36674592,  0.43966429,  0.77113172,
                                 -0.28554035],
                                [ 1.63744119,  2.00222948,  1.46652224, -2.28452632,
                                 -1.38847933],
                                [ 0.2511776 ,  1.07380698, -0.69981506, -1.26867245,
                                 -0.4456235 ]],
                        
                               [[        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan]],
                        
                               [[-1.54431399,  0.670313  , -1.02530554, -1.99468532,
                                 -0.39255704],
                                [-0.3252367 , -0.28522905, -1.08574032, -1.5172906 ,
                                 -0.7328258 ],
                                [-0.22207721,  1.44372828,  1.13017584,  1.50967789,
                                 -0.33282213]],
                        ...
                               [[ 1.29864939, -0.02809683,  0.32941774, -0.95100739,
                                 -1.39324158],
                                [ 1.68638837, -0.39771502,  0.75804519, -0.68319853,
                                 -0.296188  ],
                                [-1.97176127, -2.10662521, -0.76608813, -0.55550723,
                                 -0.93024318]],
                        
                               [[        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan]],
                        
                               [[-0.01770615,  0.70325669,  1.38918332,  1.21306141,
                                 -2.35228298],
                                [ 1.38039618,  0.32021353,  1.81746665,  0.4164532 ,
                                  1.61925102],
                                [-0.6889194 , -0.45100886,  0.15614456, -0.05456826,
                                 -0.50223001]]])
                      • WindSpeed
                        (time, x, y)
                        float64
                        0.07895 -1.058 ... 1.602 -1.026
                        array([[[ 0.07894539, -1.05765309,  0.46617626, -1.2630226 ,
                                 -0.38895218],
                                [-1.20795325, -0.43844783, -1.49854381,  0.49633755,
                                  0.88666826],
                                [ 0.55171479,  0.58689156, -0.4132902 ,  0.94501406,
                                 -1.08944353]],
                        
                               [[        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan]],
                        
                               [[-0.10608959, -1.71632891,  0.82395283, -0.06455496,
                                  1.51207925],
                                [-1.27012772,  0.07389177,  0.83048688, -0.35311826,
                                 -2.51101704],
                                [ 0.51425579, -0.42296983, -0.88769071,  0.22654847,
                                 -0.36530911]],
                        ...
                               [[ 0.38600075,  0.72749619,  0.55701387, -0.21291942,
                                 -0.41960468],
                                [-1.46336765,  1.25235069,  1.63056501,  1.10075378,
                                 -0.50920692],
                                [ 0.82820387,  1.05035477,  0.2660529 ,  0.99606496,
                                  0.21548673]],
                        
                               [[        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan]],
                        
                               [[ 0.64598828,  2.3643872 , -0.75322983,  0.79013423,
                                 -2.02513535],
                                [-0.82472209, -1.64758168, -0.34286339, -0.63347715,
                                 -1.02948406],
                                [-0.26162739, -0.26093031,  0.20363354,  1.6020004 ,
                                 -1.02648887]]])
                      • Precipitation
                        (time, x, y)
                        float64
                        -0.5524 -1.196 ... 0.6257 -1.678
                        array([[[-0.55236716, -1.19625733,  0.20955818,  0.70743435,
                                  0.13764513],
                                [ 0.73722263,  2.24012964, -0.96602978, -0.82823633,
                                  0.15809608],
                                [-0.60228522, -0.7071827 ,  0.18051121,  0.28288257,
                                 -0.10475908]],
                        
                               [[        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan]],
                        
                               [[ 0.56045232, -0.55042156,  0.47893734,  0.03414732,
                                  0.84321747],
                                [-0.28084061,  2.15538612,  1.95313342, -1.87149092,
                                  1.20022505],
                                [ 1.76673585, -0.16828121,  1.64375119,  0.30063656,
                                 -0.39669181]],
                        ...
                               [[-1.18397041,  0.47137331, -0.61284131, -0.69318562,
                                  1.12265484],
                                [-0.57622863,  2.57319673, -0.99218704, -0.70930382,
                                  0.02746981],
                                [-0.76413676, -1.25485989,  1.15855515, -1.01659864,
                                 -0.79784192]],
                        
                               [[        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan],
                                [        nan,         nan,         nan,         nan,
                                         nan]],
                        
                               [[-0.40251371,  0.62897312,  1.66222385,  1.23651906,
                                 -0.17310687],
                                [-0.14254492,  0.43154201, -1.40214714, -0.14372735,
                                 -0.65240715],
                                [-2.59369344,  0.14563744,  0.55661785,  0.62573462,
                                 -1.67833048]]])
                      • PM25
                        (time, x, y)
                        float64
                        1.149 0.8266 ... -0.6217 -0.07389
                        array([[[ 1.14942133e+00,  8.26586757e-01, -1.47701503e+00,
                                  1.42893050e+00,  2.40283034e-01],
                                [ 1.70862604e+00, -8.37166196e-01,  4.83629504e-01,
                                 -1.80580741e+00, -9.49655124e-01],
                                [ 1.18998007e-01,  5.70838063e-01, -1.82446794e+00,
                                 -7.54482730e-01, -1.46312025e+00]],
                        
                               [[            nan,             nan,             nan,
                                             nan,             nan],
                                [            nan,             nan,             nan,
                                             nan,             nan],
                                [            nan,             nan,             nan,
                                             nan,             nan]],
                        
                               [[-1.04513297e+00, -1.65826306e-02, -6.92415573e-01,
                                 -1.21543089e+00,  2.70292152e-01],
                                [ 8.32009334e-01,  2.12666983e+00,  9.87277711e-01,
                                 -8.13272494e-01, -8.63043266e-01],
                                [ 1.35084642e+00, -3.62113981e-01,  5.68793112e-01,
                                 -6.82451422e-01,  1.04896631e-01]],
                        ...
                               [[ 1.38722040e-01, -1.13255119e+00, -3.17190482e-01,
                                  3.17312174e-01,  1.05257571e+00],
                                [ 7.60821520e-01,  1.76736480e+00, -1.03842146e+00,
                                  4.33398497e-02, -4.03442068e-01],
                                [-1.02573574e+00, -6.66209068e-01, -2.09622832e+00,
                                 -3.21497707e-01,  1.03403426e-02]],
                        
                               [[            nan,             nan,             nan,
                                             nan,             nan],
                                [            nan,             nan,             nan,
                                             nan,             nan],
                                [            nan,             nan,             nan,
                                             nan,             nan]],
                        
                               [[-8.35446640e-01, -6.62022616e-01, -2.99456106e-01,
                                  2.72734691e+00, -8.52768464e-01],
                                [-1.50305205e-01, -4.15471493e-01,  1.00312552e+00,
                                 -5.47985161e-01,  1.53879341e+00],
                                [ 1.01054907e+00,  1.43865551e+00, -6.00190258e-01,
                                 -6.21718753e-01, -7.38877135e-02]]])
                    twelve_hour_sampled_mean["time"]
                    
                    <xarray.DataArray 'time' (time: 13)>
                    array(['2021-01-01T00:00:00.000000000', '2021-01-01T12:00:00.000000000',
                           '2021-01-02T00:00:00.000000000', '2021-01-02T12:00:00.000000000',
                           '2021-01-03T00:00:00.000000000', '2021-01-03T12:00:00.000000000',
                           '2021-01-04T00:00:00.000000000', '2021-01-04T12:00:00.000000000',
                           '2021-01-05T00:00:00.000000000', '2021-01-05T12:00:00.000000000',
                           '2021-01-06T00:00:00.000000000', '2021-01-06T12:00:00.000000000',
                           '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
                    Coordinates:
                      * time     (time) datetime64[ns] 2021-01-01 2021-01-01T12:00:00 ... 2021-01-07
                    xarray.DataArray
                    'time'
                    • time: 13
                    • 2021-01-01 2021-01-01T12:00:00 ... 2021-01-06T12:00:00 2021-01-07
                      array(['2021-01-01T00:00:00.000000000', '2021-01-01T12:00:00.000000000',
                             '2021-01-02T00:00:00.000000000', '2021-01-02T12:00:00.000000000',
                             '2021-01-03T00:00:00.000000000', '2021-01-03T12:00:00.000000000',
                             '2021-01-04T00:00:00.000000000', '2021-01-04T12:00:00.000000000',
                             '2021-01-05T00:00:00.000000000', '2021-01-05T12:00:00.000000000',
                             '2021-01-06T00:00:00.000000000', '2021-01-06T12:00:00.000000000',
                             '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
                      • time
                        (time)
                        datetime64[ns]
                        2021-01-01 ... 2021-01-07
                        array(['2021-01-01T00:00:00.000000000', '2021-01-01T12:00:00.000000000',
                               '2021-01-02T00:00:00.000000000', '2021-01-02T12:00:00.000000000',
                               '2021-01-03T00:00:00.000000000', '2021-01-03T12:00:00.000000000',
                               '2021-01-04T00:00:00.000000000', '2021-01-04T12:00:00.000000000',
                               '2021-01-05T00:00:00.000000000', '2021-01-05T12:00:00.000000000',
                               '2021-01-06T00:00:00.000000000', '2021-01-06T12:00:00.000000000',
                               '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
                    twelve_hour_sampled_mean["Temperature"]
                    
                    <xarray.DataArray 'Temperature' (time: 13, x: 3, y: 5)>
                    array([[[82., 38., 90., 23., 51.],
                            [51., 93., 64., 97., 87.],
                            [97., 23., 72.,  9., 25.]],
                    
                           [[nan, nan, nan, nan, nan],
                            [nan, nan, nan, nan, nan],
                            [nan, nan, nan, nan, nan]],
                    
                           [[ 3., 28., 72., 85., 22.],
                            [28., 28., 29., 49., 53.],
                            [26., 48., 14., 16., 15.]],
                    
                           [[nan, nan, nan, nan, nan],
                            [nan, nan, nan, nan, nan],
                            [nan, nan, nan, nan, nan]],
                    
                           [[36., 47.,  5.,  3., 45.],
                            [99., 74., 89., 72., 21.],
                            [44., 55., 87., 96., 12.]],
                    
                    ...
                    
                           [[38., 47., 86., 46., 23.],
                            [59.,  4., 47., 16., 29.],
                            [18., 23.,  2., 27., 24.]],
                    
                           [[nan, nan, nan, nan, nan],
                            [nan, nan, nan, nan, nan],
                            [nan, nan, nan, nan, nan]],
                    
                           [[65., 10., 34., 62., 49.],
                            [70., 88., 15., 34., 99.],
                            [98., 40.,  4., 15., 70.]],
                    
                           [[nan, nan, nan, nan, nan],
                            [nan, nan, nan, nan, nan],
                            [nan, nan, nan, nan, nan]],
                    
                           [[83., 88., 25., 39., 80.],
                            [78.,  2., 38.,  6.,  2.],
                            [30., 64., 88., 68., 41.]]])
                    Coordinates:
                      * time     (time) datetime64[ns] 2021-01-01 2021-01-01T12:00:00 ... 2021-01-07
                        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
                    'Temperature'
                    • time: 13
                    • x: 3
                    • y: 5
                    • 82.0 38.0 90.0 23.0 51.0 51.0 93.0 ... 2.0 30.0 64.0 88.0 68.0 41.0
                      array([[[82., 38., 90., 23., 51.],
                              [51., 93., 64., 97., 87.],
                              [97., 23., 72.,  9., 25.]],
                      
                             [[nan, nan, nan, nan, nan],
                              [nan, nan, nan, nan, nan],
                              [nan, nan, nan, nan, nan]],
                      
                             [[ 3., 28., 72., 85., 22.],
                              [28., 28., 29., 49., 53.],
                              [26., 48., 14., 16., 15.]],
                      
                             [[nan, nan, nan, nan, nan],
                              [nan, nan, nan, nan, nan],
                              [nan, nan, nan, nan, nan]],
                      
                             [[36., 47.,  5.,  3., 45.],
                              [99., 74., 89., 72., 21.],
                              [44., 55., 87., 96., 12.]],
                      
                      ...
                      
                             [[38., 47., 86., 46., 23.],
                              [59.,  4., 47., 16., 29.],
                              [18., 23.,  2., 27., 24.]],
                      
                             [[nan, nan, nan, nan, nan],
                              [nan, nan, nan, nan, nan],
                              [nan, nan, nan, nan, nan]],
                      
                             [[65., 10., 34., 62., 49.],
                              [70., 88., 15., 34., 99.],
                              [98., 40.,  4., 15., 70.]],
                      
                             [[nan, nan, nan, nan, nan],
                              [nan, nan, nan, nan, nan],
                              [nan, nan, nan, nan, nan]],
                      
                             [[83., 88., 25., 39., 80.],
                              [78.,  2., 38.,  6.,  2.],
                              [30., 64., 88., 68., 41.]]])
                      • time
                        (time)
                        datetime64[ns]
                        2021-01-01 ... 2021-01-07
                        array(['2021-01-01T00:00:00.000000000', '2021-01-01T12:00:00.000000000',
                               '2021-01-02T00:00:00.000000000', '2021-01-02T12:00:00.000000000',
                               '2021-01-03T00:00:00.000000000', '2021-01-03T12:00:00.000000000',
                               '2021-01-04T00:00:00.000000000', '2021-01-04T12:00:00.000000000',
                               '2021-01-05T00:00:00.000000000', '2021-01-05T12:00:00.000000000',
                               '2021-01-06T00:00:00.000000000', '2021-01-06T12:00:00.000000000',
                               '2021-01-07T00:00:00.000000000'], dtype='datetime64[ns]')
                      • 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.        ]])

                    This ends our small tutorial explaining how we can use Dataset data structure available from xarray library. Please feel free to let us know your views in the comments section.

                    References

                    Sunny Solanki  Sunny Solanki

                    YouTube Subscribe Comfortable Learning through Video Tutorials?

                    If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

                    Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

                    When going through coding examples, it's quite common to have doubts and errors.

                    If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

                    You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

                    Share Views Want to Share Your Views? Have Any Suggestions?

                    If you want to

                    • provide some suggestions on topic
                    • share your views
                    • include some details in tutorial
                    • suggest some new topics on which we should create tutorials/blogs
                    Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.