Skip to content

ENH: Add default fill values for decode_cf #5680

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from functools import partial
from typing import Any, Hashable

import netCDF4
import numpy as np
import pandas as pd

Expand Down Expand Up @@ -183,6 +184,8 @@ def decode(self, variable, name=None):
pop_to(attrs, encoding, attr, name=name)
for attr in ("missing_value", "_FillValue")
]

raw_fill_values.append(netCDF4.default_fillvals["f8"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should vendor this dictionary to avoid adding netcdf4 as a required dependency.

{'S1': '\x00',
 'i1': -127,
 'u1': 255,
 'i2': -32767,
 'u2': 65535,
 'i4': -2147483647,
 'u4': 4294967295,
 'i8': -9223372036854775806,
 'u8': 18446744073709551614,
 'f4': 9.969209968386869e+36,
 'f8': 9.969209968386869e+36}

We'll also have to pick the appropriate one based on dtype.

if raw_fill_values:
encoded_fill_values = {
fv
Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_conventions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import warnings

import netCDF4
import numpy as np
import pandas as pd
import pytest
Expand Down Expand Up @@ -232,6 +233,12 @@ def test_decode_cf_with_multiple_missing_values(self):
assert_identical(expected, actual)
assert "has multiple fill" in str(w[0].message)

def test_decode_standard_missing_value(self):
original = Variable(["t"], [netCDF4.default_fillvals["f8"], 1, 2], {})
expected = Variable(["t"], [np.nan, 1, 2], {})
actual = conventions.decode_cf_variable("t", original)
assert_identical(expected, actual)

def test_decode_cf_with_drop_variables(self):
original = Dataset(
{
Expand Down