From 70c52b8106aa7840b0e0669b0fe6bd9ee6a174b5 Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Mon, 29 May 2023 22:42:38 +0530 Subject: [PATCH 01/30] add examples --- xarray/core/dataset.py | 118 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 433c724cc21..879bb742001 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2486,6 +2486,35 @@ def isel( in this dataset, unless vectorized indexing was triggered by using an array indexer, in which case the data will be a copy. + Example + ------- + + import xarray as xr + + dataset = xr.Dataset( + { + 'math_scores': ( + ['student', 'test'], + [[90, 85, 92], + [78, 80, 85], + [95, 92, 98]] + ), + 'english_scores': ( + ['student', 'test'], + [[88, 90, 92], + [75, 82, 79], + [93, 96, 91]] + ) + }, + coords={ + 'student': ['Alice', 'Bob', 'Charlie'], + 'test': ['Test 1', 'Test 2', 'Test 3'] + } + ) + + second_student_first_test = dataset.isel(student=1, test=0) + print(second_student_first_test) + See Also -------- Dataset.sel @@ -5911,6 +5940,38 @@ def reduce( reduced : Dataset Dataset with this object's DataArrays replaced with new DataArrays of summarized data and the indicated dimension(s) removed. + + Example + ------- + + import xarray as xr + import numpy as np + + dataset = xr.Dataset( + { + 'math_scores': ( + ['student', 'test'], + [[90, 85, 92], + [78, 80, 85], + [95, 92, 98]] + ), + 'english_scores': ( + ['student', 'test'], + [[88, 90, 92], + [75, 82, 79], + [93, 96, 91]] + ) + }, + coords={ + 'student': ['Alice', 'Bob', 'Charlie'], + 'test': ['Test 1', 'Test 2', 'Test 3'] + } + ) + + mean_scores = dataset.reduce(func=np.mean, dim='test') + + print(mean_scores) + """ if kwargs.get("axis", None) is not None: raise ValueError( @@ -8424,6 +8485,36 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: ------- result : Dataset + Example + ------- + + import xarray as xr + + dataset = xr.Dataset( + { + 'math_scores': ( + ['student', 'test'], + [[90, 85, 79], + [78, 80, 85], + [95, 92, 98]] + ), + 'english_scores': ( + ['student', 'test'], + [[88, 80, 92], + [75, 95, 79], + [93, 96, 78]] + ) + }, + coords={ + 'student': ['Alice', 'Bob', 'Charlie'], + 'test': ['Test 1', 'Test 2', 'Test 3'] + } + ) + + argmin_indices = dataset.argmin(dim='student') + + print(argmin_indices) + See Also -------- DataArray.argmin @@ -8483,6 +8574,33 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: ------- result : Dataset + Example + ------- + + dataset = xr.Dataset( + { + 'math_scores': ( + ['student', 'test'], + [[90, 85, 92], + [78, 80, 85], + [95, 92, 98]] + ), + 'english_scores': ( + ['student', 'test'], + [[88, 90, 92], + [75, 82, 79], + [93, 96, 91]] + ) + }, + coords={ + 'student': ['Alice', 'Bob', 'Charlie'], + 'test': ['Test 1', 'Test 2', 'Test 3'] + } + ) + argmax_indices = dataset.argmax(dim='test') + + print(argmax_indices) + See Also -------- DataArray.argmax From 1e403a89808e73c41b1532ba64695e6cb751c5cb Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Mon, 5 Jun 2023 23:14:11 +0530 Subject: [PATCH 02/30] changes done to dataset.isel --- xarray/core/dataset.py | 243 +++++++++++++++++++++++++---------------- 1 file changed, 147 insertions(+), 96 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 879bb742001..f63d54cc977 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2489,31 +2489,68 @@ def isel( Example ------- - import xarray as xr - - dataset = xr.Dataset( - { - 'math_scores': ( - ['student', 'test'], - [[90, 85, 92], - [78, 80, 85], - [95, 92, 98]] - ), - 'english_scores': ( - ['student', 'test'], - [[88, 90, 92], - [75, 82, 79], - [93, 96, 91]] - ) - }, - coords={ - 'student': ['Alice', 'Bob', 'Charlie'], - 'test': ['Test 1', 'Test 2', 'Test 3'] - } - ) - - second_student_first_test = dataset.isel(student=1, test=0) - print(second_student_first_test) + # Defined the dataset + >>> dataset = xr.Dataset( + ... { + ... "math_scores": ( + ... ["student", "test"], + ... [[90, 85, 92], [78, 80, 85], [95, 92, 98]], + ... ), + ... "english_scores": ( + ... ["student", "test"], + ... [[88, 90, 92], [75, 82, 79], [93, 96, 91]], + ... ), + ... }, + ... coords={ + ... "student": ["Alice", "Bob", "Charlie"], + ... "test": ["Test 1", "Test 2", "Test 3"], + ... }, + ... ) + + # A specific element from the dataset is selected + >>> second_student_first_test = dataset.isel(student=1, test=0) + + # Print the selected element + >>> second_student_first_test + + Dimensions: () + Coordinates: + student >> slice_of_data = dataset.isel(student=slice(0, 2), test=slice(0, 2)) + + # Print the sliced data + >>> slice_of_data + + Dimensions: (student: 2, test: 2) + Coordinates: + * student (student) >> index_array = xr.DataArray([0, 2], dims="student") + + # Use isel with the DataArray for indexing + >>> indexed_data = dataset.isel(student=index_array) + + # Print the indexed data + >>> indexed_data + + Dimensions: (student: 2, test: 3) + Coordinates: + * student (student) >> dataset = xr.Dataset( + ... { + ... "math_scores": ( + ... ["student", "test"], + ... [[90, 85, 92], [78, 80, 85], [95, 92, 98]], + ... ), + ... "english_scores": ( + ... ["student", "test"], + ... [[88, 90, 92], [75, 82, 79], [93, 96, 91]], + ... ), + ... }, + ... coords={ + ... "student": ["Alice", "Bob", "Charlie"], + ... "test": ["Test 1", "Test 2", "Test 3"], + ... }, + ... ) - mean_scores = dataset.reduce(func=np.mean, dim='test') + # Mean scores across the 'test' dimension are calculated + >>> mean_scores = dataset.reduce(func=np.mean, dim="test") - print(mean_scores) + # Print the mean scores + >>> mean_scores + + Dimensions: (student: 3) + Coordinates: + * student (student) T_Dataset: Example ------- - import xarray as xr - - dataset = xr.Dataset( - { - 'math_scores': ( - ['student', 'test'], - [[90, 85, 79], - [78, 80, 85], - [95, 92, 98]] - ), - 'english_scores': ( - ['student', 'test'], - [[88, 80, 92], - [75, 95, 79], - [93, 96, 78]] - ) - }, - coords={ - 'student': ['Alice', 'Bob', 'Charlie'], - 'test': ['Test 1', 'Test 2', 'Test 3'] - } - ) + # Defined the dataset + >>> dataset = xr.Dataset( + ... { + ... "math_scores": ( + ... ["student", "test"], + ... [[90, 85, 79], [78, 80, 85], [95, 92, 98]], + ... ), + ... "english_scores": ( + ... ["student", "test"], + ... [[88, 80, 92], [75, 95, 79], [93, 96, 78]], + ... ), + ... }, + ... coords={ + ... "student": ["Alice", "Bob", "Charlie"], + ... "test": ["Test 1", "Test 2", "Test 3"], + ... }, + ... ) - argmin_indices = dataset.argmin(dim='student') + # Indices of the minimum values along the 'student' dimension are calculated + >>> argmin_indices = dataset.argmin(dim="student") - print(argmin_indices) + # Print the indices of the minimum values + >>> argmin_indices + + Dimensions: (test: 3) + Coordinates: + * test (test) T_Dataset: Example ------- - dataset = xr.Dataset( - { - 'math_scores': ( - ['student', 'test'], - [[90, 85, 92], - [78, 80, 85], - [95, 92, 98]] - ), - 'english_scores': ( - ['student', 'test'], - [[88, 90, 92], - [75, 82, 79], - [93, 96, 91]] - ) - }, - coords={ - 'student': ['Alice', 'Bob', 'Charlie'], - 'test': ['Test 1', 'Test 2', 'Test 3'] - } - ) - argmax_indices = dataset.argmax(dim='test') + #Defined the dataset + >>> dataset = xr.Dataset( + ... { + ... "math_scores": ( + ... ["student", "test"], + ... [[90, 85, 92], [78, 80, 85], [95, 92, 98]], + ... ), + ... "english_scores": ( + ... ["student", "test"], + ... [[88, 90, 92], [75, 82, 79], [93, 96, 91]], + ... ), + ... }, + ... coords={ + ... "student": ["Alice", "Bob", "Charlie"], + ... "test": ["Test 1", "Test 2", "Test 3"], + ... }, + ... ) + + # Indices of the minimum values along the 'student' dimension are calculated + >>> argmax_indices = dataset.argmax(dim="test") - print(argmax_indices) + # Print the indices of the minimum values + >>> argmax_indices + + Dimensions: (student: 3) + Coordinates: + * student (student) Date: Mon, 12 Jun 2023 07:09:06 +0530 Subject: [PATCH 03/30] Changes done on isel and reduce --- xarray/core/dataset.py | 129 +++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 57 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index f63d54cc977..cf754966439 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -5950,67 +5950,82 @@ def reduce( ) -> T_Dataset: """Reduce this dataset by applying `func` along some dimension(s). - Parameters - ---------- - func : callable - Function which can be called in the form - `f(x, axis=axis, **kwargs)` to return the result of reducing an - np.ndarray over an integer valued axis. - dim : str, Iterable of Hashable or None, optional - Dimension(s) over which to apply `func`. By default `func` is - applied over all dimensions. - keep_attrs : bool or None, optional - If True, the dataset's attributes (`attrs`) will be copied from - the original object to the new one. If False (default), the new - object will be returned without attributes. - keepdims : bool, default: False - If True, the dimensions which are reduced are left in the result - as dimensions of size one. Coordinates that use these dimensions - are removed. - numeric_only : bool, default: False - If True, only apply ``func`` to variables with a numeric dtype. - **kwargs : Any - Additional keyword arguments passed on to ``func``. - - Returns - ------- - reduced : Dataset - Dataset with this object's DataArrays replaced with new DataArrays - of summarized data and the indicated dimension(s) removed. + Parameters + ---------- + func : callable + Function which can be called in the form + `f(x, axis=axis, **kwargs)` to return the result of reducing an + np.ndarray over an integer valued axis. + dim : str, Iterable of Hashable or None, optional + Dimension(s) over which to apply `func`. By default `func` is + applied over all dimensions. + keep_attrs : bool or None, optional + If True, the dataset's attributes (`attrs`) will be copied from + the original object to the new one. If False (default), the new + object will be returned without attributes. + keepdims : bool, default: False + If True, the dimensions which are reduced are left in the result + as dimensions of size one. Coordinates that use these dimensions + are removed. + numeric_only : bool, default: False + If True, only apply ``func`` to variables with a numeric dtype. + **kwargs : Any + Additional keyword arguments passed on to ``func``. + + Returns + ------- + reduced : Dataset + Dataset with this object's DataArrays replaced with new DataArrays + of summarized data and the indicated dimension(s) removed. + + Example + ------- + + # Defined the dataset + >>> dataset = xr.Dataset( + ... { + ... "math_scores": ( + ... ["student", "test"], + ... [[90, 85, 92], [78, 80, 85], [95, 92, 98]], + ... ), + ... "english_scores": ( + ... ["student", "test"], + ... [[88, 90, 92], [75, 82, 79], [93, 96, 91]], + ... ), + ... }, + ... coords={ + ... "student": ["Alice", "Bob", "Charlie"], + ... "test": ["Test 1", "Test 2", "Test 3"], + ... }, + ... ) + + # Calculate the 75th percentile of math scores for each student using np.percentile + >>> percentile_math_scores = dataset["math_scores"].reduce( + ... np.percentile, q=75, dim="test" + ... ) + + # Print 75th percentile of math scores + >>> percentile_math_scores + + array([91. , 82.5, 96.5]) + Coordinates: + * student (student) >> dataset = xr.Dataset( - ... { - ... "math_scores": ( - ... ["student", "test"], - ... [[90, 85, 92], [78, 80, 85], [95, 92, 98]], - ... ), - ... "english_scores": ( - ... ["student", "test"], - ... [[88, 90, 92], [75, 82, 79], [93, 96, 91]], - ... ), - ... }, - ... coords={ - ... "student": ["Alice", "Bob", "Charlie"], - ... "test": ["Test 1", "Test 2", "Test 3"], - ... }, - ... ) + # To use the `skew` function, you need to import it from the `scipy.stats` module + >>> from scipy.stats import skew - # Mean scores across the 'test' dimension are calculated - >>> mean_scores = dataset.reduce(func=np.mean, dim="test") + # Calculate the skewness of math scores for each student using scipy.stats.skew + >>> skewness_math_scores = dataset["math_scores"].reduce( + ... skew, dim="test" + ... ) - # Print the mean scores - >>> mean_scores - - Dimensions: (student: 3) - Coordinates: - * student (student) >> skewness_math_scores + + array([-0.47033046, 0.47033046, 0. ]) + Coordinates: + * student (student) Date: Tue, 20 Jun 2023 10:59:53 +0530 Subject: [PATCH 04/30] xarray.dataset.tail --- xarray/core/dataset.py | 186 ++++++++++++++++++++++++++++------------- 1 file changed, 129 insertions(+), 57 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index cf754966439..ea1f4b46e27 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2786,6 +2786,37 @@ def tail( The keyword arguments form of ``indexers``. One of indexers or indexers_kwargs must be provided. + Example + ------- + + # Sample dataset + >>> data = xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dims=("x", "y")) + >>> dataset = xr.Dataset({"data": data}) + + # Print the original dataset + >>> "Original Dataset:" + >>> dataset + + # Get the last 2 elements using tail() + >>> tail_dataset = dataset.tail(2) + + # Print the tail dataset + >>> "Tail Dataset:" + >>> tail_dataset + 'Original Dataset:' + + Dimensions: (x: 3, y: 3) + Dimensions without coordinates: x, y + Data variables: + data (x, y) int64 1 2 3 4 5 6 7 8 9 + + 'Tail Dataset:' + + Dimensions: (x: 2, y: 2) + Dimensions without coordinates: x, y + Data variables: + data (x, y) int64 5 6 8 9 + See Also -------- Dataset.head @@ -8515,68 +8546,109 @@ def idxmax( def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: """Indices of the minima of the member variables. - If there are multiple minima, the indices of the first one found will be - returned. + If there are multiple minima, the indices of the first one found will be + returned. - Parameters - ---------- - dim : Hashable, optional - The dimension over which to find the minimum. By default, finds minimum over - all dimensions - for now returning an int for backward compatibility, but - this is deprecated, in future will be an error, since DataArray.argmin will - return a dict with indices for all dimensions, which does not make sense for - a Dataset. - keep_attrs : bool, optional - If True, the attributes (`attrs`) will be copied from the original - object to the new one. If False (default), the new object will be - returned without attributes. - skipna : bool, optional - If True, skip missing values (as marked by NaN). By default, only - skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been - implemented (object, datetime64 or timedelta64). + Parameters + ---------- + dim : Hashable, optional + The dimension over which to find the minimum. By default, finds minimum over + all dimensions - for now returning an int for backward compatibility, but + this is deprecated, in future will be an error, since DataArray.argmin will + return a dict with indices for all dimensions, which does not make sense for + a Dataset. + keep_attrs : bool, optional + If True, the attributes (`attrs`) will be copied from the original + object to the new one. If False (default), the new object will be + returned without attributes. + skipna : bool, optional + If True, skip missing values (as marked by NaN). By default, only + skips missing values for float dtypes; other dtypes either do not + have a sentinel missing value (int) or skipna=True has not been + implemented (object, datetime64 or timedelta64). - Returns - ------- - result : Dataset + Returns + ------- + result : Dataset - Example - ------- + Example + ------- - # Defined the dataset - >>> dataset = xr.Dataset( - ... { - ... "math_scores": ( - ... ["student", "test"], - ... [[90, 85, 79], [78, 80, 85], [95, 92, 98]], - ... ), - ... "english_scores": ( - ... ["student", "test"], - ... [[88, 80, 92], [75, 95, 79], [93, 96, 78]], - ... ), - ... }, - ... coords={ - ... "student": ["Alice", "Bob", "Charlie"], - ... "test": ["Test 1", "Test 2", "Test 3"], - ... }, - ... ) + # Defined the dataset + >>> dataset = xr.Dataset( + ... { + ... "math_scores": ( + ... ["student", "test"], + ... [[90, 85, 79], [78, 80, 85], [95, 92, 98]], + ... ), + ... "english_scores": ( + ... ["student", "test"], + ... [[88, 80, 92], [75, 95, 79], [93, 96, 78]], + ... ), + ... }, + ... coords={ + ... "student": ["Alice", "Bob", "Charlie"], + ... "test": ["Test 1", "Test 2", "Test 3"], + ... }, + ... ) - # Indices of the minimum values along the 'student' dimension are calculated - >>> argmin_indices = dataset.argmin(dim="student") + # Indices of the minimum values along the 'student' dimension are calculated + >>> argmin_indices = dataset.argmin(dim="student") - # Print the indices of the minimum values - >>> argmin_indices - - Dimensions: (test: 3) - Coordinates: - * test (test) >> argmin_indices + + Dimensions: (test: 3) + Coordinates: + * test (test) >> precipitation_data = xr.Dataset( + ... { + ... "precipitation": ( + ... ["time", "location"], + ... [ + ... [5.3, 3.8, 2.1], + ... [2.7, 4.2, 1.5], + ... [3.9, 2.6, 1.9], + ... [2.1, 1.4, 3.2], + ... ], + ... ) + ... }, + ... coords={ + ... "time": pd.date_range(start="2023-06-01", periods=4), + ... "location": ["A", "B", "C"], + ... }, + ... ) + + # Index of the day with the minimum precipitation + >>> min_precipitation_index = precipitation_data[ + ... "precipitation" + ... ].argmin(dim="time") + + # Using isel to extract the corresponding data + >>> min_precipitation_data = precipitation_data.isel( + ... time=min_precipitation_index + ... ) + + # Print the minimum precipitation data + >>> min_precipitation_data + + Dimensions: (location: 3) + Coordinates: + time (location) datetime64[ns] 2023-06-04 2023-06-04 2023-06-02 + * location (location) T_Dataset: ... }, ... ) - # Indices of the minimum values along the 'student' dimension are calculated + # Indices of the maximum values along the 'student' dimension are calculated >>> argmax_indices = dataset.argmax(dim="test") - # Print the indices of the minimum values + # Print the indices of the maximum values >>> argmax_indices Dimensions: (student: 3) From 82e1161578ce44f24cae2aa5fd6b11a8037c32be Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Tue, 20 Jun 2023 11:23:54 +0530 Subject: [PATCH 05/30] xarray.dataset.head --- xarray/core/dataset.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index ea1f4b46e27..ebf9e245bc7 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2740,6 +2740,43 @@ def head( The keyword arguments form of ``indexers``. One of indexers or indexers_kwargs must be provided. + Example + ------- + + # Sample Dataset + >>> dataset = xr.Dataset( + ... { + ... "temperature": [25.1, 28.3, 30.5, 27.2, 26.8], + ... "humidity": [60.2, 55.6, 50.3, 58.8, 61.7], + ... }, + ... coords={"time": [1, 2, 3, 4, 5]}, + ... ) + + # Originial Dataset + >>> dataset + + Dimensions: (temperature: 5, humidity: 5, time: 5) + Coordinates: + * temperature (temperature) float64 25.1 28.3 30.5 27.2 26.8 + * humidity (humidity) float64 60.2 55.6 50.3 58.8 61.7 + * time (time) int64 1 2 3 4 5 + Data variables: + *empty* + + # Use head() function to retrieve the first three elements + >>> head_dataset = dataset.head(2) + + # Print the head dataset + >>> head_dataset + + Dimensions: (temperature: 2, humidity: 2, time: 2) + Coordinates: + * temperature (temperature) float64 25.1 28.3 + * humidity (humidity) float64 60.2 55.6 + * time (time) int64 1 2 + Data variables: + *empty* + See Also -------- Dataset.tail From 760aa4a8c45e8b84798684b9327415bbe53dc627 Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Tue, 20 Jun 2023 11:55:53 +0530 Subject: [PATCH 06/30] xarray.dataset.dropna --- xarray/core/dataset.py | 73 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index ebf9e245bc7..fc02ab76352 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -5683,6 +5683,79 @@ def dropna( Which variables to check for missing values. By default, all variables in the dataset are checked. + Example + ------- + + # Sample dataset with missing values + >>> data = { + ... "time": [0, 1, 2, 3], + ... "temperature": [25.0, None, 27.5, 28.0], + ... "humidity": [60.0, 65.0, None, 70.0], + ... } + >>> dataset = xr.Dataset(data) + # Print the original dataset + >>> dataset + + Dimensions: (time: 4, temperature: 4, humidity: 4) + Coordinates: + * time (time) int64 0 1 2 3 + * temperature (temperature) object 25.0 None 27.5 28.0 + * humidity (humidity) object 60.0 65.0 None 70.0 + Data variables: + *empty* + + # Drop rows with any missing values + >>> dataset_dropped_any = dataset.dropna(dim="time", how="any") + # Print the dataset after dropping rows with any missing values + >>> dataset_dropped_any + + Dimensions: (time: 4, temperature: 4, humidity: 4) + Coordinates: + * time (time) int64 0 1 2 3 + * temperature (temperature) object 25.0 None 27.5 28.0 + * humidity (humidity) object 60.0 65.0 None 70.0 + Data variables: + *empty* + + # Drop rows with all missing values + >>> dataset_dropped_all = dataset.dropna(dim="time", how="all") + # Print the dataset after dropping rows with all missing values + >>> dataset_dropped_all + + Dimensions: (time: 0, temperature: 4, humidity: 4) + Coordinates: + * time (time) int64 + * temperature (temperature) object 25.0 None 27.5 28.0 + * humidity (humidity) object 60.0 65.0 None 70.0 + Data variables: + *empty* + + # Drop rows with a threshold of non-missing values + >>> dataset_dropped_thresh = dataset.dropna(dim="time", thresh=2) + # Print the dataset after dropping rows with a threshold of non-missing values + >>> dataset_dropped_thresh + + Dimensions: (time: 0, temperature: 4, humidity: 4) + Coordinates: + * time (time) int64 + * temperature (temperature) object 25.0 None 27.5 28.0 + * humidity (humidity) object 60.0 65.0 None 70.0 + Data variables: + *empty* + + # Drop rows for a subset of variables + >>> dataset_dropped_subset = dataset.dropna(dim="time", subset=["temperature"]) + # Print the dataset after dropping rows for a subset of variables + >>> dataset_dropped_subset + + Dimensions: (time: 4, temperature: 4, humidity: 4) + Coordinates: + * time (time) int64 0 1 2 3 + * temperature (temperature) object 25.0 None 27.5 28.0 + * humidity (humidity) object 60.0 65.0 None 70.0 + Data variables: + *empty* + Returns ------- Dataset From 848a5fd3a598c5c7d8d3ca38f9abebc93b81ce0c Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Tue, 20 Jun 2023 12:26:49 +0530 Subject: [PATCH 07/30] xarray.dataset.ffill --- xarray/core/dataset.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index fc02ab76352..74ae45328f3 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -6025,6 +6025,34 @@ def ffill(self: T_Dataset, dim: Hashable, limit: int | None = None) -> T_Dataset than 0 or None for no limit. Must be None or greater than or equal to axis length if filling along chunked axes (dimensions). + Example + ------- + # Create a sample dataset with missing values + >>> time = pd.date_range("2023-01-01", periods=10, freq="D") + >>> data = np.array([1, np.nan, 3, np.nan, 5, 6, np.nan, 8, np.nan, 10]) + >>> dataset = xr.Dataset({"data": (("time",), data)}, coords={"time": time}) + + # Perform forward fill (ffill) on the dataset + >>> filled_dataset = dataset.ffill(dim="time") + + # Print the original dataset + >>> dataset + + Dimensions: (time: 10) + Coordinates: + * time (time) datetime64[ns] 2023-01-01 2023-01-02 ... 2023-01-10 + Data variables: + data (time) float64 1.0 nan 3.0 nan 5.0 6.0 nan 8.0 nan 10.0 + + # Print the filled dataset, fills NaN values by propagating values forward + >>> filled_dataset + + Dimensions: (time: 10) + Coordinates: + * time (time) datetime64[ns] 2023-01-01 2023-01-02 ... 2023-01-10 + Data variables: + data (time) float64 1.0 1.0 3.0 3.0 5.0 6.0 6.0 8.0 8.0 10.0 + Returns ------- Dataset From eb7514e6f806209f757012ab82f9246b6e4d2cae Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Tue, 20 Jun 2023 15:15:54 +0530 Subject: [PATCH 08/30] xarray.dataset.bfill --- xarray/core/dataset.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 74ae45328f3..055826ec77b 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -6079,6 +6079,36 @@ def bfill(self: T_Dataset, dim: Hashable, limit: int | None = None) -> T_Dataset than 0 or None for no limit. Must be None or greater than or equal to axis length if filling along chunked axes (dimensions). + Example + ------- + # Create a sample dataset + >>> data = np.array([[1, 2, np.nan], [4, np.nan, 6], [np.nan, 8, 9]]) + >>> coords = {"x": [0, 1, 2], "y": [0, 1, 2]} + >>> dataset = xr.Dataset({"data": (["x", "y"], data)}, coords=coords) + + # Print the original dataset + >>> dataset + + Dimensions: (x: 3, y: 3) + Coordinates: + * x (x) int64 0 1 2 + * y (y) int64 0 1 2 + Data variables: + data (x, y) float64 1.0 2.0 nan 4.0 nan 6.0 nan 8.0 9.0 + + # Apply backward fill (bfill) along the 'y' dimension + >>> dataset_bfill = dataset.bfill(dim="y") + + # Print the dataset after backward fill + >>> dataset_bfill + + Dimensions: (x: 3, y: 3) + Coordinates: + * x (x) int64 0 1 2 + * y (y) int64 0 1 2 + Data variables: + data (x, y) float64 1.0 2.0 nan 4.0 6.0 6.0 8.0 8.0 9.0 + Returns ------- Dataset From 5ecfd6380f9096fe8d7b2c0ce4d8befcf2ec3f98 Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Tue, 20 Jun 2023 16:26:09 +0530 Subject: [PATCH 09/30] xarray.dataset.set_Coords --- xarray/core/dataset.py | 61 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 055826ec77b..f7b7ed7cf47 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1726,19 +1726,58 @@ def data_vars(self) -> DataVariables: def set_coords(self: T_Dataset, names: Hashable | Iterable[Hashable]) -> T_Dataset: """Given names of one or more variables, set them as coordinates - Parameters - ---------- - names : hashable or iterable of hashable - Name(s) of variables in this dataset to convert into coordinates. + Parameters + ---------- + names : hashable or iterable of hashable + Name(s) of variables in this dataset to convert into coordinates. - Returns - ------- - Dataset + Example + ------- - See Also - -------- - Dataset.swap_dims - Dataset.assign_coords + # Sample dataset + >>> data = xr.DataArray( + ... [[1, 2], [3, 4]], + ... dims=("x", "y"), + ... coords={"x": [0, 1], "y": [0, 1]}, + ... name="data", + ... ) + >>> dataset = xr.Dataset(data_vars={"data": data}) + + # Print the dataset before setting coordinates + >>> dataset + + Dimensions: (x: 2, y: 2) + Coordinates: + * x (x) int64 0 1 + * y (y) int64 0 1 + Data variables: + data (x, y) int64 1 2 3 4 + + + # Set "x" and "y" variables as coordinates + >>> dataset_coords = dataset.set_coords(["x", "y"]) + + # Print the dataset after setting coordinates + >>> dataset_coords + + Dimensions: (x: 2, y: 2) + Coordinates: + x (x) int64 0 1 + y (y) int64 0 1 + Data variables: + data (x, y) int64 1 2 3 4 + + In the initial dataset, the "x" and "y" variables are present as dimensions. After calling ``set_coords`` (["x", "y"]), these + variables are converted to coordinates, as shown in the final dataset. + + Returns + ------- + Dataset + + See Also + -------- + Dataset.swap_dims + Dataset.assign_coords """ # TODO: allow inserting new coordinates with this method, like # DataFrame.set_index? From 7575a1245e19c3dbf813d68400da6fe8b07f66bc Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Tue, 20 Jun 2023 16:39:02 +0530 Subject: [PATCH 10/30] xarray.dataset.reset_coords --- xarray/core/dataset.py | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index f7b7ed7cf47..0b0278f61bc 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1808,6 +1808,58 @@ def reset_coords( If True, remove coordinates instead of converting them into variables. + Example + ------- + + # Sample dataset + >>> dataset = xr.Dataset( + ... { + ... "temperature": ( + ... ["time", "lat", "lon"], + ... [[[25, 26], [27, 28]], [[29, 30], [31, 32]]], + ... ), + ... "precipitation": ( + ... ["time", "lat", "lon"], + ... [[[0.5, 0.8], [0.2, 0.4]], [[0.3, 0.6], [0.7, 0.9]]], + ... ), + ... }, + ... coords={ + ... "time": pd.date_range(start="2023-01-01", periods=2), + ... "lat": [40, 41], + ... "lon": [-80, -79], + ... "altitude": 1000, + ... }, + ... ) + + # Print the dataset before resetting coordinates + >>> dataset + + Dimensions: (time: 2, lat: 2, lon: 2) + Coordinates: + * time (time) datetime64[ns] 2023-01-01 2023-01-02 + * lat (lat) int64 40 41 + * lon (lon) int64 -80 -79 + altitude int64 1000 + Data variables: + temperature (time, lat, lon) int64 25 26 27 28 29 30 31 32 + precipitation (time, lat, lon) float64 0.5 0.8 0.2 0.4 0.3 0.6 0.7 0.9 + + # Reset the 'altitude' coordinate + >>> dataset_reset = dataset.reset_coords("altitude") + + # Print the dataset after resetting coordinates + >>> dataset_reset + + Dimensions: (time: 2, lat: 2, lon: 2) + Coordinates: + * time (time) datetime64[ns] 2023-01-01 2023-01-02 + * lat (lat) int64 40 41 + * lon (lon) int64 -80 -79 + Data variables: + temperature (time, lat, lon) int64 25 26 27 28 29 30 31 32 + precipitation (time, lat, lon) float64 0.5 0.8 0.2 0.4 0.3 0.6 0.7 0.9 + altitude int64 1000 + Returns ------- Dataset From 14952fa3a5eb60fec4af254a63541231b312e75a Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Thu, 22 Jun 2023 12:22:15 +0530 Subject: [PATCH 11/30] Revert "xarray.dataset.reset_coords" This reverts commit 7575a1245e19c3dbf813d68400da6fe8b07f66bc. --- xarray/core/dataset.py | 52 ------------------------------------------ 1 file changed, 52 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 0b0278f61bc..f7b7ed7cf47 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1808,58 +1808,6 @@ def reset_coords( If True, remove coordinates instead of converting them into variables. - Example - ------- - - # Sample dataset - >>> dataset = xr.Dataset( - ... { - ... "temperature": ( - ... ["time", "lat", "lon"], - ... [[[25, 26], [27, 28]], [[29, 30], [31, 32]]], - ... ), - ... "precipitation": ( - ... ["time", "lat", "lon"], - ... [[[0.5, 0.8], [0.2, 0.4]], [[0.3, 0.6], [0.7, 0.9]]], - ... ), - ... }, - ... coords={ - ... "time": pd.date_range(start="2023-01-01", periods=2), - ... "lat": [40, 41], - ... "lon": [-80, -79], - ... "altitude": 1000, - ... }, - ... ) - - # Print the dataset before resetting coordinates - >>> dataset - - Dimensions: (time: 2, lat: 2, lon: 2) - Coordinates: - * time (time) datetime64[ns] 2023-01-01 2023-01-02 - * lat (lat) int64 40 41 - * lon (lon) int64 -80 -79 - altitude int64 1000 - Data variables: - temperature (time, lat, lon) int64 25 26 27 28 29 30 31 32 - precipitation (time, lat, lon) float64 0.5 0.8 0.2 0.4 0.3 0.6 0.7 0.9 - - # Reset the 'altitude' coordinate - >>> dataset_reset = dataset.reset_coords("altitude") - - # Print the dataset after resetting coordinates - >>> dataset_reset - - Dimensions: (time: 2, lat: 2, lon: 2) - Coordinates: - * time (time) datetime64[ns] 2023-01-01 2023-01-02 - * lat (lat) int64 40 41 - * lon (lon) int64 -80 -79 - Data variables: - temperature (time, lat, lon) int64 25 26 27 28 29 30 31 32 - precipitation (time, lat, lon) float64 0.5 0.8 0.2 0.4 0.3 0.6 0.7 0.9 - altitude int64 1000 - Returns ------- Dataset From 3869b78c6d9c55a543457565c8e955513c5eda76 Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Thu, 22 Jun 2023 12:26:31 +0530 Subject: [PATCH 12/30] Revert "xarray.dataset.tail" This reverts commit ee8ba416cc0de808e0f75897f25e9739a1d4bb6f. --- xarray/core/dataset.py | 186 +++++++++++++---------------------------- 1 file changed, 57 insertions(+), 129 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index f7b7ed7cf47..cc20ceff63a 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2862,37 +2862,6 @@ def tail( The keyword arguments form of ``indexers``. One of indexers or indexers_kwargs must be provided. - Example - ------- - - # Sample dataset - >>> data = xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dims=("x", "y")) - >>> dataset = xr.Dataset({"data": data}) - - # Print the original dataset - >>> "Original Dataset:" - >>> dataset - - # Get the last 2 elements using tail() - >>> tail_dataset = dataset.tail(2) - - # Print the tail dataset - >>> "Tail Dataset:" - >>> tail_dataset - 'Original Dataset:' - - Dimensions: (x: 3, y: 3) - Dimensions without coordinates: x, y - Data variables: - data (x, y) int64 1 2 3 4 5 6 7 8 9 - - 'Tail Dataset:' - - Dimensions: (x: 2, y: 2) - Dimensions without coordinates: x, y - Data variables: - data (x, y) int64 5 6 8 9 - See Also -------- Dataset.head @@ -8753,109 +8722,68 @@ def idxmax( def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: """Indices of the minima of the member variables. - If there are multiple minima, the indices of the first one found will be - returned. - - Parameters - ---------- - dim : Hashable, optional - The dimension over which to find the minimum. By default, finds minimum over - all dimensions - for now returning an int for backward compatibility, but - this is deprecated, in future will be an error, since DataArray.argmin will - return a dict with indices for all dimensions, which does not make sense for - a Dataset. - keep_attrs : bool, optional - If True, the attributes (`attrs`) will be copied from the original - object to the new one. If False (default), the new object will be - returned without attributes. - skipna : bool, optional - If True, skip missing values (as marked by NaN). By default, only - skips missing values for float dtypes; other dtypes either do not - have a sentinel missing value (int) or skipna=True has not been - implemented (object, datetime64 or timedelta64). - - Returns - ------- - result : Dataset - - Example - ------- - - # Defined the dataset - >>> dataset = xr.Dataset( - ... { - ... "math_scores": ( - ... ["student", "test"], - ... [[90, 85, 79], [78, 80, 85], [95, 92, 98]], - ... ), - ... "english_scores": ( - ... ["student", "test"], - ... [[88, 80, 92], [75, 95, 79], [93, 96, 78]], - ... ), - ... }, - ... coords={ - ... "student": ["Alice", "Bob", "Charlie"], - ... "test": ["Test 1", "Test 2", "Test 3"], - ... }, - ... ) - - # Indices of the minimum values along the 'student' dimension are calculated - >>> argmin_indices = dataset.argmin(dim="student") + If there are multiple minima, the indices of the first one found will be + returned. - # Print the indices of the minimum values - >>> argmin_indices - - Dimensions: (test: 3) - Coordinates: - * test (test) >> precipitation_data = xr.Dataset( - ... { - ... "precipitation": ( - ... ["time", "location"], - ... [ - ... [5.3, 3.8, 2.1], - ... [2.7, 4.2, 1.5], - ... [3.9, 2.6, 1.9], - ... [2.1, 1.4, 3.2], - ... ], - ... ) - ... }, - ... coords={ - ... "time": pd.date_range(start="2023-06-01", periods=4), - ... "location": ["A", "B", "C"], - ... }, - ... ) + Example + ------- - # Index of the day with the minimum precipitation - >>> min_precipitation_index = precipitation_data[ - ... "precipitation" - ... ].argmin(dim="time") + # Defined the dataset + >>> dataset = xr.Dataset( + ... { + ... "math_scores": ( + ... ["student", "test"], + ... [[90, 85, 79], [78, 80, 85], [95, 92, 98]], + ... ), + ... "english_scores": ( + ... ["student", "test"], + ... [[88, 80, 92], [75, 95, 79], [93, 96, 78]], + ... ), + ... }, + ... coords={ + ... "student": ["Alice", "Bob", "Charlie"], + ... "test": ["Test 1", "Test 2", "Test 3"], + ... }, + ... ) - # Using isel to extract the corresponding data - >>> min_precipitation_data = precipitation_data.isel( - ... time=min_precipitation_index - ... ) + # Indices of the minimum values along the 'student' dimension are calculated + >>> argmin_indices = dataset.argmin(dim="student") - # Print the minimum precipitation data - >>> min_precipitation_data - - Dimensions: (location: 3) - Coordinates: - time (location) datetime64[ns] 2023-06-04 2023-06-04 2023-06-02 - * location (location) >> argmin_indices + + Dimensions: (test: 3) + Coordinates: + * test (test) T_Dataset: ... }, ... ) - # Indices of the maximum values along the 'student' dimension are calculated + # Indices of the minimum values along the 'student' dimension are calculated >>> argmax_indices = dataset.argmax(dim="test") - # Print the indices of the maximum values + # Print the indices of the minimum values >>> argmax_indices Dimensions: (student: 3) From 212c87435e5faafbdfe008be7d2cb8f9cb122931 Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Thu, 22 Jun 2023 12:27:48 +0530 Subject: [PATCH 13/30] Revert "xarray.dataset.head" This reverts commit 82e1161578ce44f24cae2aa5fd6b11a8037c32be. --- xarray/core/dataset.py | 37 ------------------------------------- 1 file changed, 37 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index cc20ceff63a..00b8343c844 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2779,43 +2779,6 @@ def head( The keyword arguments form of ``indexers``. One of indexers or indexers_kwargs must be provided. - Example - ------- - - # Sample Dataset - >>> dataset = xr.Dataset( - ... { - ... "temperature": [25.1, 28.3, 30.5, 27.2, 26.8], - ... "humidity": [60.2, 55.6, 50.3, 58.8, 61.7], - ... }, - ... coords={"time": [1, 2, 3, 4, 5]}, - ... ) - - # Originial Dataset - >>> dataset - - Dimensions: (temperature: 5, humidity: 5, time: 5) - Coordinates: - * temperature (temperature) float64 25.1 28.3 30.5 27.2 26.8 - * humidity (humidity) float64 60.2 55.6 50.3 58.8 61.7 - * time (time) int64 1 2 3 4 5 - Data variables: - *empty* - - # Use head() function to retrieve the first three elements - >>> head_dataset = dataset.head(2) - - # Print the head dataset - >>> head_dataset - - Dimensions: (temperature: 2, humidity: 2, time: 2) - Coordinates: - * temperature (temperature) float64 25.1 28.3 - * humidity (humidity) float64 60.2 55.6 - * time (time) int64 1 2 - Data variables: - *empty* - See Also -------- Dataset.tail From 05c9e6695443c316420c2ffb7de23dfba109950f Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Thu, 22 Jun 2023 12:29:10 +0530 Subject: [PATCH 14/30] Revert "xarray.dataset.dropna" This reverts commit 760aa4a8c45e8b84798684b9327415bbe53dc627. --- xarray/core/dataset.py | 73 ------------------------------------------ 1 file changed, 73 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 00b8343c844..75b97933cab 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -5654,79 +5654,6 @@ def dropna( Which variables to check for missing values. By default, all variables in the dataset are checked. - Example - ------- - - # Sample dataset with missing values - >>> data = { - ... "time": [0, 1, 2, 3], - ... "temperature": [25.0, None, 27.5, 28.0], - ... "humidity": [60.0, 65.0, None, 70.0], - ... } - >>> dataset = xr.Dataset(data) - # Print the original dataset - >>> dataset - - Dimensions: (time: 4, temperature: 4, humidity: 4) - Coordinates: - * time (time) int64 0 1 2 3 - * temperature (temperature) object 25.0 None 27.5 28.0 - * humidity (humidity) object 60.0 65.0 None 70.0 - Data variables: - *empty* - - # Drop rows with any missing values - >>> dataset_dropped_any = dataset.dropna(dim="time", how="any") - # Print the dataset after dropping rows with any missing values - >>> dataset_dropped_any - - Dimensions: (time: 4, temperature: 4, humidity: 4) - Coordinates: - * time (time) int64 0 1 2 3 - * temperature (temperature) object 25.0 None 27.5 28.0 - * humidity (humidity) object 60.0 65.0 None 70.0 - Data variables: - *empty* - - # Drop rows with all missing values - >>> dataset_dropped_all = dataset.dropna(dim="time", how="all") - # Print the dataset after dropping rows with all missing values - >>> dataset_dropped_all - - Dimensions: (time: 0, temperature: 4, humidity: 4) - Coordinates: - * time (time) int64 - * temperature (temperature) object 25.0 None 27.5 28.0 - * humidity (humidity) object 60.0 65.0 None 70.0 - Data variables: - *empty* - - # Drop rows with a threshold of non-missing values - >>> dataset_dropped_thresh = dataset.dropna(dim="time", thresh=2) - # Print the dataset after dropping rows with a threshold of non-missing values - >>> dataset_dropped_thresh - - Dimensions: (time: 0, temperature: 4, humidity: 4) - Coordinates: - * time (time) int64 - * temperature (temperature) object 25.0 None 27.5 28.0 - * humidity (humidity) object 60.0 65.0 None 70.0 - Data variables: - *empty* - - # Drop rows for a subset of variables - >>> dataset_dropped_subset = dataset.dropna(dim="time", subset=["temperature"]) - # Print the dataset after dropping rows for a subset of variables - >>> dataset_dropped_subset - - Dimensions: (time: 4, temperature: 4, humidity: 4) - Coordinates: - * time (time) int64 0 1 2 3 - * temperature (temperature) object 25.0 None 27.5 28.0 - * humidity (humidity) object 60.0 65.0 None 70.0 - Data variables: - *empty* - Returns ------- Dataset From 4ee860363241d762fee1f8cd8a42751eae18845e Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Thu, 22 Jun 2023 12:29:35 +0530 Subject: [PATCH 15/30] Revert "xarray.dataset.ffill" This reverts commit 848a5fd3a598c5c7d8d3ca38f9abebc93b81ce0c. --- xarray/core/dataset.py | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 75b97933cab..5931b803cc4 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -5923,34 +5923,6 @@ def ffill(self: T_Dataset, dim: Hashable, limit: int | None = None) -> T_Dataset than 0 or None for no limit. Must be None or greater than or equal to axis length if filling along chunked axes (dimensions). - Example - ------- - # Create a sample dataset with missing values - >>> time = pd.date_range("2023-01-01", periods=10, freq="D") - >>> data = np.array([1, np.nan, 3, np.nan, 5, 6, np.nan, 8, np.nan, 10]) - >>> dataset = xr.Dataset({"data": (("time",), data)}, coords={"time": time}) - - # Perform forward fill (ffill) on the dataset - >>> filled_dataset = dataset.ffill(dim="time") - - # Print the original dataset - >>> dataset - - Dimensions: (time: 10) - Coordinates: - * time (time) datetime64[ns] 2023-01-01 2023-01-02 ... 2023-01-10 - Data variables: - data (time) float64 1.0 nan 3.0 nan 5.0 6.0 nan 8.0 nan 10.0 - - # Print the filled dataset, fills NaN values by propagating values forward - >>> filled_dataset - - Dimensions: (time: 10) - Coordinates: - * time (time) datetime64[ns] 2023-01-01 2023-01-02 ... 2023-01-10 - Data variables: - data (time) float64 1.0 1.0 3.0 3.0 5.0 6.0 6.0 8.0 8.0 10.0 - Returns ------- Dataset From 95e27d226e7880ced9ade2e096dac8cb39bd85c0 Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Thu, 22 Jun 2023 12:30:04 +0530 Subject: [PATCH 16/30] Revert "xarray.dataset.bfill" This reverts commit eb7514e6f806209f757012ab82f9246b6e4d2cae. --- xarray/core/dataset.py | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 5931b803cc4..4a435233079 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -5949,36 +5949,6 @@ def bfill(self: T_Dataset, dim: Hashable, limit: int | None = None) -> T_Dataset than 0 or None for no limit. Must be None or greater than or equal to axis length if filling along chunked axes (dimensions). - Example - ------- - # Create a sample dataset - >>> data = np.array([[1, 2, np.nan], [4, np.nan, 6], [np.nan, 8, 9]]) - >>> coords = {"x": [0, 1, 2], "y": [0, 1, 2]} - >>> dataset = xr.Dataset({"data": (["x", "y"], data)}, coords=coords) - - # Print the original dataset - >>> dataset - - Dimensions: (x: 3, y: 3) - Coordinates: - * x (x) int64 0 1 2 - * y (y) int64 0 1 2 - Data variables: - data (x, y) float64 1.0 2.0 nan 4.0 nan 6.0 nan 8.0 9.0 - - # Apply backward fill (bfill) along the 'y' dimension - >>> dataset_bfill = dataset.bfill(dim="y") - - # Print the dataset after backward fill - >>> dataset_bfill - - Dimensions: (x: 3, y: 3) - Coordinates: - * x (x) int64 0 1 2 - * y (y) int64 0 1 2 - Data variables: - data (x, y) float64 1.0 2.0 nan 4.0 6.0 6.0 8.0 8.0 9.0 - Returns ------- Dataset From 48116411e53bf223188ee67758b3c074a17d2525 Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Thu, 22 Jun 2023 12:30:33 +0530 Subject: [PATCH 17/30] Revert "xarray.dataset.set_Coords" This reverts commit 5ecfd6380f9096fe8d7b2c0ce4d8befcf2ec3f98. --- xarray/core/dataset.py | 61 ++++++++---------------------------------- 1 file changed, 11 insertions(+), 50 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 4a435233079..cf754966439 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1726,58 +1726,19 @@ def data_vars(self) -> DataVariables: def set_coords(self: T_Dataset, names: Hashable | Iterable[Hashable]) -> T_Dataset: """Given names of one or more variables, set them as coordinates - Parameters - ---------- - names : hashable or iterable of hashable - Name(s) of variables in this dataset to convert into coordinates. - - Example - ------- - - # Sample dataset - >>> data = xr.DataArray( - ... [[1, 2], [3, 4]], - ... dims=("x", "y"), - ... coords={"x": [0, 1], "y": [0, 1]}, - ... name="data", - ... ) - >>> dataset = xr.Dataset(data_vars={"data": data}) - - # Print the dataset before setting coordinates - >>> dataset - - Dimensions: (x: 2, y: 2) - Coordinates: - * x (x) int64 0 1 - * y (y) int64 0 1 - Data variables: - data (x, y) int64 1 2 3 4 - - - # Set "x" and "y" variables as coordinates - >>> dataset_coords = dataset.set_coords(["x", "y"]) - - # Print the dataset after setting coordinates - >>> dataset_coords - - Dimensions: (x: 2, y: 2) - Coordinates: - x (x) int64 0 1 - y (y) int64 0 1 - Data variables: - data (x, y) int64 1 2 3 4 - - In the initial dataset, the "x" and "y" variables are present as dimensions. After calling ``set_coords`` (["x", "y"]), these - variables are converted to coordinates, as shown in the final dataset. + Parameters + ---------- + names : hashable or iterable of hashable + Name(s) of variables in this dataset to convert into coordinates. - Returns - ------- - Dataset + Returns + ------- + Dataset - See Also - -------- - Dataset.swap_dims - Dataset.assign_coords + See Also + -------- + Dataset.swap_dims + Dataset.assign_coords """ # TODO: allow inserting new coordinates with this method, like # DataFrame.set_index? From 20ac818d90f2f400ac70d7c1f8497a43c37a349d Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Tue, 27 Jun 2023 23:42:51 +0530 Subject: [PATCH 18/30] check pre-commit --- xarray/core/dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index cf754966439..437a4512fda 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2486,8 +2486,8 @@ def isel( in this dataset, unless vectorized indexing was triggered by using an array indexer, in which case the data will be a copy. - Example - ------- + Examples + -------- # Defined the dataset >>> dataset = xr.Dataset( From eae0ae9fb859b3c935633edfbe0441526822247e Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Fri, 30 Jun 2023 17:03:12 +0530 Subject: [PATCH 19/30] changes --- xarray/core/dataset.py | 269 ++++++++++++++++++++--------------------- 1 file changed, 128 insertions(+), 141 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 437a4512fda..4ac7100675c 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2488,69 +2488,64 @@ def isel( Examples -------- + # Defined the dataset + >>> dataset = xr.Dataset( + ... { + ... "math_scores": ( + ... ["student", "test"], + ... [[90, 85, 92], [78, 80, 85], [95, 92, 98]], + ... ), + ... "english_scores": ( + ... ["student", "test"], + ... [[88, 90, 92], [75, 82, 79], [93, 96, 91]], + ... ), + ... }, + ... coords={ + ... "student": ["Alice", "Bob", "Charlie"], + ... "test": ["Test 1", "Test 2", "Test 3"], + ... }, + ... ) + + # A specific element from the dataset is selected + >>> second_student_first_test = dataset.isel(student=1, test=0) + + # Print the selected element + >>> second_student_first_test + + Dimensions: () + Coordinates: + student >> dataset = xr.Dataset( - ... { - ... "math_scores": ( - ... ["student", "test"], - ... [[90, 85, 92], [78, 80, 85], [95, 92, 98]], - ... ), - ... "english_scores": ( - ... ["student", "test"], - ... [[88, 90, 92], [75, 82, 79], [93, 96, 91]], - ... ), - ... }, - ... coords={ - ... "student": ["Alice", "Bob", "Charlie"], - ... "test": ["Test 1", "Test 2", "Test 3"], - ... }, - ... ) - - # A specific element from the dataset is selected - >>> second_student_first_test = dataset.isel(student=1, test=0) - - # Print the selected element - >>> second_student_first_test - - Dimensions: () - Coordinates: - student >> slice_of_data = dataset.isel(student=slice(0, 2), test=slice(0, 2)) - - # Print the sliced data - >>> slice_of_data - - Dimensions: (student: 2, test: 2) - Coordinates: + # Indexing with a slice using isel + >>> slice_of_data = dataset.isel(student=slice(0, 2), test=slice(0, 2)) + >>> slice_of_data + + Dimensions: (student: 2, test: 2) + Coordinates: * student (student) >> index_array = xr.DataArray([0, 2], dims="student") + Data variables: + math_scores (student, test) int64 90 85 78 80 + english_scores (student, test) int64 88 90 75 82 - # Use isel with the DataArray for indexing - >>> indexed_data = dataset.isel(student=index_array) + # Create a DataArray for indexing + >>> index_array = xr.DataArray([0, 2], dims="student") - # Print the indexed data - >>> indexed_data - - Dimensions: (student: 2, test: 3) - Coordinates: + # Use isel with the DataArray for indexing + >>> indexed_data = dataset.isel(student=index_array) + >>> indexed_data + + Dimensions: (student: 2, test: 3) + Coordinates: * student (student) T_Dataset: """Reduce this dataset by applying `func` along some dimension(s). - Parameters - ---------- - func : callable - Function which can be called in the form - `f(x, axis=axis, **kwargs)` to return the result of reducing an - np.ndarray over an integer valued axis. - dim : str, Iterable of Hashable or None, optional - Dimension(s) over which to apply `func`. By default `func` is - applied over all dimensions. - keep_attrs : bool or None, optional - If True, the dataset's attributes (`attrs`) will be copied from - the original object to the new one. If False (default), the new - object will be returned without attributes. - keepdims : bool, default: False - If True, the dimensions which are reduced are left in the result - as dimensions of size one. Coordinates that use these dimensions - are removed. - numeric_only : bool, default: False - If True, only apply ``func`` to variables with a numeric dtype. - **kwargs : Any - Additional keyword arguments passed on to ``func``. - - Returns - ------- - reduced : Dataset - Dataset with this object's DataArrays replaced with new DataArrays - of summarized data and the indicated dimension(s) removed. - - Example - ------- - - # Defined the dataset - >>> dataset = xr.Dataset( - ... { - ... "math_scores": ( - ... ["student", "test"], - ... [[90, 85, 92], [78, 80, 85], [95, 92, 98]], - ... ), - ... "english_scores": ( - ... ["student", "test"], - ... [[88, 90, 92], [75, 82, 79], [93, 96, 91]], - ... ), - ... }, - ... coords={ - ... "student": ["Alice", "Bob", "Charlie"], - ... "test": ["Test 1", "Test 2", "Test 3"], - ... }, - ... ) - - # Calculate the 75th percentile of math scores for each student using np.percentile - >>> percentile_math_scores = dataset["math_scores"].reduce( - ... np.percentile, q=75, dim="test" - ... ) - - # Print 75th percentile of math scores - >>> percentile_math_scores - - array([91. , 82.5, 96.5]) - Coordinates: - * student (student) >> dataset = xr.Dataset( + ... { + ... "math_scores": ( + ... ["student", "test"], + ... [[90, 85, 92], [78, 80, 85], [95, 92, 98]], + ... ), + ... "english_scores": ( + ... ["student", "test"], + ... [[88, 90, 92], [75, 82, 79], [93, 96, 91]], + ... ), + ... }, + ... coords={ + ... "student": ["Alice", "Bob", "Charlie"], + ... "test": ["Test 1", "Test 2", "Test 3"], + ... }, + ... ) - Calculating skewness of math scores for each student using the ``scipy.stats.skew`` function using the above mentioned dataset + # Calculate the 75th percentile of math scores for each student using np.percentile + >>> percentile_scores = dataset.reduce(np.percentile, q=75, dim="test") + >>> percentile_cores + + Dimensions: (student: 3) + Coordinates: + * student (student) >> from scipy.stats import skew +Calculating skewness of math scores for each student using the ``scipy.stats.skew`` function using the above mentioned dataset - # Calculate the skewness of math scores for each student using scipy.stats.skew - >>> skewness_math_scores = dataset["math_scores"].reduce( - ... skew, dim="test" - ... ) + # To use the `skew` function, you need to import it from the `scipy.stats` module + >>> from scipy.stats import skew - >>> skewness_math_scores - - array([-0.47033046, 0.47033046, 0. ]) - Coordinates: - * student (student) >> combined_scores = xr.concat([dataset["math_scores"], dataset["english_scores"]], dim="subject") + + # Calculate the skewness of scores for all students + >>> skewness_scores = combined_scores.reduce(skew, dim=("test", "student")) + + >>> skewness_scores + + array([-0.19423043, -0.60125 ]) + Dimensions without coordinates: subject + +Positive skewed value implies that many students scored low with only a few scoring high ( difficult test). +Conversely, a negative skewed value implies that many students scored high with only a few scoring low (an easier test). """ if kwargs.get("axis", None) is not None: @@ -8540,10 +8535,8 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: ------- result : Dataset - Example - ------- - - # Defined the dataset + Examples + -------- >>> dataset = xr.Dataset( ... { ... "math_scores": ( @@ -8563,8 +8556,6 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: # Indices of the minimum values along the 'student' dimension are calculated >>> argmin_indices = dataset.argmin(dim="student") - - # Print the indices of the minimum values >>> argmin_indices Dimensions: (test: 3) @@ -8633,10 +8624,8 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: ------- result : Dataset - Example - ------- - - #Defined the dataset + Examples + -------- >>> dataset = xr.Dataset( ... { ... "math_scores": ( @@ -8654,10 +8643,8 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: ... }, ... ) - # Indices of the minimum values along the 'student' dimension are calculated + # Indices of the maximum values along the 'student' dimension are calculated >>> argmax_indices = dataset.argmax(dim="test") - - # Print the indices of the minimum values >>> argmax_indices Dimensions: (student: 3) From 0a1a558da89569b9d40338f7675c5037cd230e5f Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Fri, 30 Jun 2023 17:42:26 +0530 Subject: [PATCH 20/30] argmin_indices --- xarray/core/dataset.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 4ac7100675c..9116e02d9e1 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2541,8 +2541,8 @@ def isel( Dimensions: (student: 2, test: 3) Coordinates: - * student (student) >> percentile_scores = dataset.reduce(np.percentile, q=75, dim="test") - >>> percentile_cores + >>> percentile_scores Dimensions: (student: 3) Coordinates: @@ -8545,7 +8545,7 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: ... ), ... "english_scores": ( ... ["student", "test"], - ... [[88, 80, 92], [75, 95, 79], [93, 96, 78]], + ... [[88, 90, 92], [75, 82, 79], [39, 96, 78]], ... ), ... }, ... coords={ @@ -8556,17 +8556,19 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: # Indices of the minimum values along the 'student' dimension are calculated >>> argmin_indices = dataset.argmin(dim="student") - >>> argmin_indices - - Dimensions: (test: 3) - Coordinates: - * test (test) >> min_score_in_math = dataset["student"].values[argmin_indices["math_scores"].values] + >>> min_score_in_math + array(['Bob', 'Bob', 'Alice'], dtype='>> min_score_in_english = dataset["student"].values[argmin_indices["english_scores"].values] + >>> min_score_in_english + array(['Charlie', 'Bob', 'Charlie'], dtype=' T_Dataset: Dimensions: (student: 3) Coordinates: - * student (student) Date: Fri, 30 Jun 2023 11:15:15 -0400 Subject: [PATCH 21/30] Try adding blank lines --- xarray/core/dataset.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index ac350e34348..7c04bb92639 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -8639,6 +8639,7 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: Examples -------- + >>> dataset = xr.Dataset( ... { ... "math_scores": ( @@ -8658,6 +8659,7 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: # Indices of the maximum values along the 'student' dimension are calculated >>> argmax_indices = dataset.argmax(dim="test") + >>> argmax_indices Dimensions: (student: 3) From 1ee2c93a762ecb72f814b4e30c02712dc8d72168 Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Fri, 30 Jun 2023 20:46:46 +0530 Subject: [PATCH 22/30] indentation change --- xarray/core/dataset.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index ac350e34348..620109ad8ea 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2537,8 +2537,8 @@ def isel( Dimensions: (student: 2, test: 2) Coordinates: - * student (student) Dimensions: (student: 3) Coordinates: - * student (student) Date: Sat, 1 Jul 2023 00:10:04 +0530 Subject: [PATCH 23/30] indentation --- xarray/core/dataset.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 559347593a9..b47f59a3020 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2499,7 +2499,7 @@ def isel( Examples -------- - # Defined the dataset + >>> dataset = xr.Dataset( ... { ... "math_scores": ( @@ -2516,12 +2516,9 @@ def isel( ... "test": ["Test 1", "Test 2", "Test 3"], ... }, ... ) - # A specific element from the dataset is selected - >>> second_student_first_test = dataset.isel(student=1, test=0) - # Print the selected element - >>> second_student_first_test + >>> dataset.isel(student=1, test=0) Dimensions: () Coordinates: @@ -2532,6 +2529,7 @@ def isel( english_scores int64 75 # Indexing with a slice using isel + >>> slice_of_data = dataset.isel(student=slice(0, 2), test=slice(0, 2)) >>> slice_of_data @@ -2543,10 +2541,7 @@ def isel( math_scores (student, test) int64 90 85 78 80 english_scores (student, test) int64 88 90 75 82 - # Create a DataArray for indexing >>> index_array = xr.DataArray([0, 2], dims="student") - - # Use isel with the DataArray for indexing >>> indexed_data = dataset.isel(student=index_array) >>> indexed_data @@ -5986,6 +5981,7 @@ def reduce( Examples -------- + >>> dataset = xr.Dataset( ... { ... "math_scores": ( @@ -6002,8 +5998,8 @@ def reduce( ... "test": ["Test 1", "Test 2", "Test 3"], ... }, ... ) - # Calculate the 75th percentile of math scores for each student using np.percentile + >>> percentile_scores = dataset.reduce(np.percentile, q=75, dim="test") >>> percentile_scores @@ -6014,17 +6010,17 @@ def reduce( math_scores (student) float64 91.0 82.5 96.5 english_scores (student) float64 91.0 80.5 94.5 -Calculating skewness of math scores for each student using the ``scipy.stats.skew`` function using the above mentioned dataset - # To use the `skew` function, you need to import it from the `scipy.stats` module + >>> from scipy.stats import skew # Combine the scores of both subjects into a single variable + >>> combined_scores = xr.concat([dataset["math_scores"], dataset["english_scores"]], dim="subject") # Calculate the skewness of scores for all students - >>> skewness_scores = combined_scores.reduce(skew, dim=("test", "student")) + >>> skewness_scores = combined_scores.reduce(skew, dim=("test", "student")) >>> skewness_scores array([-0.19423043, -0.60125 ]) @@ -8639,7 +8635,7 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: Examples -------- - + >>> dataset = xr.Dataset( ... { ... "math_scores": ( @@ -8659,7 +8655,7 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: # Indices of the maximum values along the 'student' dimension are calculated >>> argmax_indices = dataset.argmax(dim="test") - + >>> argmax_indices Dimensions: (student: 3) From 1be4f82de35fbc7ef76a3c8975d0f6aebfc57a72 Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Mon, 3 Jul 2023 09:11:54 +0530 Subject: [PATCH 24/30] removed dataset.reduce 'skew' example --- xarray/core/dataset.py | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index b47f59a3020..597a46f9639 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -6009,26 +6009,6 @@ def reduce( Data variables: math_scores (student) float64 91.0 82.5 96.5 english_scores (student) float64 91.0 80.5 94.5 - - # To use the `skew` function, you need to import it from the `scipy.stats` module - - >>> from scipy.stats import skew - - # Combine the scores of both subjects into a single variable - - >>> combined_scores = xr.concat([dataset["math_scores"], dataset["english_scores"]], dim="subject") - - # Calculate the skewness of scores for all students - - >>> skewness_scores = combined_scores.reduce(skew, dim=("test", "student")) - >>> skewness_scores - - array([-0.19423043, -0.60125 ]) - Dimensions without coordinates: subject - -Positive skewed value implies that many students scored low with only a few scoring high ( difficult test). -Conversely, a negative skewed value implies that many students scored high with only a few scoring low (an easier test). - """ if kwargs.get("axis", None) is not None: raise ValueError( From 067a310a08d004b9dd433b57ade86940d88a82da Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Mon, 3 Jul 2023 19:28:51 +0530 Subject: [PATCH 25/30] doctests --- xarray/core/dataset.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 597a46f9639..15a280d59e8 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2516,6 +2516,7 @@ def isel( ... "test": ["Test 1", "Test 2", "Test 3"], ... }, ... ) + # A specific element from the dataset is selected >>> dataset.isel(student=1, test=0) @@ -5998,6 +5999,7 @@ def reduce( ... "test": ["Test 1", "Test 2", "Test 3"], ... }, ... ) + # Calculate the 75th percentile of math scores for each student using np.percentile >>> percentile_scores = dataset.reduce(np.percentile, q=75, dim="test") @@ -8542,16 +8544,24 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: ... ) # Indices of the minimum values along the 'student' dimension are calculated + >>> argmin_indices = dataset.argmin(dim="student") - # Prints student names with minimum scores for each test - >>> min_score_in_math = dataset["student"].values[argmin_indices["math_scores"].values] + >>> min_score_in_math = dataset["student"].isel(student=argmin_indices["math_scores"]) >>> min_score_in_math + array(['Bob', 'Bob', 'Alice'], dtype='>> min_score_in_english = dataset["student"].values[argmin_indices["english_scores"].values] >>> min_score_in_english + array(['Charlie', 'Bob', 'Charlie'], dtype=' Date: Mon, 3 Jul 2023 20:00:09 +0530 Subject: [PATCH 26/30] change --- xarray/core/dataset.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 15a280d59e8..7044489aa21 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -8547,15 +8547,19 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: >>> argmin_indices = dataset.argmin(dim="student") - >>> min_score_in_math = dataset["student"].isel(student=argmin_indices["math_scores"]) + >>> min_score_in_math = dataset["student"].isel( + ... student=argmin_indices["math_scores"] + ... ) >>> min_score_in_math array(['Bob', 'Bob', 'Alice'], dtype='>> min_score_in_english = dataset["student"].values[argmin_indices["english_scores"].values] + >>> min_score_in_english = dataset["student"].values[ + ... argmin_indices["english_scores"].values + ... ] >>> min_score_in_english array(['Charlie', 'Bob', 'Charlie'], dtype=' Date: Mon, 3 Jul 2023 20:15:46 +0530 Subject: [PATCH 27/30] argmin --- xarray/core/dataset.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 7044489aa21..3b6332a6b2a 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -8557,15 +8557,15 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: student (test) >> min_score_in_english = dataset["student"].values[ - ... argmin_indices["english_scores"].values - ... ] + >>> min_score_in_english = dataset["student"].isel( + ... student=argmin_indices["english_scores"] + ... ) >>> min_score_in_english array(['Charlie', 'Bob', 'Charlie'], dtype=' Date: Mon, 3 Jul 2023 22:55:16 +0530 Subject: [PATCH 28/30] Update xarray/core/dataset.py Co-authored-by: Tom Nicholas --- xarray/core/dataset.py | 1 + 1 file changed, 1 insertion(+) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 3b6332a6b2a..0cabe803f7d 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -8648,6 +8648,7 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset: ... ) # Indices of the maximum values along the 'student' dimension are calculated + >>> argmax_indices = dataset.argmax(dim="test") >>> argmax_indices From 4ed93934de18050c1258fd8018cbd17f4b918d25 Mon Sep 17 00:00:00 2001 From: harshitha1201 Date: Mon, 3 Jul 2023 23:27:58 +0530 Subject: [PATCH 29/30] what's new.rst updated --- doc/whats-new.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ce2c0a698ac..adfa62ce912 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -100,6 +100,11 @@ Bug fixes Documentation ~~~~~~~~~~~~~ + +- Added examples to docstrings of :py:meth:`Dataset.isel`, :py:meth:`Dataset.reduce`, :py:meth:`Dataset.argmin`, + :py:meth:`Dataset.argmax` (:issue:`6793`, :pull:`7881`) + By `Harshitha `_ . + Internal Changes ~~~~~~~~~~~~~~~~ From 42a7ceed1f32b60e38f1237ae438a8ac9b580438 Mon Sep 17 00:00:00 2001 From: Tom Nicholas Date: Mon, 3 Jul 2023 17:19:47 -0400 Subject: [PATCH 30/30] Move whatsnew entry to unreleased version --- doc/whats-new.rst | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index adfa62ce912..1667fd2fb9c 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -40,6 +40,10 @@ Documentation - Expanded the page on wrapping numpy-like "duck" arrays. (:pull:`7911`) By `Tom Nicholas `_. +- Added examples to docstrings of :py:meth:`Dataset.isel`, :py:meth:`Dataset.reduce`, :py:meth:`Dataset.argmin`, + :py:meth:`Dataset.argmax` (:issue:`6793`, :pull:`7881`) + By `Harshitha `_ . + Internal Changes ~~~~~~~~~~~~~~~~ @@ -100,11 +104,6 @@ Bug fixes Documentation ~~~~~~~~~~~~~ - -- Added examples to docstrings of :py:meth:`Dataset.isel`, :py:meth:`Dataset.reduce`, :py:meth:`Dataset.argmin`, - :py:meth:`Dataset.argmax` (:issue:`6793`, :pull:`7881`) - By `Harshitha `_ . - Internal Changes ~~~~~~~~~~~~~~~~