From 10fc573a621f6c075e61d32c48b42eda8048760c Mon Sep 17 00:00:00 2001 From: dcherian Date: Sun, 28 Oct 2018 17:38:48 -0700 Subject: [PATCH 1/9] Start deprecating inplace. --- xarray/core/dataarray.py | 15 ++++++++++----- xarray/core/dataset.py | 38 ++++++++++++++++++++++++-------------- xarray/core/utils.py | 8 ++++++++ 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index f131b003a69..625c1269668 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -19,7 +19,8 @@ from .options import OPTIONS from .pycompat import OrderedDict, basestring, iteritems, range, zip from .utils import ( - decode_numpy_dict_values, either_dict_or_kwargs, ensure_us_time_resolution) + _check_inplace, decode_numpy_dict_values, either_dict_or_kwargs, + ensure_us_time_resolution) from .variable import ( IndexVariable, Variable, as_compatible_data, as_variable, assert_unique_multiindex_level_names) @@ -546,7 +547,7 @@ def coords(self): """ return DataArrayCoordinates(self) - def reset_coords(self, names=None, drop=False, inplace=False): + def reset_coords(self, names=None, drop=False, inplace=None): """Given names of coordinates, reset them to become variables. Parameters @@ -565,6 +566,7 @@ def reset_coords(self, names=None, drop=False, inplace=False): ------- Dataset, or DataArray if ``drop == True`` """ + _check_inplace(inplace) if inplace and not drop: raise ValueError('cannot reset coordinates in-place on a ' 'DataArray without ``drop == True``') @@ -1155,7 +1157,7 @@ def expand_dims(self, dim, axis=None): ds = self._to_temp_dataset().expand_dims(dim, axis) return self._from_temp_dataset(ds) - def set_index(self, indexes=None, append=False, inplace=False, + def set_index(self, indexes=None, append=False, inplace=None, **indexes_kwargs): """Set DataArray (multi-)indexes using one or more existing coordinates. @@ -1185,6 +1187,7 @@ def set_index(self, indexes=None, append=False, inplace=False, -------- DataArray.reset_index """ + _check_inplace(inplace) indexes = either_dict_or_kwargs(indexes, indexes_kwargs, 'set_index') coords, _ = merge_indexes(indexes, self._coords, set(), append=append) if inplace: @@ -1192,7 +1195,7 @@ def set_index(self, indexes=None, append=False, inplace=False, else: return self._replace(coords=coords) - def reset_index(self, dims_or_levels, drop=False, inplace=False): + def reset_index(self, dims_or_levels, drop=False, inplace=None): """Reset the specified index(es) or multi-index level(s). Parameters @@ -1217,6 +1220,7 @@ def reset_index(self, dims_or_levels, drop=False, inplace=False): -------- DataArray.set_index """ + _check_inplace(inplace) coords, _ = split_indexes(dims_or_levels, self._coords, set(), self._level_coords, drop=drop) if inplace: @@ -1224,7 +1228,7 @@ def reset_index(self, dims_or_levels, drop=False, inplace=False): else: return self._replace(coords=coords) - def reorder_levels(self, dim_order=None, inplace=False, + def reorder_levels(self, dim_order=None, inplace=None, **dim_order_kwargs): """Rearrange index levels using input order. @@ -1247,6 +1251,7 @@ def reorder_levels(self, dim_order=None, inplace=False, 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 = {} diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 983270cf425..b7fbf52cbfd 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -32,9 +32,9 @@ from .pycompat import ( OrderedDict, basestring, dask_array_type, integer_types, iteritems, range) from .utils import ( - Frozen, SortedKeysDict, datetime_to_numeric, decode_numpy_dict_values, - either_dict_or_kwargs, ensure_us_time_resolution, hashable, - maybe_wrap_array) + _check_inplace, Frozen, SortedKeysDict, datetime_to_numeric, + decode_numpy_dict_values, either_dict_or_kwargs, ensure_us_time_resolution, + hashable, maybe_wrap_array) from .variable import IndexVariable, Variable, as_variable, broadcast_variables # list of attributes of pd.DatetimeIndex that are ndarrays of time info @@ -657,7 +657,7 @@ def _from_vars_and_coord_names(cls, variables, coord_names, attrs=None): return cls._construct_direct(variables, coord_names, dims, attrs) def _replace_vars_and_dims(self, variables, coord_names=None, dims=None, - attrs=__default_attrs, inplace=False): + attrs=__default_attrs, inplace=None): """Fastpath constructor for internal use. Preserves coord names and attributes. If not provided explicitly, @@ -677,6 +677,7 @@ def _replace_vars_and_dims(self, variables, coord_names=None, dims=None, ------- new : Dataset """ + _check_inplace(inplace) if dims is None: dims = calculate_dimensions(variables) if inplace: @@ -1072,7 +1073,7 @@ def data_vars(self): """ return DataVariables(self) - def set_coords(self, names, inplace=False): + def set_coords(self, names, inplace=None): """Given names of one or more variables, set them as coordinates Parameters @@ -1095,6 +1096,7 @@ def set_coords(self, names, inplace=False): # 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, basestring): names = [names] self._assert_all_in_dataset(names) @@ -1102,7 +1104,7 @@ def set_coords(self, names, inplace=False): obj._coord_names.update(names) return obj - def reset_coords(self, names=None, drop=False, inplace=False): + def reset_coords(self, names=None, drop=False, inplace=None): """Given names of coordinates, reset them to become variables Parameters @@ -1121,6 +1123,7 @@ def reset_coords(self, names=None, drop=False, inplace=False): ------- Dataset """ + _check_inplace(inplace) if names is None: names = self._coord_names - set(self.dims) else: @@ -2045,7 +2048,7 @@ def interp_like(self, other, method='linear', assume_sorted=False, ds = self.reindex(object_coords) return ds.interp(numeric_coords, method, assume_sorted, kwargs) - def rename(self, name_dict=None, inplace=False, **names): + def rename(self, name_dict=None, inplace=None, **names): """Returns a new object with renamed variables and dimensions. Parameters @@ -2070,6 +2073,7 @@ def rename(self, name_dict=None, inplace=False, **names): Dataset.swap_dims DataArray.rename """ + _check_inplace(inplace) name_dict = either_dict_or_kwargs(name_dict, names, 'rename') for k, v in name_dict.items(): if k not in self and k not in self.dims: @@ -2095,7 +2099,7 @@ def rename(self, name_dict=None, inplace=False, **names): return self._replace_vars_and_dims(variables, coord_names, dims=dims, inplace=inplace) - def swap_dims(self, dims_dict, inplace=False): + def swap_dims(self, dims_dict, inplace=None): """Returns a new object with swapped dimensions. Parameters @@ -2119,6 +2123,7 @@ def swap_dims(self, dims_dict, inplace=False): Dataset.rename DataArray.swap_dims """ + _check_inplace(inplace) for k, v in dims_dict.items(): if k not in self.dims: raise ValueError('cannot swap from dimension %r because it is ' @@ -2231,7 +2236,7 @@ def expand_dims(self, dim, axis=None): return self._replace_vars_and_dims(variables, self._coord_names) - def set_index(self, indexes=None, append=False, inplace=False, + def set_index(self, indexes=None, append=False, inplace=None, **indexes_kwargs): """Set Dataset (multi-)indexes using one or more existing coordinates or variables. @@ -2262,6 +2267,7 @@ def set_index(self, indexes=None, append=False, inplace=False, 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, @@ -2269,7 +2275,7 @@ def set_index(self, indexes=None, append=False, inplace=False, return self._replace_vars_and_dims(variables, coord_names=coord_names, inplace=inplace) - def reset_index(self, dims_or_levels, drop=False, inplace=False): + def reset_index(self, dims_or_levels, drop=False, inplace=None): """Reset the specified index(es) or multi-index level(s). Parameters @@ -2293,13 +2299,14 @@ def reset_index(self, dims_or_levels, drop=False, inplace=False): -------- Dataset.set_index """ + _check_inplace(inplace) variables, coord_names = split_indexes(dims_or_levels, self._variables, self._coord_names, self._level_coords, drop=drop) return self._replace_vars_and_dims(variables, coord_names=coord_names, inplace=inplace) - def reorder_levels(self, dim_order=None, inplace=False, + def reorder_levels(self, dim_order=None, inplace=None, **dim_order_kwargs): """Rearrange index levels using input order. @@ -2322,6 +2329,7 @@ def reorder_levels(self, dim_order=None, inplace=False, 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') replace_variables = {} @@ -2494,12 +2502,13 @@ def update(self, other, inplace=True): If any dimensions would have inconsistent sizes in the updated dataset. """ + _check_inplace(inplace) variables, coord_names, dims = dataset_update_method(self, other) return self._replace_vars_and_dims(variables, coord_names, dims, inplace=inplace) - def merge(self, other, inplace=False, overwrite_vars=frozenset(), + def merge(self, other, inplace=None, overwrite_vars=frozenset(), compat='no_conflicts', join='outer'): """Merge the arrays of two datasets into a single dataset. @@ -2550,6 +2559,7 @@ def merge(self, other, inplace=False, overwrite_vars=frozenset(), MergeError If any variables conflict (see ``compat``). """ + _check_inplace(inplace) variables, coord_names, dims = dataset_merge_method( self, other, overwrite_vars=overwrite_vars, compat=compat, join=join) @@ -3311,8 +3321,8 @@ def func(self, other): return func def _calculate_binary_op(self, f, other, join='inner', - inplace=False): - + inplace=None): + _check_inplace(inplace) def apply_over_both(lhs_data_vars, rhs_data_vars, lhs_vars, rhs_vars): if inplace and set(lhs_data_vars) != set(rhs_data_vars): raise ValueError('datasets must have the same data variables ' diff --git a/xarray/core/utils.py b/xarray/core/utils.py index 5c9d8bfbf77..15b180a3e26 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -18,6 +18,14 @@ OrderedDict, basestring, bytes_type, dask_array_type, iteritems) +def _check_inplace(inplace): + if inplace is None: + inplace = False + else: + warnings.warn('The inplace argument will be removed in' + ' xarray 0.12.0.', FutureWarning) + + def alias_message(old_name, new_name): return '%s has been deprecated. Use %s instead.' % (old_name, new_name) From edf1a0a33b7b405283c045c65924039c87ced8ca Mon Sep 17 00:00:00 2001 From: dcherian Date: Sun, 28 Oct 2018 17:58:43 -0700 Subject: [PATCH 2/9] remove warnings from tests. --- xarray/core/dataset.py | 2 +- xarray/core/utils.py | 4 ++-- xarray/tests/test_dataarray.py | 1 + xarray/tests/test_dataset.py | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index b7fbf52cbfd..986dbd80402 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2480,7 +2480,7 @@ def unstack(self, dim=None): result = result._unstack_once(dim) return result - def update(self, other, inplace=True): + def update(self, other, inplace=None): """Update this dataset's variables with those from another dataset. Parameters diff --git a/xarray/core/utils.py b/xarray/core/utils.py index 15b180a3e26..e5971cf0d2f 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -22,8 +22,8 @@ def _check_inplace(inplace): if inplace is None: inplace = False else: - warnings.warn('The inplace argument will be removed in' - ' xarray 0.12.0.', FutureWarning) + warnings.warn('The inplace argument has been deprecated and will be ' + 'removed in xarray 0.12.0.', FutureWarning) def alias_message(old_name, new_name): diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index e49b6cdf517..1d70de66d7a 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -23,6 +23,7 @@ requires_scipy, source_ndarray) +@pytest.mark.filterwarnings('ignore:.*inplace argument.*:FutureWarning') class TestDataArray(object): @pytest.fixture(autouse=True) def setup(self): diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index aa226ff1ce8..3062a81fe1f 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -86,6 +86,7 @@ def lazy_inaccessible(k, v): k, v in iteritems(self._variables)) +@pytest.mark.filterwarnings('ignore:The inplace argument') class TestDataset(object): def test_repr(self): data = create_test_data(seed=123) From 3a5cf5d658e1be7f19caae3577b6421346d4570b Mon Sep 17 00:00:00 2001 From: dcherian Date: Sun, 28 Oct 2018 20:50:51 -0700 Subject: [PATCH 3/9] this commit silences nearly all warnings. --- xarray/core/dataset.py | 7 +++---- xarray/tests/test_dataarray.py | 2 +- xarray/tests/test_plot.py | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 986dbd80402..d88e7af47ca 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -657,7 +657,7 @@ def _from_vars_and_coord_names(cls, variables, coord_names, attrs=None): return cls._construct_direct(variables, coord_names, dims, attrs) def _replace_vars_and_dims(self, variables, coord_names=None, dims=None, - attrs=__default_attrs, inplace=None): + attrs=__default_attrs, inplace=False): """Fastpath constructor for internal use. Preserves coord names and attributes. If not provided explicitly, @@ -677,7 +677,6 @@ def _replace_vars_and_dims(self, variables, coord_names=None, dims=None, ------- new : Dataset """ - _check_inplace(inplace) if dims is None: dims = calculate_dimensions(variables) if inplace: @@ -2480,7 +2479,7 @@ def unstack(self, dim=None): result = result._unstack_once(dim) return result - def update(self, other, inplace=None): + def update(self, other, inplace=True): """Update this dataset's variables with those from another dataset. Parameters @@ -2502,7 +2501,7 @@ def update(self, other, inplace=None): If any dimensions would have inconsistent sizes in the updated dataset. """ - _check_inplace(inplace) + # _check_inplace(inplace) variables, coord_names, dims = dataset_update_method(self, other) return self._replace_vars_and_dims(variables, coord_names, dims, diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 1d70de66d7a..3cb4e5d6cea 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -23,7 +23,7 @@ requires_scipy, source_ndarray) -@pytest.mark.filterwarnings('ignore:.*inplace argument.*:FutureWarning') +@pytest.mark.filterwarnings('ignore:The inplace argument') class TestDataArray(object): @pytest.fixture(autouse=True) def setup(self): diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 38cf68e47cc..10c4283032d 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -797,7 +797,7 @@ def setUp(self): x, y = np.meshgrid(da.x.values, da.y.values) ds['x2d'] = DataArray(x, dims=['y', 'x']) ds['y2d'] = DataArray(y, dims=['y', 'x']) - ds.set_coords(['x2d', 'y2d'], inplace=True) + ds = ds.set_coords(['x2d', 'y2d']) # set darray and plot method self.darray = ds.testvar From b1ed4df78adf29f5ad38b29c6b34ed88cb7a9050 Mon Sep 17 00:00:00 2001 From: dcherian Date: Sun, 28 Oct 2018 20:58:32 -0700 Subject: [PATCH 4/9] Add whats-new. --- doc/whats-new.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 4497c57e5f2..e02076120a6 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -58,6 +58,9 @@ Documentation without dimension argument will change in the next release. Now we warn a FutureWarning. By `Keisuke Fujii `_. +- The ``inplace`` kwarg of a number of `DataArray` and `Dataset` methods is being + deprecated and will be removed in the next release. + By `Deepak Cherian `_. Enhancements ~~~~~~~~~~~~ From 484d71fb58b425c0b2bf50ec0397dde069216794 Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 29 Oct 2018 08:42:55 -0700 Subject: [PATCH 5/9] Add a default kwarg to _check_inplace and use for Dataset.update. --- xarray/core/dataset.py | 4 ++-- xarray/core/utils.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index d88e7af47ca..36d6cc19921 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2479,7 +2479,7 @@ def unstack(self, dim=None): result = result._unstack_once(dim) return result - def update(self, other, inplace=True): + def update(self, other, inplace=None): """Update this dataset's variables with those from another dataset. Parameters @@ -2501,7 +2501,7 @@ def update(self, other, inplace=True): If any dimensions would have inconsistent sizes in the updated dataset. """ - # _check_inplace(inplace) + _check_inplace(inplace, default=True) variables, coord_names, dims = dataset_update_method(self, other) return self._replace_vars_and_dims(variables, coord_names, dims, diff --git a/xarray/core/utils.py b/xarray/core/utils.py index e5971cf0d2f..89279b9e0dd 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -18,9 +18,9 @@ OrderedDict, basestring, bytes_type, dask_array_type, iteritems) -def _check_inplace(inplace): +def _check_inplace(inplace, default=False): if inplace is None: - inplace = False + inplace = default else: warnings.warn('The inplace argument has been deprecated and will be ' 'removed in xarray 0.12.0.', FutureWarning) From 63d4175fbda2078300b45ab49450f0f9b158c968 Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 29 Oct 2018 08:49:50 -0700 Subject: [PATCH 6/9] Major fix! --- xarray/core/dataarray.py | 8 ++++---- xarray/core/dataset.py | 20 ++++++++++---------- xarray/core/utils.py | 2 ++ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 625c1269668..d1e0516d756 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -566,7 +566,7 @@ def reset_coords(self, names=None, drop=False, inplace=None): ------- Dataset, or DataArray if ``drop == True`` """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) if inplace and not drop: raise ValueError('cannot reset coordinates in-place on a ' 'DataArray without ``drop == True``') @@ -1187,7 +1187,7 @@ def set_index(self, indexes=None, append=False, inplace=None, -------- DataArray.reset_index """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) indexes = either_dict_or_kwargs(indexes, indexes_kwargs, 'set_index') coords, _ = merge_indexes(indexes, self._coords, set(), append=append) if inplace: @@ -1220,7 +1220,7 @@ def reset_index(self, dims_or_levels, drop=False, inplace=None): -------- DataArray.set_index """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) coords, _ = split_indexes(dims_or_levels, self._coords, set(), self._level_coords, drop=drop) if inplace: @@ -1251,7 +1251,7 @@ def reorder_levels(self, dim_order=None, inplace=None, Another dataarray, with this dataarray's data but replaced coordinates. """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) dim_order = either_dict_or_kwargs(dim_order, dim_order_kwargs, 'reorder_levels') replace_coords = {} diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 36d6cc19921..35f3077a6d4 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1095,7 +1095,7 @@ def set_coords(self, names, inplace=None): # DataFrame.set_index? # nb. check in self._variables, not self.data_vars to insure that the # operation is idempotent - _check_inplace(inplace) + inplace = _check_inplace(inplace) if isinstance(names, basestring): names = [names] self._assert_all_in_dataset(names) @@ -1122,7 +1122,7 @@ def reset_coords(self, names=None, drop=False, inplace=None): ------- Dataset """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) if names is None: names = self._coord_names - set(self.dims) else: @@ -2072,7 +2072,7 @@ def rename(self, name_dict=None, inplace=None, **names): Dataset.swap_dims DataArray.rename """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) name_dict = either_dict_or_kwargs(name_dict, names, 'rename') for k, v in name_dict.items(): if k not in self and k not in self.dims: @@ -2122,7 +2122,7 @@ def swap_dims(self, dims_dict, inplace=None): Dataset.rename DataArray.swap_dims """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) for k, v in dims_dict.items(): if k not in self.dims: raise ValueError('cannot swap from dimension %r because it is ' @@ -2266,7 +2266,7 @@ def set_index(self, indexes=None, append=False, inplace=None, Dataset.reset_index Dataset.swap_dims """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) indexes = either_dict_or_kwargs(indexes, indexes_kwargs, 'set_index') variables, coord_names = merge_indexes(indexes, self._variables, self._coord_names, @@ -2298,7 +2298,7 @@ def reset_index(self, dims_or_levels, drop=False, inplace=None): -------- Dataset.set_index """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) variables, coord_names = split_indexes(dims_or_levels, self._variables, self._coord_names, self._level_coords, drop=drop) @@ -2328,7 +2328,7 @@ def reorder_levels(self, dim_order=None, inplace=None, Another dataset, with this dataset's data but replaced coordinates. """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) dim_order = either_dict_or_kwargs(dim_order, dim_order_kwargs, 'reorder_levels') replace_variables = {} @@ -2501,7 +2501,7 @@ def update(self, other, inplace=None): If any dimensions would have inconsistent sizes in the updated dataset. """ - _check_inplace(inplace, default=True) + inplace = _check_inplace(inplace, default=True) variables, coord_names, dims = dataset_update_method(self, other) return self._replace_vars_and_dims(variables, coord_names, dims, @@ -2558,7 +2558,7 @@ def merge(self, other, inplace=None, overwrite_vars=frozenset(), MergeError If any variables conflict (see ``compat``). """ - _check_inplace(inplace) + inplace = _check_inplace(inplace) variables, coord_names, dims = dataset_merge_method( self, other, overwrite_vars=overwrite_vars, compat=compat, join=join) @@ -3321,7 +3321,7 @@ def func(self, other): def _calculate_binary_op(self, f, other, join='inner', inplace=None): - _check_inplace(inplace) + inplace = _check_inplace(inplace) def apply_over_both(lhs_data_vars, rhs_data_vars, lhs_vars, rhs_vars): if inplace and set(lhs_data_vars) != set(rhs_data_vars): raise ValueError('datasets must have the same data variables ' diff --git a/xarray/core/utils.py b/xarray/core/utils.py index 89279b9e0dd..50180ecbce1 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -25,6 +25,8 @@ def _check_inplace(inplace, default=False): warnings.warn('The inplace argument has been deprecated and will be ' 'removed in xarray 0.12.0.', FutureWarning) + return inplace + def alias_message(old_name, new_name): return '%s has been deprecated. Use %s instead.' % (old_name, new_name) From 66d3ceab2acb122041d651646b920805c26cfbf9 Mon Sep 17 00:00:00 2001 From: dcherian Date: Tue, 30 Oct 2018 10:14:08 -0700 Subject: [PATCH 7/9] Add stacklevel --- xarray/core/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/utils.py b/xarray/core/utils.py index 50180ecbce1..85e1cc59522 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -23,7 +23,7 @@ def _check_inplace(inplace, default=False): inplace = default else: warnings.warn('The inplace argument has been deprecated and will be ' - 'removed in xarray 0.12.0.', FutureWarning) + 'removed in xarray 0.12.0.', FutureWarning, stacklevel=3) return inplace From 62faebc4eef7e8ffb2c3e6a35d318e9602956e04 Mon Sep 17 00:00:00 2001 From: dcherian Date: Wed, 31 Oct 2018 11:13:21 -0700 Subject: [PATCH 8/9] Tests: Less aggressive warning filter + fix unnecessary inplace. --- xarray/tests/test_dataarray.py | 19 ++++++++++--------- xarray/tests/test_dataset.py | 34 +++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 48524bf38c5..3d3d9335f7c 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -23,7 +23,6 @@ requires_scipy, source_ndarray) -@pytest.mark.filterwarnings('ignore:The inplace argument') class TestDataArray(object): @pytest.fixture(autouse=True) def setup(self): @@ -1156,7 +1155,7 @@ def test_reset_coords(self): assert_identical(actual, expected) actual = data.copy() - actual.reset_coords(drop=True, inplace=True) + actual = actual.reset_coords(drop=True) assert_identical(actual, expected) actual = data.reset_coords('bar', drop=True) @@ -1165,8 +1164,9 @@ def test_reset_coords(self): dims=['x', 'y'], name='foo') assert_identical(actual, expected) - with raises_regex(ValueError, 'cannot reset coord'): - data.reset_coords(inplace=True) + with pytest.warns(FutureWarning, message='The inplace argument'): + with raises_regex(ValueError, 'cannot reset coord'): + 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'): @@ -1399,7 +1399,7 @@ def test_set_index(self): expected = array.set_index(x=['level_1', 'level_2', 'level_3']) assert_identical(obj, expected) - array.set_index(x=['level_1', 'level_2', 'level_3'], inplace=True) + array = array.set_index(x=['level_1', 'level_2', 'level_3']) assert_identical(array, expected) array2d = DataArray(np.random.rand(2, 2), @@ -1432,7 +1432,7 @@ def test_reset_index(self): assert_identical(obj, expected) array = self.mda.copy() - array.reset_index(['x'], drop=True, inplace=True) + array = array.reset_index(['x'], drop=True) assert_identical(array, expected) # single index @@ -1448,9 +1448,10 @@ def test_reorder_levels(self): obj = self.mda.reorder_levels(x=['level_2', 'level_1']) assert_identical(obj, expected) - array = self.mda.copy() - array.reorder_levels(x=['level_2', 'level_1'], inplace=True) - assert_identical(array, expected) + with pytest.warns(FutureWarning, message='The inplace argument'): + array = self.mda.copy() + array.reorder_levels(x=['level_2', 'level_1'], inplace=True) + assert_identical(array, expected) array = DataArray([1, 2], dims='x') with pytest.raises(KeyError): diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 3062a81fe1f..06291a2e9b9 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -86,7 +86,6 @@ def lazy_inaccessible(k, v): k, v in iteritems(self._variables)) -@pytest.mark.filterwarnings('ignore:The inplace argument') class TestDataset(object): def test_repr(self): data = create_test_data(seed=123) @@ -1966,6 +1965,7 @@ def test_rename_same_name(self): renamed = data.rename(newnames) assert_identical(renamed, data) + @pytest.mark.filterwarnings('ignore:The inplace argument') def test_rename_inplace(self): times = pd.date_range('2000-01-01', periods=3) data = Dataset({'z': ('x', [2, 3, 4]), 't': ('t', times)}) @@ -1991,7 +1991,7 @@ def test_swap_dims(self): assert_identical(original.set_coords('y'), roundtripped) actual = original.copy() - actual.swap_dims({'x': 'y'}, inplace=True) + actual = actual.swap_dims({'x': 'y'}) assert_identical(expected, actual) with raises_regex(ValueError, 'cannot swap'): @@ -2013,7 +2013,7 @@ def test_expand_dims_error(self): # Make sure it raises true error also for non-dimensional coordinates # which has dimension. - original.set_coords('z', inplace=True) + original = original.set_coords('z') with raises_regex(ValueError, 'already exists'): original.expand_dims(dim=['z']) @@ -2060,8 +2060,9 @@ def test_set_index(self): obj = ds.set_index(x=mindex.names) assert_identical(obj, expected) - ds.set_index(x=mindex.names, inplace=True) - assert_identical(ds, expected) + with pytest.warns(FutureWarning, message='The inplace argument'): + 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 @@ -2079,8 +2080,9 @@ def test_reset_index(self): obj = ds.reset_index('x') assert_identical(obj, expected) - ds.reset_index('x', inplace=True) - assert_identical(ds, expected) + with pytest.warns(FutureWarning, message='The inplace argument'): + ds.reset_index('x', inplace=True) + assert_identical(ds, expected) def test_reorder_levels(self): ds = create_test_multiindex() @@ -2091,8 +2093,9 @@ def test_reorder_levels(self): reindexed = ds.reorder_levels(x=['level_2', 'level_1']) assert_identical(reindexed, expected) - ds.reorder_levels(x=['level_2', 'level_1'], inplace=True) - assert_identical(ds, expected) + with pytest.warns(FutureWarning, message='The inplace argument'): + ds.reorder_levels(x=['level_2', 'level_1'], inplace=True) + assert_identical(ds, expected) ds = Dataset({}, coords={'x': [1, 2]}) with raises_regex(ValueError, 'has no MultiIndex'): @@ -2170,14 +2173,15 @@ def test_update(self): assert_identical(expected, actual) actual = data.copy() - actual_result = actual.update(data, inplace=True) + actual_result = actual.update(data) assert actual_result is actual assert_identical(expected, actual) - actual = data.update(data, inplace=False) - expected = data - assert actual is not expected - assert_identical(expected, actual) + with pytest.warns(FutureWarning, message='The inplace argument'): + actual = data.update(data, inplace=False) + expected = data + assert actual is not expected + assert_identical(expected, actual) other = Dataset(attrs={'new': 'attr'}) actual = data.copy() @@ -2557,7 +2561,7 @@ def get_args(v): return [set(args[0]) & set(v.dims)] if args else [] expected = Dataset(dict((k, v.squeeze(*get_args(v))) for k, v in iteritems(data.variables))) - expected.set_coords(data.coords, inplace=True) + expected = expected.set_coords(data.coords) assert_identical(expected, data.squeeze(*args)) # invalid squeeze with raises_regex(ValueError, 'cannot select a dimension'): From 7a0d37b53dd5281918c78c8a64f8072c8af8e7e9 Mon Sep 17 00:00:00 2001 From: dcherian Date: Wed, 31 Oct 2018 17:20:04 -0700 Subject: [PATCH 9/9] revert changes to _calculate_binary_op --- xarray/core/dataset.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 1f88ebaef70..4f9c61b3269 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -3325,8 +3325,7 @@ def func(self, other): return func def _calculate_binary_op(self, f, other, join='inner', - inplace=None): - inplace = _check_inplace(inplace) + inplace=False): def apply_over_both(lhs_data_vars, rhs_data_vars, lhs_vars, rhs_vars): if inplace and set(lhs_data_vars) != set(rhs_data_vars): raise ValueError('datasets must have the same data variables '