From a7cb9f22607c16ff6544975170a5d69e4d2e51ea Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 7 Oct 2023 18:19:18 -0700 Subject: [PATCH 01/10] Rename `reset_encoding` to `drop_encoding` Closes #8259 --- xarray/core/dataarray.py | 6 +++++- xarray/core/dataset.py | 6 +++++- xarray/core/variable.py | 4 ++++ xarray/tests/test_dataarray.py | 4 ++-- xarray/tests/test_dataset.py | 4 ++-- xarray/tests/test_variable.py | 6 +++--- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 2bcc5ab85e2..c95684e0f14 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -914,9 +914,13 @@ def encoding(self, value: Mapping[Any, Any]) -> None: self.variable.encoding = dict(value) def reset_encoding(self) -> Self: + warnings.warn("reset_encoding is deprecated, use `drop_encoding` instead") + self.drop_encoding() + + def drop_encoding(self) -> Self: """Return a new DataArray without encoding on the array or any attached coords.""" - ds = self._to_temp_dataset().reset_encoding() + ds = self._to_temp_dataset().drop_encoding() return self._from_temp_dataset(ds) @property diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index e49c981b827..7d5ac6e77fd 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -756,9 +756,13 @@ def encoding(self, value: Mapping[Any, Any]) -> None: self._encoding = dict(value) def reset_encoding(self) -> Self: + warnings.warn("reset_encoding is deprecated, use `drop_encoding` instead") + self.drop_encoding() + + def drop_encoding(self) -> Self: """Return a new Dataset without encoding on the dataset or any of its variables/coords.""" - variables = {k: v.reset_encoding() for k, v in self.variables.items()} + variables = {k: v.drop_encoding() for k, v in self.variables.items()} return self._replace(variables=variables, encoding={}) @property diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 3baecfe5f6d..47e8f6016e3 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -883,6 +883,10 @@ def encoding(self, value): raise ValueError("encoding must be castable to a dictionary") def reset_encoding(self) -> Self: + warnings.warn("reset_encoding is deprecated, use `drop_encoding` instead") + self.drop_encoding() + + def drop_encoding(self) -> Self: """Return a new Variable without encoding.""" return self._replace(encoding={}) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index d497cd5a54d..5eb5394d58e 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -287,7 +287,7 @@ def test_encoding(self) -> None: self.dv.encoding = expected2 assert expected2 is not self.dv.encoding - def test_reset_encoding(self) -> None: + def test_drop_encoding(self) -> None: array = self.mda encoding = {"scale_factor": 10} array.encoding = encoding @@ -296,7 +296,7 @@ def test_reset_encoding(self) -> None: assert array.encoding == encoding assert array["x"].encoding == encoding - actual = array.reset_encoding() + actual = array.drop_encoding() # did not modify in place assert array.encoding == encoding diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 3841398ff75..880c958e8ed 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2959,7 +2959,7 @@ def test_copy_with_data_errors(self) -> None: with pytest.raises(ValueError, match=r"contain all variables in original"): orig.copy(data={"var1": new_var1}) - def test_reset_encoding(self) -> None: + def test_drop_encoding(self) -> None: orig = create_test_data() vencoding = {"scale_factor": 10} orig.encoding = {"foo": "bar"} @@ -2967,7 +2967,7 @@ def test_reset_encoding(self) -> None: for k, v in orig.variables.items(): orig[k].encoding = vencoding - actual = orig.reset_encoding() + actual = orig.drop_encoding() assert actual.encoding == {} for k, v in actual.variables.items(): assert v.encoding == {} diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 1ffd51f4a04..73238b6ae3a 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -473,12 +473,12 @@ def test_encoding_preserved(self): assert_identical(expected.to_base_variable(), actual.to_base_variable()) assert expected.encoding == actual.encoding - def test_reset_encoding(self) -> None: + def test_drop_encoding(self) -> None: encoding1 = {"scale_factor": 1} # encoding set via cls constructor v1 = self.cls(["a"], [0, 1, 2], encoding=encoding1) assert v1.encoding == encoding1 - v2 = v1.reset_encoding() + v2 = v1.drop_encoding() assert v1.encoding == encoding1 assert v2.encoding == {} @@ -486,7 +486,7 @@ def test_reset_encoding(self) -> None: encoding3 = {"scale_factor": 10} v3 = self.cls(["a"], [0, 1, 2], encoding=encoding3) assert v3.encoding == encoding3 - v4 = v3.reset_encoding() + v4 = v3.drop_encoding() assert v3.encoding == encoding3 assert v4.encoding == {} From 6cd879ab93f716743ddf5c0dfd8845b00f0d7eeb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 01:19:59 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/core/dataarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index c95684e0f14..c40db653b1b 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -916,7 +916,7 @@ def encoding(self, value: Mapping[Any, Any]) -> None: def reset_encoding(self) -> Self: warnings.warn("reset_encoding is deprecated, use `drop_encoding` instead") self.drop_encoding() - + def drop_encoding(self) -> Self: """Return a new DataArray without encoding on the array or any attached coords.""" From 6add7dca587905f99c590c28d6bba51d2e01bc2a Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 7 Oct 2023 18:22:16 -0700 Subject: [PATCH 03/10] --- doc/whats-new.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 92acc3f90c0..4d2f89411ec 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -42,7 +42,12 @@ Breaking changes Deprecations ~~~~~~~~~~~~ - +- Rename :py:meth:`Dataset.reset_encoding` & :py:meth:`DataArray.reset_encoding` + to :py:meth:`Dataset.drop_encoding` & :py:meth:`DataArray.drop_encoding` for + consistency with other ``drop`` & ``reset`` methods — ``drop`` generally + removes something, while ``reset`` generally resets to some default or + standard value. (:pull:`8287`, :issue:`8259`) + By `Maximilian Roos `_. Bug fixes ~~~~~~~~~ From 297e4663d7534398026e1d35bd9fd6cf2496c7c1 Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Mon, 9 Oct 2023 06:30:32 -0700 Subject: [PATCH 04/10] Update dataarray.py Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com> --- xarray/core/dataarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index c40db653b1b..30d25c63cc6 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -914,7 +914,7 @@ def encoding(self, value: Mapping[Any, Any]) -> None: self.variable.encoding = dict(value) def reset_encoding(self) -> Self: - warnings.warn("reset_encoding is deprecated, use `drop_encoding` instead") + warnings.warn("reset_encoding is deprecated since 2023.11, use `drop_encoding` instead") self.drop_encoding() def drop_encoding(self) -> Self: From 39982dc0380bdcee08f0282cc9982017657f10c1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Oct 2023 13:31:21 +0000 Subject: [PATCH 05/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/core/dataarray.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 30d25c63cc6..7a071d85a7c 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -914,7 +914,9 @@ def encoding(self, value: Mapping[Any, Any]) -> None: self.variable.encoding = dict(value) def reset_encoding(self) -> Self: - warnings.warn("reset_encoding is deprecated since 2023.11, use `drop_encoding` instead") + warnings.warn( + "reset_encoding is deprecated since 2023.11, use `drop_encoding` instead" + ) self.drop_encoding() def drop_encoding(self) -> Self: From f16b3551324449dea51fa7629baa162c3630d3d1 Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Wed, 11 Oct 2023 22:33:52 -0700 Subject: [PATCH 06/10] Update xarray/core/dataarray.py Co-authored-by: Michael Niklas --- xarray/core/dataarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 7a071d85a7c..aaecece4bc0 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -917,7 +917,7 @@ def reset_encoding(self) -> Self: warnings.warn( "reset_encoding is deprecated since 2023.11, use `drop_encoding` instead" ) - self.drop_encoding() + return self.drop_encoding() def drop_encoding(self) -> Self: """Return a new DataArray without encoding on the array or any attached From 3d49e0c1fcb62642f2bd995bc2b46fc7af3820fd Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Wed, 11 Oct 2023 22:33:58 -0700 Subject: [PATCH 07/10] Update xarray/core/variable.py Co-authored-by: Michael Niklas --- xarray/core/variable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 47e8f6016e3..9e2451af91b 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -884,7 +884,7 @@ def encoding(self, value): def reset_encoding(self) -> Self: warnings.warn("reset_encoding is deprecated, use `drop_encoding` instead") - self.drop_encoding() + return self.drop_encoding() def drop_encoding(self) -> Self: """Return a new Variable without encoding.""" From 5570744889974f937cf41f7d47c701c51736debd Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Wed, 11 Oct 2023 22:34:19 -0700 Subject: [PATCH 08/10] Update xarray/core/dataset.py Co-authored-by: Michael Niklas --- xarray/core/dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 7d5ac6e77fd..c670878615a 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -757,7 +757,7 @@ def encoding(self, value: Mapping[Any, Any]) -> None: def reset_encoding(self) -> Self: warnings.warn("reset_encoding is deprecated, use `drop_encoding` instead") - self.drop_encoding() + return self.drop_encoding() def drop_encoding(self) -> Self: """Return a new Dataset without encoding on the dataset or any of its From fd184a9686c453f137b31084ebe4ba768c1b0290 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Wed, 11 Oct 2023 22:38:12 -0700 Subject: [PATCH 09/10] api --- doc/api.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api.rst b/doc/api.rst index 0cf07f91df8..96b4864804f 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -110,9 +110,9 @@ Dataset contents Dataset.drop_indexes Dataset.drop_duplicates Dataset.drop_dims + Dataset.drop_encoding Dataset.set_coords Dataset.reset_coords - Dataset.reset_encoding Dataset.convert_calendar Dataset.interp_calendar Dataset.get_index @@ -303,8 +303,8 @@ DataArray contents DataArray.drop_vars DataArray.drop_indexes DataArray.drop_duplicates + DataArray.drop_encoding DataArray.reset_coords - DataArray.reset_encoding DataArray.copy DataArray.convert_calendar DataArray.interp_calendar From b808404cb59a3cc5df8d0e8009504a28b2da5e08 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Wed, 11 Oct 2023 23:08:24 -0700 Subject: [PATCH 10/10] --- doc/api-hidden.rst | 2 +- doc/user-guide/io.rst | 6 +++--- xarray/core/dataset.py | 4 +++- xarray/core/variable.py | 4 +++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/doc/api-hidden.rst b/doc/api-hidden.rst index d97c4010528..552d11a06dc 100644 --- a/doc/api-hidden.rst +++ b/doc/api-hidden.rst @@ -265,7 +265,7 @@ Variable.dims Variable.dtype Variable.encoding - Variable.reset_encoding + Variable.drop_encoding Variable.imag Variable.nbytes Variable.ndim diff --git a/doc/user-guide/io.rst b/doc/user-guide/io.rst index 2ffc25b2009..ffded682035 100644 --- a/doc/user-guide/io.rst +++ b/doc/user-guide/io.rst @@ -260,12 +260,12 @@ Note that all operations that manipulate variables other than indexing will remove encoding information. In some cases it is useful to intentionally reset a dataset's original encoding values. -This can be done with either the :py:meth:`Dataset.reset_encoding` or -:py:meth:`DataArray.reset_encoding` methods. +This can be done with either the :py:meth:`Dataset.drop_encoding` or +:py:meth:`DataArray.drop_encoding` methods. .. ipython:: python - ds_no_encoding = ds_disk.reset_encoding() + ds_no_encoding = ds_disk.drop_encoding() ds_no_encoding.encoding .. _combining multiple files: diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index d27191dfd2d..ebd6fb6f51f 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -756,7 +756,9 @@ def encoding(self, value: Mapping[Any, Any]) -> None: self._encoding = dict(value) def reset_encoding(self) -> Self: - warnings.warn("reset_encoding is deprecated, use `drop_encoding` instead") + warnings.warn( + "reset_encoding is deprecated since 2023.11, use `drop_encoding` instead" + ) return self.drop_encoding() def drop_encoding(self) -> Self: diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 9e2451af91b..fa5523b1340 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -883,7 +883,9 @@ def encoding(self, value): raise ValueError("encoding must be castable to a dictionary") def reset_encoding(self) -> Self: - warnings.warn("reset_encoding is deprecated, use `drop_encoding` instead") + warnings.warn( + "reset_encoding is deprecated since 2023.11, use `drop_encoding` instead" + ) return self.drop_encoding() def drop_encoding(self) -> Self: