Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename reset_encoding to drop_encoding #8287

Merged
merged 11 commits into from
Oct 12, 2023
7 changes: 6 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/max-sixty>`_.

Bug fixes
~~~~~~~~~
Expand Down
8 changes: 7 additions & 1 deletion xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,15 @@ 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"
)
self.drop_encoding()
max-sixty marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down
6 changes: 5 additions & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
max-sixty marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down
4 changes: 4 additions & 0 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
max-sixty marked this conversation as resolved.
Show resolved Hide resolved

def drop_encoding(self) -> Self:
"""Return a new Variable without encoding."""
return self._replace(encoding={})

Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2959,15 +2959,15 @@ 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"}

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 == {}
Expand Down
6 changes: 3 additions & 3 deletions xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,20 +473,20 @@ 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 == {}

# encoding set via setter
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 == {}

Expand Down
Loading