From a1087a20f9328a967a143abe5ded6e812d1e112b Mon Sep 17 00:00:00 2001 From: Keewis Date: Fri, 27 Nov 2020 00:08:35 +0100 Subject: [PATCH 1/2] remove all the deprecated and removed inplace parameters --- xarray/core/dataarray.py | 14 +++----------- xarray/core/dataset.py | 26 +++++--------------------- xarray/core/utils.py | 8 -------- 3 files changed, 8 insertions(+), 40 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index b95f681bc79..b3ac11cf905 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -56,7 +56,7 @@ from .indexing import is_fancy_indexer from .merge import PANDAS_TYPES, MergeError, _extract_indexes_from_coords from .options import OPTIONS, _get_keep_attrs -from .utils import Default, ReprObject, _check_inplace, _default, either_dict_or_kwargs +from .utils import Default, ReprObject, _default, either_dict_or_kwargs from .variable import ( IndexVariable, Variable, @@ -778,7 +778,6 @@ def reset_coords( self, names: Union[Iterable[Hashable], Hashable, None] = None, drop: bool = False, - inplace: bool = None, ) -> Union[None, "DataArray", Dataset]: """Given names of coordinates, reset them to become variables. @@ -795,7 +794,6 @@ def reset_coords( ------- Dataset, or DataArray if ``drop == True`` """ - _check_inplace(inplace) if names is None: names = set(self.coords) - set(self.dims) dataset = self.coords.to_dataset().reset_coords(names, drop) @@ -1745,7 +1743,6 @@ def set_index( self, indexes: Mapping[Hashable, Union[Hashable, Sequence[Hashable]]] = None, append: bool = False, - inplace: bool = None, **indexes_kwargs: Union[Hashable, Sequence[Hashable]], ) -> Optional["DataArray"]: """Set DataArray (multi-)indexes using one or more existing @@ -1796,16 +1793,13 @@ def set_index( -------- DataArray.reset_index """ - ds = self._to_temp_dataset().set_index( - indexes, append=append, inplace=inplace, **indexes_kwargs - ) + ds = self._to_temp_dataset().set_index(indexes, append=append, **indexes_kwargs) return self._from_temp_dataset(ds) def reset_index( self, dims_or_levels: Union[Hashable, Sequence[Hashable]], drop: bool = False, - inplace: bool = None, ) -> Optional["DataArray"]: """Reset the specified index(es) or multi-index level(s). @@ -1828,7 +1822,6 @@ def reset_index( -------- DataArray.set_index """ - _check_inplace(inplace) coords, _ = split_indexes( dims_or_levels, self._coords, set(), self._level_coords, drop=drop ) @@ -1837,7 +1830,6 @@ def reset_index( def reorder_levels( self, dim_order: Mapping[Hashable, Sequence[int]] = None, - inplace: bool = None, **dim_order_kwargs: Sequence[int], ) -> "DataArray": """Rearrange index levels using input order. @@ -1858,7 +1850,6 @@ def reorder_levels( Another dataarray, with this dataarray's data but replaced coordinates. """ - _check_inplace(inplace) dim_order = either_dict_or_kwargs(dim_order, dim_order_kwargs, "reorder_levels") replace_coords = {} for dim, order in dim_order.items(): @@ -3496,6 +3487,7 @@ def map_blocks( ... gb = da.groupby(groupby_type) ... clim = gb.mean(dim="time") ... return gb - clim + ... >>> time = xr.cftime_range("1990-01", "1992-01", freq="M") >>> month = xr.DataArray(time.month, coords={"time": time}, dims=["time"]) >>> np.random.seed(123) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 04974c58113..cda7dcd34d7 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -85,7 +85,6 @@ Default, Frozen, SortedKeysDict, - _check_inplace, _default, decode_numpy_dict_values, drop_dims_from_indexers, @@ -1476,9 +1475,7 @@ def data_vars(self) -> DataVariables: """Dictionary of DataArray objects corresponding to data variables""" return DataVariables(self) - def set_coords( - self, names: "Union[Hashable, Iterable[Hashable]]", inplace: bool = None - ) -> "Dataset": + def set_coords(self, names: "Union[Hashable, Iterable[Hashable]]") -> "Dataset": """Given names of one or more variables, set them as coordinates Parameters @@ -1498,7 +1495,6 @@ def set_coords( # DataFrame.set_index? # nb. check in self._variables, not self.data_vars to insure that the # operation is idempotent - _check_inplace(inplace) if isinstance(names, str) or not isinstance(names, Iterable): names = [names] else: @@ -1512,7 +1508,6 @@ def reset_coords( self, names: "Union[Hashable, Iterable[Hashable], None]" = None, drop: bool = False, - inplace: bool = None, ) -> "Dataset": """Given names of coordinates, reset them to become variables @@ -1529,7 +1524,6 @@ def reset_coords( ------- Dataset """ - _check_inplace(inplace) if names is None: names = self._coord_names - set(self.dims) else: @@ -3022,9 +3016,7 @@ def rename_vars( ) return self._replace(variables, coord_names, dims=dims, indexes=indexes) - def swap_dims( - self, dims_dict: Mapping[Hashable, Hashable], inplace: bool = None - ) -> "Dataset": + def swap_dims(self, dims_dict: Mapping[Hashable, Hashable]) -> "Dataset": """Returns a new object with swapped dimensions. Parameters @@ -3083,7 +3075,6 @@ def swap_dims( """ # TODO: deprecate this method in favor of a (less confusing) # rename_dims() method that only renames dimensions. - _check_inplace(inplace) for k, v in dims_dict.items(): if k not in self.dims: raise ValueError( @@ -3258,7 +3249,6 @@ def set_index( self, indexes: Mapping[Hashable, Union[Hashable, Sequence[Hashable]]] = None, append: bool = False, - inplace: bool = None, **indexes_kwargs: Union[Hashable, Sequence[Hashable]], ) -> "Dataset": """Set Dataset (multi-)indexes using one or more existing coordinates @@ -3313,7 +3303,6 @@ def set_index( Dataset.reset_index Dataset.swap_dims """ - _check_inplace(inplace) indexes = either_dict_or_kwargs(indexes, indexes_kwargs, "set_index") variables, coord_names = merge_indexes( indexes, self._variables, self._coord_names, append=append @@ -3324,7 +3313,6 @@ def reset_index( self, dims_or_levels: Union[Hashable, Sequence[Hashable]], drop: bool = False, - inplace: bool = None, ) -> "Dataset": """Reset the specified index(es) or multi-index level(s). @@ -3346,7 +3334,6 @@ def reset_index( -------- Dataset.set_index """ - _check_inplace(inplace) variables, coord_names = split_indexes( dims_or_levels, self._variables, @@ -3359,7 +3346,6 @@ def reset_index( def reorder_levels( self, dim_order: Mapping[Hashable, Sequence[int]] = None, - inplace: bool = None, **dim_order_kwargs: Sequence[int], ) -> "Dataset": """Rearrange index levels using input order. @@ -3380,7 +3366,6 @@ def reorder_levels( Another dataset, with this dataset's data but replaced coordinates. """ - _check_inplace(inplace) dim_order = either_dict_or_kwargs(dim_order, dim_order_kwargs, "reorder_levels") variables = self._variables.copy() indexes = dict(self.indexes) @@ -3683,7 +3668,7 @@ def unstack( result = result._unstack_once(dim, fill_value, sparse) return result - def update(self, other: "CoercibleMapping", inplace: bool = None) -> "Dataset": + def update(self, other: "CoercibleMapping") -> "Dataset": """Update this dataset's variables with those from another dataset. Parameters @@ -3709,14 +3694,12 @@ def update(self, other: "CoercibleMapping", inplace: bool = None) -> "Dataset": If any dimensions would have inconsistent sizes in the updated dataset. """ - _check_inplace(inplace) merge_result = dataset_update_method(self, other) return self._replace(inplace=True, **merge_result._asdict()) def merge( self, other: Union["CoercibleMapping", "DataArray"], - inplace: bool = None, overwrite_vars: Union[Hashable, Iterable[Hashable]] = frozenset(), compat: str = "no_conflicts", join: str = "outer", @@ -3772,7 +3755,6 @@ def merge( MergeError If any variables conflict (see ``compat``). """ - _check_inplace(inplace) other = other.to_dataset() if isinstance(other, xr.DataArray) else other merge_result = dataset_merge_method( self, @@ -5794,6 +5776,7 @@ def filter_by_attrs(self, **kwargs): Examples -------- >>> # Create an example dataset: + ... >>> import numpy as np >>> import pandas as pd >>> import xarray as xr @@ -5974,6 +5957,7 @@ def map_blocks( ... gb = da.groupby(groupby_type) ... clim = gb.mean(dim="time") ... return gb - clim + ... >>> time = xr.cftime_range("1990-01", "1992-01", freq="M") >>> month = xr.DataArray(time.month, coords={"time": time}, dims=["time"]) >>> np.random.seed(123) diff --git a/xarray/core/utils.py b/xarray/core/utils.py index 05e6ee8716b..1a98b24b9b7 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -36,14 +36,6 @@ T = TypeVar("T") -def _check_inplace(inplace: Optional[bool]) -> None: - if inplace is not None: - raise TypeError( - "The `inplace` argument has been removed from xarray. " - "You can achieve an identical effect with python's standard assignment." - ) - - def alias_message(old_name: str, new_name: str) -> str: return f"{old_name} has been deprecated. Use {new_name} instead." From 17b92c76de0e3c51df88704a8f394c1713007c8e Mon Sep 17 00:00:00 2001 From: Keewis Date: Fri, 27 Nov 2020 01:06:45 +0100 Subject: [PATCH 2/2] remove tests checking for inplace to raise TypeError --- xarray/tests/test_dataarray.py | 6 ------ xarray/tests/test_dataset.py | 13 ------------- 2 files changed, 19 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 599584e0081..a8c62a2ce15 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1405,8 +1405,6 @@ def test_reset_coords(self): ) assert_identical(actual, expected) - with pytest.raises(TypeError): - data = data.reset_coords(inplace=True) with raises_regex(ValueError, "cannot be found"): data.reset_coords("foo", drop=True) with raises_regex(ValueError, "cannot be found"): @@ -1871,10 +1869,6 @@ def test_reorder_levels(self): obj = self.mda.reorder_levels(x=["level_2", "level_1"]) assert_identical(obj, expected) - with pytest.raises(TypeError): - array = self.mda.copy() - array.reorder_levels(x=["level_2", "level_1"], inplace=True) - array = DataArray([1, 2], dims="x") with pytest.raises(KeyError): array.reorder_levels(x=["level_1", "level_2"]) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 6c4311c3791..099fb5c0515 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2889,10 +2889,6 @@ def test_set_index(self): obj = ds.set_index(x=mindex.names) assert_identical(obj, expected) - with pytest.raises(TypeError): - ds.set_index(x=mindex.names, inplace=True) - assert_identical(ds, expected) - # ensure set_index with no existing index and a single data var given # doesn't return multi-index ds = Dataset(data_vars={"x_var": ("x", [0, 1, 2])}) @@ -2914,9 +2910,6 @@ def test_reset_index(self): obj = ds.reset_index("x") assert_identical(obj, expected) - with pytest.raises(TypeError): - ds.reset_index("x", inplace=True) - def test_reset_index_keep_attrs(self): coord_1 = DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True}) ds = Dataset({}, {"coord_1": coord_1}) @@ -2933,9 +2926,6 @@ def test_reorder_levels(self): reindexed = ds.reorder_levels(x=["level_2", "level_1"]) assert_identical(reindexed, expected) - with pytest.raises(TypeError): - ds.reorder_levels(x=["level_2", "level_1"], inplace=True) - ds = Dataset({}, coords={"x": [1, 2]}) with raises_regex(ValueError, "has no MultiIndex"): ds.reorder_levels(x=["level_1", "level_2"]) @@ -3133,9 +3123,6 @@ def test_update(self): assert actual_result is actual assert_identical(expected, actual) - with pytest.raises(TypeError): - actual = data.update(data, inplace=False) - other = Dataset(attrs={"new": "attr"}) actual = data.copy() actual.update(other)