Skip to content

Commit fa56937

Browse files
committed
return early if no missing values defined
1 parent a7848d3 commit fa56937

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

xarray/coding/variables.py

+22-24
Original file line numberDiff line numberDiff line change
@@ -224,34 +224,32 @@ def encode(self, variable: Variable, name: T_Name = None):
224224
fv = encoding.get("_FillValue")
225225
mv = encoding.get("missing_value")
226226

227-
if fv is not None or mv is not None:
228-
if (
229-
fv is not None
230-
and mv is not None
231-
and not duck_array_ops.allclose_or_equiv(fv, mv)
232-
):
233-
raise ValueError(
234-
f"Variable {name!r} has conflicting _FillValue ({fv}) and missing_value ({mv}). Cannot encode data."
235-
)
227+
fv_exists = fv is not None
228+
mv_exists = mv is not None
236229

237-
if fv is not None:
238-
# Ensure _FillValue is cast to same dtype as data's
239-
encoding["_FillValue"] = dtype.type(fv)
240-
fill_value = pop_to(encoding, attrs, "_FillValue", name=name)
241-
if not pd.isnull(fill_value):
242-
data = duck_array_ops.fillna(data, fill_value)
230+
if not fv_exists and not mv_exists:
231+
return variable
243232

244-
if mv is not None:
245-
# Ensure missing_value is cast to same dtype as data's
246-
encoding["missing_value"] = dtype.type(mv)
247-
fill_value = pop_to(encoding, attrs, "missing_value", name=name)
248-
if not pd.isnull(fill_value) and fv is None:
249-
data = duck_array_ops.fillna(data, fill_value)
233+
if fv_exists and mv_exists and not duck_array_ops.allclose_or_equiv(fv, mv):
234+
raise ValueError(
235+
f"Variable {name!r} has conflicting _FillValue ({fv}) and missing_value ({mv}). Cannot encode data."
236+
)
250237

251-
return Variable(dims, data, attrs, encoding, fastpath=True)
238+
if fv_exists:
239+
# Ensure _FillValue is cast to same dtype as data's
240+
encoding["_FillValue"] = dtype.type(fv)
241+
fill_value = pop_to(encoding, attrs, "_FillValue", name=name)
242+
if not pd.isnull(fill_value):
243+
data = duck_array_ops.fillna(data, fill_value)
252244

253-
else:
254-
return variable
245+
if mv_exists:
246+
# Ensure missing_value is cast to same dtype as data's
247+
encoding["missing_value"] = dtype.type(mv)
248+
fill_value = pop_to(encoding, attrs, "missing_value", name=name)
249+
if not pd.isnull(fill_value) and fv_exists:
250+
data = duck_array_ops.fillna(data, fill_value)
251+
252+
return Variable(dims, data, attrs, encoding, fastpath=True)
255253

256254
def decode(self, variable: Variable, name: T_Name = None):
257255
dims, data, attrs, encoding = unpack_for_decoding(variable)

0 commit comments

Comments
 (0)