Skip to content

Commit d50a5e5

Browse files
max-sixtypre-commit-ci[bot]Illviljanheadtr1ck
authored
Rename reset_encoding to drop_encoding (#8287)
* Rename `reset_encoding` to `drop_encoding` Closes #8259 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update dataarray.py Co-authored-by: Illviljan <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update xarray/core/dataarray.py Co-authored-by: Michael Niklas <[email protected]> * Update xarray/core/variable.py Co-authored-by: Michael Niklas <[email protected]> * Update xarray/core/dataset.py Co-authored-by: Michael Niklas <[email protected]> * api --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Illviljan <[email protected]> Co-authored-by: Michael Niklas <[email protected]>
1 parent 75af56c commit d50a5e5

File tree

10 files changed

+39
-16
lines changed

10 files changed

+39
-16
lines changed

doc/api-hidden.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265
Variable.dims
266266
Variable.dtype
267267
Variable.encoding
268-
Variable.reset_encoding
268+
Variable.drop_encoding
269269
Variable.imag
270270
Variable.nbytes
271271
Variable.ndim

doc/api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ Dataset contents
110110
Dataset.drop_indexes
111111
Dataset.drop_duplicates
112112
Dataset.drop_dims
113+
Dataset.drop_encoding
113114
Dataset.set_coords
114115
Dataset.reset_coords
115-
Dataset.reset_encoding
116116
Dataset.convert_calendar
117117
Dataset.interp_calendar
118118
Dataset.get_index
@@ -303,8 +303,8 @@ DataArray contents
303303
DataArray.drop_vars
304304
DataArray.drop_indexes
305305
DataArray.drop_duplicates
306+
DataArray.drop_encoding
306307
DataArray.reset_coords
307-
DataArray.reset_encoding
308308
DataArray.copy
309309
DataArray.convert_calendar
310310
DataArray.interp_calendar

doc/user-guide/io.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ Note that all operations that manipulate variables other than indexing
260260
will remove encoding information.
261261

262262
In some cases it is useful to intentionally reset a dataset's original encoding values.
263-
This can be done with either the :py:meth:`Dataset.reset_encoding` or
264-
:py:meth:`DataArray.reset_encoding` methods.
263+
This can be done with either the :py:meth:`Dataset.drop_encoding` or
264+
:py:meth:`DataArray.drop_encoding` methods.
265265

266266
.. ipython:: python
267267
268-
ds_no_encoding = ds_disk.reset_encoding()
268+
ds_no_encoding = ds_disk.drop_encoding()
269269
ds_no_encoding.encoding
270270
271271
.. _combining multiple files:

doc/whats-new.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ Breaking changes
4545

4646
Deprecations
4747
~~~~~~~~~~~~
48-
48+
- Rename :py:meth:`Dataset.reset_encoding` & :py:meth:`DataArray.reset_encoding`
49+
to :py:meth:`Dataset.drop_encoding` & :py:meth:`DataArray.drop_encoding` for
50+
consistency with other ``drop`` & ``reset`` methods — ``drop`` generally
51+
removes something, while ``reset`` generally resets to some default or
52+
standard value. (:pull:`8287`, :issue:`8259`)
53+
By `Maximilian Roos <https://github.com/max-sixty>`_.
4954

5055
Bug fixes
5156
~~~~~~~~~

xarray/core/dataarray.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,15 @@ def encoding(self, value: Mapping[Any, Any]) -> None:
914914
self.variable.encoding = dict(value)
915915

916916
def reset_encoding(self) -> Self:
917+
warnings.warn(
918+
"reset_encoding is deprecated since 2023.11, use `drop_encoding` instead"
919+
)
920+
return self.drop_encoding()
921+
922+
def drop_encoding(self) -> Self:
917923
"""Return a new DataArray without encoding on the array or any attached
918924
coords."""
919-
ds = self._to_temp_dataset().reset_encoding()
925+
ds = self._to_temp_dataset().drop_encoding()
920926
return self._from_temp_dataset(ds)
921927

922928
@property

xarray/core/dataset.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,15 @@ def encoding(self, value: Mapping[Any, Any]) -> None:
756756
self._encoding = dict(value)
757757

758758
def reset_encoding(self) -> Self:
759+
warnings.warn(
760+
"reset_encoding is deprecated since 2023.11, use `drop_encoding` instead"
761+
)
762+
return self.drop_encoding()
763+
764+
def drop_encoding(self) -> Self:
759765
"""Return a new Dataset without encoding on the dataset or any of its
760766
variables/coords."""
761-
variables = {k: v.reset_encoding() for k, v in self.variables.items()}
767+
variables = {k: v.drop_encoding() for k, v in self.variables.items()}
762768
return self._replace(variables=variables, encoding={})
763769

764770
@property

xarray/core/variable.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,12 @@ def encoding(self, value):
883883
raise ValueError("encoding must be castable to a dictionary")
884884

885885
def reset_encoding(self) -> Self:
886+
warnings.warn(
887+
"reset_encoding is deprecated since 2023.11, use `drop_encoding` instead"
888+
)
889+
return self.drop_encoding()
890+
891+
def drop_encoding(self) -> Self:
886892
"""Return a new Variable without encoding."""
887893
return self._replace(encoding={})
888894

xarray/tests/test_dataarray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def test_encoding(self) -> None:
287287
self.dv.encoding = expected2
288288
assert expected2 is not self.dv.encoding
289289

290-
def test_reset_encoding(self) -> None:
290+
def test_drop_encoding(self) -> None:
291291
array = self.mda
292292
encoding = {"scale_factor": 10}
293293
array.encoding = encoding
@@ -296,7 +296,7 @@ def test_reset_encoding(self) -> None:
296296
assert array.encoding == encoding
297297
assert array["x"].encoding == encoding
298298

299-
actual = array.reset_encoding()
299+
actual = array.drop_encoding()
300300

301301
# did not modify in place
302302
assert array.encoding == encoding

xarray/tests/test_dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,15 +2959,15 @@ def test_copy_with_data_errors(self) -> None:
29592959
with pytest.raises(ValueError, match=r"contain all variables in original"):
29602960
orig.copy(data={"var1": new_var1})
29612961

2962-
def test_reset_encoding(self) -> None:
2962+
def test_drop_encoding(self) -> None:
29632963
orig = create_test_data()
29642964
vencoding = {"scale_factor": 10}
29652965
orig.encoding = {"foo": "bar"}
29662966

29672967
for k, v in orig.variables.items():
29682968
orig[k].encoding = vencoding
29692969

2970-
actual = orig.reset_encoding()
2970+
actual = orig.drop_encoding()
29712971
assert actual.encoding == {}
29722972
for k, v in actual.variables.items():
29732973
assert v.encoding == {}

xarray/tests/test_variable.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,20 +473,20 @@ def test_encoding_preserved(self):
473473
assert_identical(expected.to_base_variable(), actual.to_base_variable())
474474
assert expected.encoding == actual.encoding
475475

476-
def test_reset_encoding(self) -> None:
476+
def test_drop_encoding(self) -> None:
477477
encoding1 = {"scale_factor": 1}
478478
# encoding set via cls constructor
479479
v1 = self.cls(["a"], [0, 1, 2], encoding=encoding1)
480480
assert v1.encoding == encoding1
481-
v2 = v1.reset_encoding()
481+
v2 = v1.drop_encoding()
482482
assert v1.encoding == encoding1
483483
assert v2.encoding == {}
484484

485485
# encoding set via setter
486486
encoding3 = {"scale_factor": 10}
487487
v3 = self.cls(["a"], [0, 1, 2], encoding=encoding3)
488488
assert v3.encoding == encoding3
489-
v4 = v3.reset_encoding()
489+
v4 = v3.drop_encoding()
490490
assert v3.encoding == encoding3
491491
assert v4.encoding == {}
492492

0 commit comments

Comments
 (0)